//
//  RDSRoomCell.m
//  Temperature
//
//  Created by RD on 2022/10/31.
//

#import "RDSRoomCell.h"
#import <SDWebImage/SDWebImage.h>

@interface RDSRoomCell()

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *roomNameLabel;
@property (nonatomic, strong) UILabel *deviceTypeLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *pointView;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UISwitch *deviceSwitch;
@property (nonatomic, strong) UIButton *deleteButton;

@property (nonatomic, strong) NSDictionary *codeToNameDic;

@end

@implementation RDSRoomCell

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


- (void)setModel:(RDSRoomModel *)model {
    _model = model;
    self.roomNameLabel.text = model.name;
//    if (isEmptyString(model.code)) {
//        self.iconView.image = [UIImage imageNamed:@"icon_fluorine_sub_controller"];
//    } else {
//        self.iconView.image = [UIImage imageNamed:self.codeToNameDic[model.code]];
//    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]] placeholderImage:[UIImage imageNamed:@"icon_fluorine_sub_controller"]];
    
    
    if (isEmptyString(model.device_type_name)) {
        self.deviceTypeLabel.text = @"分控制器";
    } else {
        self.deviceTypeLabel.text = model.device_type_name;
    }
    // 如果是永续精灵则隐藏相应的组件显示
    if (model.deviceType == RDSDeviceType_YongXuSpirit) {
        self.deviceSwitch.hidden = true;
    } else {
        self.deviceSwitch.hidden = false;
        self.lineView.hidden = false;
        self.statusLabel.hidden = false;
        self.pointView.hidden = false;
        self.deviceSwitch.on = model.power;
    }
    
    
//    RDSHomeModel *currentHome = TheDataManager.detailHome;
    if (model.is_online) {
        self.statusLabel.text = [NSString stringWithFormat:@"温度%@°C 湿度%@%%", @(model.temperature), @(model.humidity)];
        self.statusLabel.textColor = [UIColor colorWithHexString:@"#14C9C8"];
        self.pointView.backgroundColor = [UIColor colorWithHexString:@"#14C9C8"];
        if (model.deviceType == RDSDeviceType_YongXuSpirit) {
            self.statusLabel.text = @"在线";
        }
    } else {
        self.statusLabel.text = @"已离线";
        self.statusLabel.textColor = [UIColor colorWithHexString:@"#FAAD14"];
        self.pointView.backgroundColor = [UIColor colorWithHexString:@"#FAAD14"];
    }
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    
    if (isEdit) {
        [self.deviceSwitch removeFromSuperview];
        [self.contentView addSubview:self.deleteButton];
        [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(48, 48));
            make.right.equalTo(self.containerView).offset(-16);
            make.centerY.mas_equalTo(0);
        }];
        
        [self.roomNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.deleteButton.mas_left).offset(-12);
        }];
    } else {
        [self.deleteButton removeFromSuperview];
        [self.contentView addSubview:self.deviceSwitch];
        [self.deviceSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(48, 24));
            make.centerY.mas_equalTo(0);
            make.right.equalTo(self.containerView).offset(-16);
        }];
        
        [self.roomNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.deviceSwitch.mas_left).offset(-12);
        }];
    }
}


- (void)onPowerChanged:(UISwitch *)powerSwitch {
    BLOCK_SAFE_RUN(_onPowerClick, powerSwitch.isOn);
}

- (void)deleteButtonClicked {
    BLOCK_SAFE_RUN(_onDelClick);
}

- (void)configSubviews {
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.iconView];
    [self.containerView addSubview:self.roomNameLabel];
    [self.containerView addSubview:self.deviceTypeLabel];
    [self.containerView addSubview:self.lineView];
    [self.containerView addSubview:self.pointView];
    [self.containerView addSubview:self.statusLabel];
    [self.containerView addSubview:self.deviceSwitch];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.containerView).offset(20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.bottom.equalTo(self.containerView).offset(-20);
    }];
    
    [self.deviceSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(48, 24));
        make.centerY.mas_equalTo(0);
        make.right.equalTo(self.containerView).offset(-16);
    }];
    
    [self.roomNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(12);
        make.top.equalTo(self.containerView).offset(18);
        make.right.equalTo(self.deviceSwitch.mas_left).offset(-12);
        make.height.mas_equalTo(23);
    }];
    
    [self.deviceTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.roomNameLabel);
        make.bottom.equalTo(self.containerView).offset(-18);
        make.height.mas_equalTo(17);
    }];
    [self.deviceTypeLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(1, 8));
        make.centerY.equalTo(self.deviceTypeLabel);
        make.left.equalTo(self.deviceTypeLabel.mas_right).offset(8);
    }];
    
    [self.pointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(5, 5));
        make.centerY.equalTo(self.deviceTypeLabel);
        make.left.equalTo(self.lineView.mas_right).offset(8);
    }];
    
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.height.equalTo(self.deviceTypeLabel);
        make.left.equalTo(self.pointView.mas_right).offset(5);
        make.right.equalTo(self.roomNameLabel);
    }];
}

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = UIColor.whiteColor;
        _containerView.layer.cornerRadius = 6;
    }
    return _containerView;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init];
    }
    return _iconView;
}

- (UILabel *)roomNameLabel {
    if (!_roomNameLabel) {
        _roomNameLabel = [[UILabel alloc] init];
        _roomNameLabel.font = [UIFont pingFangSCMediumOfSize:16];
        _roomNameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _roomNameLabel;
}

- (UILabel *)deviceTypeLabel {
    if (!_deviceTypeLabel) {
        _deviceTypeLabel = [[UILabel alloc] init];
        _deviceTypeLabel.font = [UIFont pingFangSCRegularOfSize:12];
        _deviceTypeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _deviceTypeLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _lineView;
}

- (UIView *)pointView {
    if (!_pointView) {
        _pointView = [[UIView alloc] init];
        _pointView.layer.cornerRadius = 2.5;
    }
    return _pointView;
}

- (UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.font = [UIFont pingFangSCRegularOfSize:12];
    }
    return _statusLabel;
}

- (UISwitch *)deviceSwitch {
    if (!_deviceSwitch) {
        _deviceSwitch = [[UISwitch alloc] init];
        _deviceSwitch.onTintColor = [UIColor colorWithHexString:@"#14C9C8"];
        _deviceSwitch.tintColor = [UIColor colorWithHexString:@"#D9D9D9"];
        [_deviceSwitch addTarget:self action:@selector(onPowerChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _deviceSwitch;
}

- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [[UIButton alloc] init];
        [_deleteButton setImage:[UIImage imageNamed:@"icon_delete"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (NSDictionary *)codeToNameDic {
    if (!_codeToNameDic) {
        _codeToNameDic = @{@"YXK-Z/86-FG-A": @"icon_fluorine_main_controller",
                           @"YXK-F/86-FG-A": @"icon_fluorine_sub_controller",
                           @"YXK-Z/86-FC-B": @"icon_water_main_controller",
                           @"YXK-F/86-FC-B": @"icon_water_sub_controller",
                           @"YXK-P/DN-JF-8": @"icon_diverter_water_collector"};
    }
    return _codeToNameDic;
}

@end
