//
//  WKSimilarPhotoCell.m
//  WuKongClearPhotoDemo
//
//  Created by ZhangJingHao2345 on 2018/3/8.
//  Copyright © 2018年 ZhangJingHao2345. All rights reserved.
//

#import "WKSimilarPhotoCell.h"
#import "UIColor+HExtension.h"
#import "ICBasePhotoModel.h"

@interface WKSimilarPhotoCell ()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UIButton *selectBtn;
@property (nonatomic, weak) UIButton *liveLogoBtn;
@property (nonatomic, weak) UIView *coverView;
@property (nonatomic, strong) ICBasePhotoModel *item;

@end

@implementation WKSimilarPhotoCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUIWithFrame:frame];
    }
    return self;
}

- (void)setupUIWithFrame:(CGRect)frame {
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:self.bounds];
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    iconView.clipsToBounds = YES;
    [self addSubview:iconView];
    self.iconView = iconView;
    
    UIView *cover = [[UIView alloc] init];
    cover.frame = self.contentView.bounds;
    cover.backgroundColor = [[UIColor colorWithHex:0x478DFE] colorWithAlphaComponent:0.3];
    cover.hidden = true;
    [self addSubview:cover];
    self.coverView = cover;
    
    CGFloat selectWH = 20; //frame.size.width * 0.3;
    CGFloat selectX = frame.size.width - selectWH;
    UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(selectX - 5, 5, selectWH, selectWH)];
    [self addSubview:selectBtn];
    self.selectBtn = selectBtn;
    [selectBtn setImage:[UIImage imageNamed:@"contact_choose"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"contact_choosed"] forState:UIControlStateSelected];
    [selectBtn addTarget:self action:@selector(clickSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *liveLogoBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, selectX-5, selectWH + 5, selectWH + 5)];
    [self addSubview:liveLogoBtn];
    liveLogoBtn.hidden = true;
    self.liveLogoBtn = liveLogoBtn;
    [liveLogoBtn setImage:[UIImage imageNamed:@"photo_cell_live_icon"] forState:UIControlStateNormal];
    
    UIImageView *bestImageView = [[UIImageView alloc] init];
    
    NSString *language = [[NSLocale preferredLanguages] firstObject];
    if ([language containsString: @"zh"]) {
        bestImageView.image = [UIImage imageNamed:@"photo_the_best_icon"];
    } else {
        bestImageView.image = [UIImage imageNamed:@"photo_the_best_icon_en"];
    }
    bestImageView.contentMode = UIViewContentModeCenter;
    self.bestIconView = bestImageView;
    [self addSubview:bestImageView];
    self.bestIconView.hidden = true;
    [bestImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self).mas_offset(-5);
        make.width.mas_equalTo(66);
        make.height.mas_equalTo(20);
    }];
    
}

- (void)bindWithModel:(ICBasePhotoModel *)model{
    self.item = model;
    self.iconView.image = model.image;
    self.selectBtn.selected = model.isSelected;
    self.coverView.hidden = !model.isSelected;
    [self.liveLogoBtn setHidden:model.asset.mediaSubtypes != PHAssetMediaSubtypePhotoLive];
}

- (void)clickSelectBtn:(UIButton *)btn {
    btn.selected = !btn.selected;
    self.item.isSelected = btn.selected;
    self.coverView.hidden = !btn.selected;
    
    if  (_refreshViewDataClosre) {
        _refreshViewDataClosre();
    }
}

@end
