//
//  NvStyleCollectionViewCell.m
//  SDKDemo
//
//  Created by Meicam on 2018/6/5.
//  Copyright © 2018年 刘东旭. All rights reserved.
//

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

@interface NvStyleCollectionViewCell()

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

@end

@implementation NvStyleCollectionViewCell

- (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.noneImageView = [UIImageView new];
        self.noneImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        self.nameLabel = [UILabel nv_labelWithText:@"无" fontSize:11 textColor:[UIColor nv_colorWithHexRGB:@"#CCFFFFFF"]];
        self.nameLabel.alpha = 0.8;
        [self.contentView addSubview:self.imageView];
        [self.contentView addSubview:self.noneImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(3*SCREANSCALE));
            make.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);
        }];
    }
    return self;
}

- (void)renderCellWithItem:(NvCaptionStyleItem *)item {
    if (item.isSelect) {
        self.imageView.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
        self.imageView.layer.borderWidth = 1;
        self.imageView.layer.cornerRadius = 2;
        self.imageView.layer.masksToBounds = YES;
        self.nameLabel.textColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
    } else {
        self.imageView.layer.borderColor = [UIColor clearColor].CGColor;
        self.imageView.layer.borderWidth = 1;
        self.imageView.layer.cornerRadius = 2;
        self.imageView.layer.masksToBounds = YES;
        self.nameLabel.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    }
    if ([item.imageUrl containsString:@"http"]) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:item.imageUrl]];
        self.noneImageView.hidden = YES;
    } else {
        if ([item.imageUrl isEqualToString:@"NvsFilterNone"]) {
            self.imageView.image = [UIImage new];
            self.noneImageView.hidden = NO;
            self.noneImageView.image = [NvUtils imageWithName:item.imageUrl];
        }else{
            self.noneImageView.hidden = YES;
            self.imageView.image = [NvUtils imageWithName:item.imageUrl];
        }
    }
    
    self.nameLabel.text = item.name;
}

@end
