//
//  RDSDeviceTypeCell.m
//  Temperature
//
//  Created by 姓二名哈字富贵儿 on 2023/8/24.
//

#import "RDSDeviceTypeCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface RDSDeviceTypeCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIImageView *deviceTypeView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *englishNameLabel;
@property (nonatomic, strong) UIView *circularView;

@property (nonatomic, strong) NSDictionary *codeToNameDic;
@property (nonatomic, strong) NSDictionary *codeToContainerColorDic;
@property (nonatomic, strong) NSDictionary *codeToCircularColorDic;

@end

@implementation RDSDeviceTypeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithHexString:@"#FBFBFB"];
        
        self.containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 40, 110 * SCREEN_WIDTH / 375)];
        self.containerView.layer.cornerRadius = 6;
        self.containerView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.containerView];
        [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 20, 0, 20));
            make.height.mas_equalTo(110.0 * SCREEN_WIDTH / 375.0);
        }];
        
        CGRect deviceTypeViewFrame = CGRectMake(36, (self.containerView.frame.size.height - 45) / 2, 45, 45);
        
        self.circularView = [[UIView alloc] initWithFrame:CGRectMake(deviceTypeViewFrame.origin.x + 27, deviceTypeViewFrame.origin.y + 25, 30, 30)];
        self.circularView.layer.cornerRadius = 15;
        self.circularView.layer.masksToBounds = YES;
        [self.containerView addSubview:self.circularView];
        
        self.deviceTypeView = [[UIImageView alloc] initWithFrame:deviceTypeViewFrame];
        [self.containerView addSubview:self.deviceTypeView];
        [self.deviceTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(36);
            make.size.mas_equalTo(45);
        }];
        
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.font = [UIFont pingFangSCMediumOfSize:18];
        self.nameLabel.textColor = [UIColor colorWithHexString:@"#31517D"];
        [self.containerView addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.deviceTypeView.mas_right).offset(40);
            make.centerY.mas_equalTo(-11);
            make.height.mas_equalTo(25);
            make.right.mas_equalTo(-36);
        }];
        
        self.englishNameLabel = [[UILabel alloc] init];
        self.englishNameLabel.font = [UIFont pingFangSCRegularOfSize:13];
        self.englishNameLabel.textColor = [UIColor colorWithHexString:@"#31517D"];
        [self.containerView addSubview:self.englishNameLabel];
        [self.englishNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.nameLabel);
            make.height.mas_equalTo(18);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        }];
        
        
    }
    return self;
}

- (void)setDeviceTypeModel:(RDSDeviceTypeModel *)deviceTypeModel {
    _deviceTypeModel = deviceTypeModel;
    for (CALayer *sublayer in self.containerView.layer.sublayers) {
        if ([sublayer isKindOfClass:[CAGradientLayer class]]) {
            [sublayer removeFromSuperlayer];
            break;
        }
    }
    [self.containerView.layer addSublayer:self.codeToContainerColorDic[deviceTypeModel.code]];
    
    for (CALayer *sublayer in self.circularView.layer.sublayers) {
        if ([sublayer isKindOfClass:[CAGradientLayer class]]) {
            [sublayer removeFromSuperlayer];
            break;
        }
    }
    [self.circularView.layer addSublayer:self.codeToCircularColorDic[deviceTypeModel.code]];
    
    [self.deviceTypeView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, deviceTypeModel.icon]] placeholderImage:[UIImage imageNamed:@"icon_fluorine_sub_controller"]];
    self.nameLabel.text = deviceTypeModel.name;
    self.englishNameLabel.text = self.codeToNameDic[deviceTypeModel.code];
}

- (NSDictionary *)codeToNameDic {
    if (!_codeToNameDic) {
        _codeToNameDic = @{@"YXK-Z/86-FG-A": @"main controller",
                           @"YXK-F/86-FG-A": @"sub controller",
                           @"YXK-Z/86-FC-B": @"main controller",
                           @"YXK-F/86-FC-B": @"sub controller",
                           @"YXK-P/DN-JF-8": @"intelligent water collector"};
    }
    return _codeToNameDic;
}

- (NSDictionary *)codeToContainerColorDic {
    if (!_codeToContainerColorDic) {
        _codeToContainerColorDic = @{@"YXK-Z/86-FG-A": [self containerViewLayerWithColorStart:@"#97C2FF" colorEnd:@"#5198FD"],
                                     @"YXK-F/86-FG-A": [self containerViewLayerWithColorStart:@"#FAE06A" colorEnd:@"#F0B756"],
                                     @"YXK-Z/86-FC-B": [self containerViewLayerWithColorStart:@"#8BFAE1" colorEnd:@"#21B191"],
                                     @"YXK-F/86-FC-B": [self containerViewLayerWithColorStart:@"#55DCDF" colorEnd:@"#4AB0E4"],
                                     @"YXK-P/DN-JF-8": [self containerViewLayerWithColorStart:@"#B1EB5C" colorEnd:@"#60A736"],
                                     @"INFRARED": [self containerViewLayerWithColorStart:@"#B1EB5C" colorEnd:@"#60A736"]};
    }
    return _codeToContainerColorDic;
}

- (NSDictionary *)codeToCircularColorDic {
    if (!_codeToCircularColorDic) {
        _codeToCircularColorDic = @{@"YXK-Z/86-FG-A": [self circularViewLayerWithColorStart:@"#97C2FF" colorEnd:@"#5198FD"],
                                    @"YXK-F/86-FG-A": [self circularViewLayerWithColorStart:@"#FAE06A" colorEnd:@"#F0B756"],
                                    @"YXK-Z/86-FC-B": [self circularViewLayerWithColorStart:@"#8BFAE1" colorEnd:@"#21B191"],
                                    @"YXK-F/86-FC-B": [self circularViewLayerWithColorStart:@"#55DCDF" colorEnd:@"#4AB0E4"],
                                    @"YXK-P/DN-JF-8": [self circularViewLayerWithColorStart:@"#B1EB5C" colorEnd:@"#60A736"],
                                    @"INFRARED": [self circularViewLayerWithColorStart:@"#B1EB5C" colorEnd:@"#60A736"]};
    }
    return _codeToCircularColorDic;
}

- (CAGradientLayer *)containerViewLayerWithColorStart:(NSString *)colorStart colorEnd:(NSString *)colorEnd {
    return [self createGradientLayerWithColorStart:colorStart colorEnd:colorEnd frame:self.containerView.bounds alpha:0.1];
}

- (CAGradientLayer *)circularViewLayerWithColorStart:(NSString *)colorStart colorEnd:(NSString *)colorEnd {
    return [self createGradientLayerWithColorStart:colorStart colorEnd:colorEnd frame:self.circularView.bounds alpha:0.2];
}

- (CAGradientLayer *)createGradientLayerWithColorStart:(NSString *)colorStart colorEnd:(NSString *)colorEnd frame:(CGRect)frame alpha:(CGFloat)alpha {
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = frame;
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithHexString:colorStart alpha:alpha].CGColor, (__bridge id)[UIColor colorWithHexString:colorEnd alpha:alpha].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    return gl;
}

@end
