//
//  KWMBrandCaramelCell.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/29.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMBrandCaramelCell.h"
#import "UIImageView+WebCache.h"
#import "KWMUserDao.h"
#import "KWMStringUtil.h"
#import "KWMImageUtil.h"
#import "KWMVariants.h"


@implementation KWMBrandCaramelCell

#define Width UI_SCREEN_WIDTH*0.5
#define ruleWidth UI_SCREEN_WIDTH/2 - 20
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self initView];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)initView{

    self.lbPriceLeft.textColor = [UIColor colorWithRed:151.0/255 green:151.0/255 blue:151.0/255 alpha:1];
    self.lbDiscountLeft.textColor = [UIColor colorWithRed:241.0/255 green:146.0/255 blue:154.0/255 alpha:1];
    self.lbPriceRight.textColor = [UIColor colorWithRed:151.0/255 green:151.0/255 blue:151.0/255 alpha:1];
    self.lbDiscountRight.textColor = [UIColor colorWithRed:241.0/255 green:146.0/255 blue:154.0/255 alpha:1];
    [self initRule];
    [self addGesture];
}

- (void)initRule{
    
    CGFloat height = self.vRootLeft.frame.size.height - 84;
    _vRuleLeft = [[KWMRuleView alloc] initWithFrame:CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height)];
    [self.ivIconLeft addSubview:_vRuleLeft];
    
    height = self.vRootRight.frame.size.height - 84;
    _vRuleRight = [[KWMRuleView alloc] initWithFrame:CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height)];
    [self.ivIconRight addSubview:_vRuleRight];
}

//添加手势
- (void)addGesture{
    //添加图片手势
    UITapGestureRecognizer *leftImagGeustre = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLeftImage:)];
    [self.ivIconLeft addGestureRecognizer:leftImagGeustre];
    
     UITapGestureRecognizer *rightImagGeustre = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRightImage:)];
    [self.ivIconRight addGestureRecognizer:rightImagGeustre];
    
    //添加收起标尺背景手势
    UITapGestureRecognizer *leftTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLeftBackground:)];
    [self.vLBackground addGestureRecognizer:leftTapGesture];
    
    UITapGestureRecognizer *rightTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRightBackground:)];
    [self.vRBackground addGestureRecognizer:rightTapGesture];
    
}

- (void)onClickLeftImage:(UITapGestureRecognizer *)leftImagGeustre{
    if ([self.delegate respondsToSelector:@selector(kwm_onClickLeftIamge:)]) {
        [self.delegate kwm_onClickLeftIamge:self];
    }
}

- (void)onClickRightImage:(UITapGestureRecognizer *)rightImagGeustre{
    if ([self.delegate respondsToSelector:@selector(kwm_onClickRightIamge:)]) {
        [self.delegate kwm_onClickRightIamge:self];
    }
}

- (void)onClickLeftBackground:(UITapGestureRecognizer *)leftTapGesture{
    self.btnRuleLeft.selected = !self.btnRuleLeft.selected;
    self.vLBackground.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
    }];
}

- (void)onClickRightBackground:(UITapGestureRecognizer *)rightTapGesture{
    self.btnRuleRight.selected = !self.btnRuleRight.selected;
    self.vRBackground.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
    }];
}

//点击左边标尺
- (IBAction)onClickRuleLeftBtn:(id)sender {
    [self setLeftRlueAnimation];
}

//点击右边标尺
- (IBAction)onClickRuleRightBtn:(id)sender {
    [self setRightRlueAnimation];
}

- (void)setLeftRlueAnimation{
    self.btnRuleLeft.selected = !self.btnRuleLeft.selected;
    self.vLBackground.userInteractionEnabled = NO;
//    self.btnRuleLeft.userInteractionEnabled = NO;
//    [UIView animateWithDuration:0.5 animations:^{
//        if (self.btnRuleLeft.selected) {
//            _vRuleLeft.frame = CGRectMake(0, 0, ruleWidth, self.ivIconLeft.frame.size.height);
//        }else{
//            _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
//        }
//    } completion:^(BOOL finished) {
        self.vLBackground.userInteractionEnabled = YES;
        self.vLBackground.hidden = NO;
        self.btnRuleLeft.userInteractionEnabled = YES;
    
        if ([self.delegate respondsToSelector:@selector(kwm_openRule:Or:And:)]) {
            [self.delegate kwm_openRule:_leftProduct Or:_leftDataProduct And:self];
        }
    
    
//    }];
}

- (void)setRightRlueAnimation{
    self.btnRuleRight.selected = !self.btnRuleRight.selected;
//    self.vRBackground.userInteractionEnabled = NO;
//    self.btnRuleRight.userInteractionEnabled = NO;
//    [UIView animateWithDuration:0.5 animations:^{
//        if (self.btnRuleRight.selected) {
//            _vRuleRight.frame = CGRectMake(0, 0, ruleWidth, self.ivIconRight.frame.size.height);
//        }else{
//            _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
//        }
//    } completion:^(BOOL finished) {
        self.vRBackground.hidden = NO;
        self.vRBackground.userInteractionEnabled = YES;
        self.btnRuleRight.userInteractionEnabled = YES;
        if ([self.delegate respondsToSelector:@selector(kwm_openRule:Or:And:)]) {
            [self.delegate kwm_openRule:_rightProduct Or:_rightDataProduct And:self];
        }
//    }];
}

- (void)initFrame{
    
    self.vRBackground.hidden = YES;
    self.vRBackground.userInteractionEnabled = NO;
    self.btnRuleRight.userInteractionEnabled = YES;
    self.vLBackground.hidden = YES;
    self.vLBackground.userInteractionEnabled = NO;
    self.btnRuleLeft.userInteractionEnabled = YES;
    
    _btnRuleRight.selected = NO;
    _btnRuleLeft.selected = NO;
    _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
    _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
}

//展开标尺
- (void)openRule:(BOOL)isLeft{
    if (isLeft) {//展开左边标尺
        self.btnRuleLeft.selected = YES;
        self.vLBackground.userInteractionEnabled = NO;
        self.btnRuleLeft.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
           
            _vRuleLeft.frame = CGRectMake(0, 0, ruleWidth, self.ivIconLeft.frame.size.height);
        } completion:^(BOOL finished) {
            self.vLBackground.userInteractionEnabled = YES;
            self.vLBackground.hidden = NO;
            self.btnRuleLeft.userInteractionEnabled = YES;
        }];
    }else{//展开右边标尺
        self.btnRuleRight.selected = YES;
        self.vRBackground.userInteractionEnabled = NO;
        self.btnRuleRight.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
            _vRuleRight.frame = CGRectMake(0, 0, ruleWidth, self.ivIconRight.frame.size.height);
        } completion:^(BOOL finished) {
            self.vRBackground.hidden = NO;
            self.vRBackground.userInteractionEnabled = YES;
            self.btnRuleRight.userInteractionEnabled = YES;
        }];
    }
}

- (void)setPrice:(NSNumber *)price WithComparePrice:(NSNumber *)compareAtPrice And:(BOOL)isLeft{
    if(price == nil){
        price = @(0);
    }
    if(compareAtPrice == nil){
        compareAtPrice = @(0);
    }
    if( compareAtPrice.integerValue!=0 ){
        NSString *oldString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:compareAtPrice]];
        //中划线
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:oldString attributes:attribtDic];
        
        if (isLeft) {
            self.priceLeft.constant = -30;
            self.lbDiscountLeft.hidden = NO;
            self.lbPriceLeft.attributedText = attribtStr;
            self.lbDiscountLeft.text = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
        }else{
            self.priceRight.constant = -30;
            self.lbDiscountRight.hidden = NO;
            self.lbPriceRight.attributedText = attribtStr;
            self.lbDiscountRight.text =  [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
        }
    }else{
        if (isLeft) {
            self.priceLeft.constant = 0;
            self.lbDiscountLeft.hidden = YES;
            self.lbPriceLeft.text =  [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring: price]];
        }else{
            self.priceRight.constant = 0;
            self.lbDiscountRight.hidden = YES;
            self.lbPriceRight.text = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
        }
    }
}

//搜索Model赋值
- (void)setDataProduct:(KWMDataProduct *)leftDataProduct rightProduct:(KWMDataProduct *)rightDataProduct{
    [self.ivIconLeft setImage:[UIImage imageNamed:@"ic_loading"]];
    [self.ivIconRight setImage:[UIImage imageNamed:@"ic_loading"]];
    
    [self initFrame];
    if(leftDataProduct == nil && rightDataProduct == nil){
        return;
    }
    self.leftDataProduct = leftDataProduct;
    self.rightDataProduct = rightDataProduct;
    if(leftDataProduct != nil){
        [self.vRuleLeft setDataProduct:leftDataProduct];
        
        self.lbBrandLeft.text = leftDataProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandLeft.text = leftDataProduct.vendor;
        }
        
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:leftDataProduct ImageSize:NormalImage];
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconLeft sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        
        KWMVariants *variant = leftDataProduct.variants.firstObject;
        if(variant == nil){
            return;
        }
        [self setPrice:variant.price WithComparePrice:variant.compareAtPrice And:YES];
    }
    if(rightDataProduct != nil){
        [self.vRuleRight setDataProduct:rightDataProduct];
        self.vRootRight.hidden = NO;
        
        self.lbBrandRight.text = rightDataProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandRight.text = rightDataProduct.vendor;
        }
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:rightDataProduct ImageSize:NormalImage];
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconRight sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];

        KWMVariants *variant = rightDataProduct.variants.firstObject;
        if(variant == nil){
            return;
        }
        [self setPrice:variant.price WithComparePrice:variant.compareAtPrice And:NO];
    }else{
        self.vRootRight.hidden = YES;
    }
}

- (void)setData:(BUYProduct *)leftProduct rightProduct:(BUYProduct *)rightProduct{
    
    [self initFrame];
    if(leftProduct == nil && rightProduct == nil){
        return;
    }
    self.leftProduct = leftProduct;
    self.rightProduct = rightProduct;
    if(leftProduct != nil){
        [self.vRuleLeft setData:leftProduct];
        self.lbBrandLeft.text = leftProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandLeft.text = leftProduct.vendor;
        }
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:leftProduct ImageSize:NormalImage];
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconLeft sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        BUYProductVariant *productVariant = leftProduct.variants.firstObject;
        if(productVariant == nil){
            return;
        }
        
        [self setPrice:productVariant.price WithComparePrice:productVariant.compareAtPrice And:YES];
    }
    if(rightProduct != nil){
        [self.vRuleRight setData:rightProduct];
        self.vRootRight.hidden = NO;
        self.lbBrandRight.text = rightProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandRight.text = rightProduct.vendor;
        }
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:rightProduct ImageSize:NormalImage];
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconRight sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        BUYProductVariant *productVariant= rightProduct.variants.firstObject;
        if(productVariant == nil){
            return;
        }
        [self setPrice:productVariant.price WithComparePrice:productVariant.compareAtPrice And:NO];
        
    }else{
        self.vRootRight.hidden = YES;
    }
}

@end