//
//  CTPrizeDetailProcessTableCellContentView.m
//  Open
//
//  Created by 雷俊博 on 2019/9/6.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CTPrizeDetailProcessTableCellContentView.h"
#import "Constants.h"
#import "Tools.h"


@interface CTPrizeDetailProcessTableCellContentView ()
{
    NSString* orederNo;
}

@property (strong, nonatomic)UILabel *infoLabel;
@property (strong, nonatomic)UILabel *dateLabel;

@property (strong, nonatomic)UILabel *infoLabel2;

@property (strong, nonatomic)UIImageView *img_error;

@property (strong, nonatomic)UIButton *btn;

@end

@implementation CTPrizeDetailProcessTableCellContentView
#pragma mark - life cycle
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.infoLabel];
        [self addSubview:self.img_error];
        [self addSubview:self.dateLabel];
        [self addSubview:self.infoLabel2];
        [self addSubview:self.btn];
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10 * WIDTHRADIU);
        make.left.offset(35 * WIDTHRADIU);
        make.height.offset(15 * WIDTHRADIU);
    }];
    
    [self.img_error mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.infoLabel);
        make.left.equalTo(self.infoLabel.mas_right).offset(3);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.infoLabel);
        make.top.equalTo(self.infoLabel.mas_bottom).offset(5 * WIDTHRADIU);
        //make.height.offset(12 * WIDTHRADIU);
    }];
    
    [self.infoLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.infoLabel);
        make.centerY.equalTo(self);
        make.height.offset(15 * WIDTHRADIU);
    }];
    
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.offset(-0);
        make.height.offset(28);
        
        make.left.greaterThanOrEqualTo(self.dateLabel.mas_right).offset(0);
    }];
    
    [self.btn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.btn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}


#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    
    
    if ([sender.titleLabel.text isEqualToString:@"提醒发货"]) {
        [Tools showMessage:@"已提醒商家发货"];
    }else if ([sender.titleLabel.text isEqualToString:@"复制单号并查询"]){
        [Tools showMessage:@"已复制订单号"];
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

        pasteboard.string = orederNo;
        
    UIViewController *vc = [self XC_GetViewControllerWithView:self];
    
    WebVC *tc=[[WebVC alloc]init];
    tc.url = @"https://m.kuaidi100.com/index_all.html";
    tc.title = @"快递";
    
    [vc.navigationController pushViewController:tc animated:YES];
     
    }
    
    
}


#pragma mark - public methods
- (void)reloadUIWithData:(NSDictionary *)dic type:(CTListPrizeType)type state:(CTListPrizeState)state Index:(NSInteger)index
{
    
    self.state = state;
    self.index = index;
    
    if (index == 0) {
        self.infoLabel.text = @"奖励已领取";
        self.img_error.image = [UIImage imageNamed:@""];
        
        if (self.state == CTListPrizeStateGiving || self.state == CTListPrizeStateFailure || self.state == CTListPrizeStateReceived) {
              self.dateLabel.text = [dic[@"orderRequestTime"] stringByReplacingOccurrencesOfString:@"/" withString:@"."];
        }else{
              self.dateLabel.text = [dic[@"createTime"] stringByReplacingOccurrencesOfString:@"/" withString:@"."];
            
        }
         self.infoLabel2.text = @"";
    }else if (index == 1){
        if (type == CTListPrizeTypeSmartCup || type == CTListPrizeTypeShoe || type == CTListPrizeTypeToyCar || type == CTListPrizeTypeElectricFan || type == CTListPrizeTypeMedicalMask || type == CTListPrizeTypeDisinfectant || type == CTListPrizeTypeTissue || type == CTListPrizeTypeFan || type == CTListPrizeTypeWashbasin || type == CTListPrizeTypeSandals || type == CTListPrizeTypeTeaSet || type == CTListPrizeTypeTeaMassageStick || type == CTListPrizeTypeMiniHumidifier || type == CTListPrizeTypeRazor || type == CTListPrizeTypeCookerTool || type == CTListPrizeTypeWashingLiquid || type == CTListPrizeTypeStorageBox || type == CTListPrizeTypeUSBElectronicScale || type == CTListPrizeTypeAutoFoldUmbrella || type == CTListPrizeTypeMosquitoKillingLamp || type == CTListPrizeTypeHUAWeiMate40 || type == CTListPrizeTypeHoldFan  || type == CTListPrizeTypequalityDrawing || type == CTListPrizeTypeTeapot || type == CTListPrizeTypeiPhone12Pro || type == CTListPrizeTypeUglyOrange || type == CTListPrizeTypeRedmi9A || type == CTListPrizeTypeSummerSleepingMat || type == CTListPrizeTypeChineseWolfberry || type == CTListPrizeTypeSummerQuilt || type == CTListPrizeTypeWashingMachine || type == CTListPrizeTypeDisposableMop || type == CTListPrizeTypeHairDrier || type == CTListPrizeTypeKitchenRack ||  type == CTListPrizeTypeMultifunctionalPluginBoard || type == CTListPrizeTypeBathroomMat || type == CTListPrizeTypeIpadMini || type == CTListPrizeTypeCeramicCasserole  || type == CTListPrizeTypeCervicalSpineMassager || type == CTListPrizeTypeWirelessBluetoothHeadset || type == CTListPrizeTypeWinterCup || type == CTListPrizeTypeBin || type == CTListPrizeTypeAirHumidifier  || type == CTListPrizeTypeCreativeLaundryBasket || type == CTListPrizeTypeBedDesk || type == CTListPrizeTypeHairBallTrimmer  || type == CTListPrizeTypePlushCushions) {
            if (state == CTListPrizeStateGiving  || type == CTListPrizeTypeSolidWoodHangers || type == CTListPrizeTypeTopCup || type == CTListPrizeTypeVirginPulpPaper || type == CTListPrizeTypeAbsorbent ||  type == CTListPrizeTypeElectronicScale || type == CTListPrizeTypeWirelessMouse || type == CTListPrizeTypeDishwashingGloves || type == CTListPrizeTypeNewYearHanged || type == CTListPrizeTypeNewYearCandy || type == CTListPrizeTypeLogNapkins || type == CTListPrizeTypeHighTempCup || type == CTListPrizeTypeHeavyAbsorbentTowel || type == CTListPrizeTypeHackTechBroom || type == CTListPrizeTypeHeadScratcherMassager || type == CTListPrizeTypeChineseRedMaskr) {
                self.infoLabel.text = @"奖品发货中...";
                if ([NSString blankString:dic[@"expressOrderNO"]]) {
                    self.dateLabel.text = @"暂无物流信息(预计三个工作日内发货)";
                }else{
                    
                    self.dateLabel.text = [NSString stringWithFormat:@"快递单号：%@",[EnercryTool AES256Decrypt:dic[@"expressOrderNO"] key:AESKEY]];
                    orederNo = [EnercryTool AES256Decrypt:dic[@"expressOrderNO"] key:AESKEY];
                    [self.btn setTitle:@"复制单号并查询" forState:UIControlStateNormal];
                }
                
                self.btn.hidden = NO;
            }else if (state == CTListPrizeStateReceived){
                self.infoLabel.text = @"奖品已领取";
                
                if (![NSString blankString:dic[@"expressOrderNO"]]) {
                    self.dateLabel.text = [NSString stringWithFormat:@"快递单号：%@",[EnercryTool AES256Decrypt:dic[@"expressOrderNO"] key:AESKEY]];
                }
            }
            
            
        }else{
            self.infoLabel.text = @"奖励发放中";
            self.dateLabel.text = @"预计两小时到账";
        }
        
        
        self.img_error.image = [UIImage imageNamed:@""];
        
        self.infoLabel2.text = @"";
    }else if (index == 2){
        
        if (self.state == CTListPrizeStateGiving) {
            self.infoLabel.text = @"";
            self.img_error.image = [UIImage imageNamed:@""];
            self.dateLabel.text = @"";
            self.infoLabel2.text = @"奖励到账";
        }else if (self.state == CTListPrizeStateReceived){
            
            if (type == CTListPrizeTypeSmartCup || type == CTListPrizeTypeShoe || type == CTListPrizeTypeToyCar || type == CTListPrizeTypeElectricFan || type == CTListPrizeTypeMedicalMask || type == CTListPrizeTypeDisinfectant || type == CTListPrizeTypeTissue || type == CTListPrizeTypeFan || type == CTListPrizeTypeWashbasin || type == CTListPrizeTypeSandals || type == CTListPrizeTypeTeaSet || type == CTListPrizeTypeTeaMassageStick || type == CTListPrizeTypeMiniHumidifier || type == CTListPrizeTypeRazor || type == CTListPrizeTypeCookerTool || type == CTListPrizeTypeWashingLiquid || type == CTListPrizeTypeStorageBox || type == CTListPrizeTypeUSBElectronicScale || type == CTListPrizeTypeAutoFoldUmbrella || type == CTListPrizeTypeMosquitoKillingLamp || type == CTListPrizeTypeHUAWeiMate40 || type == CTListPrizeTypeHoldFan  || type == CTListPrizeTypequalityDrawing || type == CTListPrizeTypeTeapot || type == CTListPrizeTypeiPhone12Pro || type == CTListPrizeTypeUglyOrange || type == CTListPrizeTypeRedmi9A || type == CTListPrizeTypeSummerSleepingMat || type == CTListPrizeTypeChineseWolfberry || type == CTListPrizeTypeSummerQuilt || type == CTListPrizeTypeWashingMachine || type == CTListPrizeTypeDisposableMop || type == CTListPrizeTypeHairDrier || type == CTListPrizeTypeKitchenRack ||  type == CTListPrizeTypeMultifunctionalPluginBoard  || type == CTListPrizeTypeBathroomMat || type == CTListPrizeTypeIpadMini || type == CTListPrizeTypeCeramicCasserole  || type == CTListPrizeTypeCervicalSpineMassager || type == CTListPrizeTypeWirelessBluetoothHeadset || type == CTListPrizeTypeWinterCup || type == CTListPrizeTypeBin || type == CTListPrizeTypeAirHumidifier  || type == CTListPrizeTypeCreativeLaundryBasket || type == CTListPrizeTypeBedDesk || type == CTListPrizeTypeHairBallTrimmer  || type == CTListPrizeTypePlushCushions || type == CTListPrizeTypeSolidWoodHangers || type == CTListPrizeTypeTopCup || type == CTListPrizeTypeVirginPulpPaper || type == CTListPrizeTypeAbsorbent ||  type == CTListPrizeTypeElectronicScale || type == CTListPrizeTypeWirelessMouse || type == CTListPrizeTypeDishwashingGloves || type == CTListPrizeTypeNewYearHanged || type == CTListPrizeTypeNewYearCandy || type == CTListPrizeTypeLogNapkins || type == CTListPrizeTypeHighTempCup || type == CTListPrizeTypeHeavyAbsorbentTowel || type == CTListPrizeTypeHackTechBroom || type == CTListPrizeTypeHeadScratcherMassager || type == CTListPrizeTypeChineseRedMaskr) {
                self.infoLabel.text = @"奖品已收到(快递已签收)";
                self.img_error.image = [UIImage imageNamed:@""];
                self.dateLabel.text = @"有疑问可联系客服";
                self.infoLabel2.text = @"";
            }else{
                self.infoLabel.text = @"奖励已到账";
                self.img_error.image = [UIImage imageNamed:@""];
                self.dateLabel.text = [dic[@"payTime"] stringByReplacingOccurrencesOfString:@"/" withString:@"."];
                self.infoLabel2.text = @"";
            }
            
           
        }else if (self.state == CTListPrizeStateFailure){
            
            if (type == CTListPrizeTypeSmartCup || type == CTListPrizeTypeShoe || type == CTListPrizeTypeToyCar || type == CTListPrizeTypeElectricFan || type == CTListPrizeTypeMedicalMask || type == CTListPrizeTypeDisinfectant || type == CTListPrizeTypeTissue || type == CTListPrizeTypeFan || type == CTListPrizeTypeWashbasin || type == CTListPrizeTypeSandals || type == CTListPrizeTypeTeaSet || type == CTListPrizeTypeTeaMassageStick || type == CTListPrizeTypeMiniHumidifier || type == CTListPrizeTypeRazor || type == CTListPrizeTypeCookerTool || type == CTListPrizeTypeWashingLiquid || type == CTListPrizeTypeStorageBox || type == CTListPrizeTypeUSBElectronicScale || type == CTListPrizeTypeAutoFoldUmbrella || type == CTListPrizeTypeMosquitoKillingLamp || type == CTListPrizeTypeHUAWeiMate40 || type == CTListPrizeTypeHoldFan  || type == CTListPrizeTypequalityDrawing || type == CTListPrizeTypeTeapot || type == CTListPrizeTypeiPhone12Pro || type == CTListPrizeTypeUglyOrange || type == CTListPrizeTypeRedmi9A || type == CTListPrizeTypeSummerSleepingMat || type == CTListPrizeTypeChineseWolfberry || type == CTListPrizeTypeSummerQuilt || type == CTListPrizeTypeWashingMachine || type == CTListPrizeTypeDisposableMop || type == CTListPrizeTypeHairDrier || type == CTListPrizeTypeKitchenRack ||  type == CTListPrizeTypeMultifunctionalPluginBoard  || type == CTListPrizeTypeBathroomMat || type == CTListPrizeTypeIpadMini || type == CTListPrizeTypeCeramicCasserole  || type == CTListPrizeTypeCervicalSpineMassager || type == CTListPrizeTypeWirelessBluetoothHeadset || type == CTListPrizeTypeWinterCup || type == CTListPrizeTypeBin || type == CTListPrizeTypeAirHumidifier  || type == CTListPrizeTypeCreativeLaundryBasket || type == CTListPrizeTypeBedDesk || type == CTListPrizeTypeHairBallTrimmer  || type == CTListPrizeTypePlushCushions || type == CTListPrizeTypeSolidWoodHangers || type == CTListPrizeTypeTopCup || type == CTListPrizeTypeVirginPulpPaper || type == CTListPrizeTypeAbsorbent ||  type == CTListPrizeTypeElectronicScale || type == CTListPrizeTypeWirelessMouse || type == CTListPrizeTypeDishwashingGloves || type == CTListPrizeTypeNewYearHanged || type == CTListPrizeTypeNewYearCandy || type == CTListPrizeTypeLogNapkins || type == CTListPrizeTypeHighTempCup || type == CTListPrizeTypeHeavyAbsorbentTowel || type == CTListPrizeTypeHackTechBroom || type == CTListPrizeTypeHeadScratcherMassager || type == CTListPrizeTypeChineseRedMaskr)
            {
                self.infoLabel.text = @"奖励发放失败";
                self.img_error.image = [UIImage imageNamed:@"奖品详情-警示"];
                self.dateLabel.text = @"请联系客服处理";
                self.infoLabel2.text = @"";
            }else{
                self.infoLabel.text = @"奖励发放失败";
                self.img_error.image = [UIImage imageNamed:@"奖品详情-警示"];
                self.dateLabel.text = @"请确认微信账号是否实名";
                self.infoLabel2.text = @"";
            }
            
            
        }else{
            self.infoLabel.text = @"";
            self.img_error.image = [UIImage imageNamed:@""];
            self.dateLabel.text = @"";
            self.infoLabel2.text = @"奖励到账";
        }
       
    }
    [self setNeedsDisplay];
}

/**
 *  获取父视图的控制器
 *
 *  @return 父视图的控制器
 */
- (UIViewController *)XC_GetViewControllerWithView:(UIView *)view
{
    for (UIView* next = [view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


#pragma mark - getters and setters
- (UILabel *)infoLabel
{
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]init];
        _infoLabel.text = @"奖励已领取";
        _infoLabel.textColor = [UIColor colorWithHex:0xfe5733];
        _infoLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        _infoLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _infoLabel;
}

- (UIImageView *)img_error
{
    if (!_img_error) {
        _img_error = [[UIImageView alloc]init];
    }
    return _img_error;
}

- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.text = @"2019.09.02  15:40";
        _dateLabel.textColor = [UIColor colorWithHex:0x808080];
        _dateLabel.font = [UIFont systemFontOfSize:12 * WIDTHRADIU];
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.numberOfLines = 0;
    }
    return _dateLabel;
}

- (UILabel *)infoLabel2
{
    if (!_infoLabel2) {
        _infoLabel2 = [[UILabel alloc]init];
        _infoLabel2.text = @"奖励到账";
        _infoLabel2.textColor = [UIColor colorWithHex:0xb2b2b2];
        _infoLabel2.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        _infoLabel2.textAlignment = NSTextAlignmentCenter;
    }
    return _infoLabel2;
}

- (UIButton *)btn
{
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn setTitle:@"提醒发货" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor colorWithHex:0xfe5733] forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:12];
        _btn.layer.borderWidth = 0.5;
        _btn.layer.borderColor = [UIColor colorWithHex:0xfe5733].CGColor;
        _btn.layer.cornerRadius = 4;
        _btn.hidden = YES;
        _btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        
        [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btn;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGFloat height = self.bounds.size.height;
    
    
    if (self.hasUpLine) {
        
       
        
        if (self.state == CTListPrizeStateGiving || self.state == CTListPrizeStateFailure) {
            
            if (self.index == 2) {
                UIBezierPath *topBezier = [UIBezierPath bezierPath];
                [topBezier moveToPoint:CGPointMake(10 * WIDTHRADIU +7.5, 0)];
                [topBezier addLineToPoint:CGPointMake(10 * WIDTHRADIU +7.5, (height -15)/2.0 + 2.5)];
                
                topBezier.lineWidth = 1.0;
                UIColor *stroke = [UIColor colorWithHex:0x808080];
                [stroke set];
                [topBezier stroke];
            }else{
                UIBezierPath *topBezier = [UIBezierPath bezierPath];
                [topBezier moveToPoint:CGPointMake(10 * WIDTHRADIU +7.5, 0)];
                [topBezier addLineToPoint:CGPointMake(10 * WIDTHRADIU +7.5, (height -15)/2.0 + 2.5)];
                
                topBezier.lineWidth = 1.0;
                UIColor *stroke = [UIColor colorWithHex:0xfe5733];
                [stroke set];
                [topBezier stroke];
            }
           
        }else{
            UIBezierPath *topBezier = [UIBezierPath bezierPath];
            [topBezier moveToPoint:CGPointMake(10 * WIDTHRADIU +7.5, 0)];
            [topBezier addLineToPoint:CGPointMake(10 * WIDTHRADIU +7.5, height/2.0 - 5)];
            
            topBezier.lineWidth = 1.0;
            UIColor *stroke = [UIColor colorWithHex:0xfe5733];
            [stroke set];
            [topBezier stroke];
        }
    }
    
    if (self.currented) {
        
        
        if (self.state == CTListPrizeStateGiving || self.state == CTListPrizeStateFailure) {
            
            if (self.index == 2) {
                UIBezierPath *cicle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
                
                cicle.lineWidth = 1.0;
                UIColor *cColor = [UIColor colorWithHex:0x808080];
                [cColor set];
                [cicle fill];
                [cicle stroke];
                
                UIBezierPath *cicle2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:7.5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
                
                cicle2.lineWidth = 1.0;
                UIColor *cColor2 = [UIColor colorWithHex:0x808080];
                [cColor2 set];
                [cicle2 stroke];
            }else{
                UIBezierPath *cicle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
                
                cicle.lineWidth = 1.0;
                UIColor *cColor = [UIColor colorWithHex:0xfe5633];
                [cColor set];
                [cicle fill];
                [cicle stroke];
                
                UIBezierPath *cicle2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:7.5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
                
                cicle2.lineWidth = 1.0;
                UIColor *cColor2 = [UIColor colorWithHex:0xfe5633];
                [cColor2 set];
                [cicle2 stroke];
            }
            
        }else{
            UIBezierPath *cicle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
            
            cicle.lineWidth = 1.0;
            UIColor *cColor = [UIColor colorWithHex:0xfe5633];
            [cColor set];
            [cicle fill];
            [cicle stroke];
            
            UIBezierPath *cicle2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(10 * WIDTHRADIU +7.5, height/2) radius:7.5 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
            
            cicle2.lineWidth = 1.0;
            UIColor *cColor2 = [UIColor colorWithHex:0xfe5633];
            [cColor2 set];
            [cicle2 stroke];
        }
        
        
    }
    
    if (self.hasDownLine) {
        
        UIBezierPath *downBezier = [UIBezierPath bezierPath];
        [downBezier moveToPoint:CGPointMake(10 * WIDTHRADIU +7.5, (height - 15)/2.0 +15 - 2.5)];
        [downBezier addLineToPoint:CGPointMake(10 * WIDTHRADIU +7.5, height)];
        
        downBezier.lineWidth = 1.0;
        UIColor *stroke = [UIColor colorWithHex:0xfe5733];
        [stroke set];
        [downBezier stroke];
    }
}


@end
