//
//  NvFilterCollectionViewCell.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/7/2.
 //
//

#import "NvFilterCollectionViewCell.h"
#import "NVHeader.h"
#import "UIImageView+WebCache.h"

@interface NvFilterCollectionViewCell()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *noneImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView *selectView;

@end

@implementation NvFilterCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        self.imageView = [UIImageView new];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView.layer.masksToBounds = YES;
        self.imageView.layer.cornerRadius = 4*SCREANSCALE;
        self.noneImageView = [UIImageView new];
        self.noneImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.nameLabel = [UILabel nv_labelWithText:@"无" fontSize:11 textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"]];
        self.nameLabel.font = [NvUtils regularFontWithSize:11];
        self.selectView = [UIView new];
        self.selectView.backgroundColor = [[UIColor nv_colorWithHexRGB:@"#52D3FF"] colorWithAlphaComponent:.7];
        self.selectView.layer.cornerRadius = 4.0f;
        self.selectView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.imageView];
        [self.contentView addSubview:self.noneImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.selectView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(@0);
            make.height.equalTo(@(49*SCREANSCALE));
        }];
        [self.noneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(@(12*SCREANSCALE));
            make.right.equalTo(@(-12*SCREANSCALE));
            make.height.equalTo(@(25*SCREANSCALE));
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).offset(8*SCREANSCALE);
            make.centerX.equalTo(self.imageView);
            make.left.right.equalTo(@0);
        }];
        [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.imageView);
        }];
    }
    return self;
}

- (void)renderCellWithItem:(NvEditFilterItem *)item {
    if (item.isSelect) {
        self.selectView.hidden = NO;
        self.nameLabel.textColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
    } else {
        self.selectView.hidden = YES;
        self.nameLabel.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    }
    
    if ([item.imageUrl containsString:@"http"]) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:item.imageUrl]];
        self.imageView.hidden = NO;
        self.noneImageView.hidden = YES;
    } else {
        if ([item.imageUrl isEqualToString:@"NvsFilterNone"]) {
            self.imageView.hidden = YES;
            self.noneImageView.hidden = NO;
            self.noneImageView.image = [NvUtils imageWithName:item.imageUrl];
        }else{
            self.imageView.hidden = NO;
            self.noneImageView.hidden = YES;
            self.imageView.image = [NvUtils imageWithName:item.imageUrl];
        }
    }
    
    self.nameLabel.text = item.name;
}

@end
