//
//  KWMOrderCell.m
//  iCemarose
//
//  Created by HouWeiBin on 16/8/30.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMOrderCell.h"
#import "KWMImageUtil.h"
#import "UIImageView+WebCache.h"
#import "UIColor+SAMAdditions.h"
#import "KWMStringUtil.h"
#import "KWMCurrencyUtil.h"

@implementation KWMOrderCell{
    BOOL isShow;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    isShow = NO;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickOrder)];
    [_vContent addGestureRecognizer:tapGesture];
    [_ivImage.layer setMasksToBounds:YES];
    _ivImage.layer.cornerRadius = 1; //圆角(圆形)
    _ivImage.layer.borderColor  = [UIColor sam_colorWithHex:@"545454"].CGColor; //要设置的颜色
    _ivImage.layer.borderWidth = 1; //要设置的描边宽
}

- (void)onClickOrder{
    if ([self.delegate respondsToSelector:@selector(kwm_onClickProduct:)]) {
        NSNumber *productId = _lineItem.productId;
        [self.delegate kwm_onClickProduct:productId];
    }
}

- (void)setMarkSection:(NSInteger)markSection{
    _markSection = markSection;
}

- (void)setLineCount:(NSInteger)lineCount{
    _lineCount = lineCount;
        [self.btnFooter setTitle:[NSString stringWithFormat:@"共%ld件商品,查看剩余%ld件商品",(long)lineCount,(lineCount - 4)] forState:UIControlStateNormal];
}

- (void)setImageUrl:(NSString *)imageUrl{
    NSURL *imageURL = [NSURL URLWithString:imageUrl];
    [self.ivImage sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
}

- (void)setOrder:(BUYOrder *)order{
    if (order != nil) {
        _order = order;
        _lbOrderNum.text = [NSString stringWithFormat:@"订单 %@",order.identifier ? order.identifier.stringValue:@""];
//        _lbTotalPrice.text = [NSString stringWithFormat:@"%@",[KWMStringUtil getEUR2CNYstring:order.totalPrice]];
//        _lbTotalPrice.text = [[KWMCurrencyUtil sharedInstance] priceFormattedByCurrencyCode:order.totalPrice];
        _lbTotalPrice.text = [[KWMCurrencyUtil sharedInstance] priceFormatted:order.totalPrice currencyCode:order.currency];
        
        __block NSInteger fulfilledCount = 0;
        [order.lineItems enumerateObjectsUsingBlock:^(BUYLineItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.fulfilledValue) {
                fulfilledCount += 1;
            }
        }];
        NSString *orderStatus = order.cancelled ? @"已取消" : fulfilledCount == 0 ? @"待发货" : fulfilledCount == order.lineItems.count ? @"已发货" : @"部分发货";
        self.lbOrderStatus.text = orderStatus;
    }
}

- (void)setLineItem:(BUYLineItem *)lineItem{
    if (lineItem != nil) {
        _lineItem = lineItem;
//        self.lbPrice.text = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:lineItem.price]];
//        self.lbPrice.text = [[KWMCurrencyUtil sharedInstance] priceFormattedByCurrencyCode:lineItem.price];
        self.lbPrice.text = [[KWMCurrencyUtil sharedInstance] priceFormatted:lineItem.price currencyCode:@"EUR"];
        self.lbName.text = lineItem.title;
        self.lbSize.text = [NSString stringWithFormat:@"x%@/%@",lineItem.quantity,lineItem.variantTitle];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

-(void)updateUI:(NSInteger)position productCount:(NSInteger)productCount showAll:(bool)showAll{
    isShow = showAll;//记录是否展开
    if(position == 0){
        self.vHeader.hidden = NO;
        //self.headerHeight.constant = 80;
        self.headerHeight.constant = 45;
    }else{
        self.vHeader.hidden = YES;
        self.headerHeight.constant = 0;
    }
    //全部没选中时,显示footView
    //选中某个section,但不选中当前的,显示footView
    if (productCount > 4) {
        self.btnFooter.hidden = NO;
        self.footHeight.constant = 40;
    }else{
        self.btnFooter.hidden = YES;
        self.footHeight.constant = 0;
    }
    
    //设置展开收起的订单footerView的title
    if (!showAll && productCount > 4) {
        if (self.lineCount) {
            [self.btnFooter setTitle:[NSString stringWithFormat:@"共%ld件商品,查看剩余%ld件商品",(long)self.lineCount,(self.lineCount - 4)] forState:UIControlStateNormal];
            [self.btnFooter setImage:nil forState:UIControlStateNormal];
        }
    }else if(showAll && productCount > 4){
        [self.btnFooter setImage:[UIImage imageNamed:@"button_myorder_pull-up"] forState:UIControlStateNormal];
        [self.btnFooter setTitle:@"" forState:UIControlStateNormal];
    }
}

-(void)onClickShowOther:(id)sender{
    if(self.delegate!=nil){
        [self.delegate kwm_onClickShowAllWith:_markSection And:isShow];
    }
}

-(void)onClickTransport:(id)sender{
    if(self.delegate!=nil){
        [self.delegate kwm_onClickTrandsport:_order];
    }
}

-(void)onClickCancelOrder:(id)sender{
    if(self.delegate!=nil){
        [self.delegate kwm_onClickCancelOrder:_order];
    }
}

@end