//
//  HomeHotCell.m
//  keyBoard
//

#import "HomeHotCell.h"

@interface HomeHotCell()
// 左侧序号
@property (nonatomic, strong) UILabel *rankLabel;
@property (nonatomic, strong) UIView *yuanView;
// 左侧圆形头像占位
@property (nonatomic, strong) UIView *avatarView;
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 副标题
@property (nonatomic, strong) UILabel *subLabel;
// 右侧加入按钮（或已加入勾选）
@property (nonatomic, strong) UIButton *actionButton;
@end

@implementation HomeHotCell

- (void)setupUI {
    [super setupUI];

    // 添加控件
    [self.contentView addSubview:self.yuanView];
    [self.contentView addSubview:self.rankLabel];
    [self.contentView addSubview:self.avatarView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subLabel];
    [self.contentView addSubview:self.actionButton];

    // 布局（使用 Masonry）
    [self.rankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.centerY.equalTo(self.contentView);
    }];
    [self.yuanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rankLabel).offset(2);
        make.bottom.equalTo(self.rankLabel).offset(-3);
        make.width.height.mas_equalTo(6);
    }];

    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rankLabel.mas_right).offset(12);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(56);
    }];

    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-16);
        make.width.mas_equalTo(56);
        make.height.mas_equalTo(38);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatarView.mas_right).offset(12);
        make.top.equalTo(self.avatarView.mas_top).offset(-2);
        make.right.lessThanOrEqualTo(self.actionButton.mas_left).offset(-12);
    }];

    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.avatarView.mas_bottom).offset(2);
    }];
}

- (void)configWithRank:(NSInteger)rank title:(NSString *)title subtitle:(NSString *)sub joined:(BOOL)joined {
    self.rankLabel.text = [NSString stringWithFormat:@"%ld", (long)rank];
    self.titleLabel.text = title ?: @"";
    self.subLabel.text = sub ?: @"";

    if (joined) {
        // 已加入状态：灰背景、打勾
        [self.actionButton setTitle:@"✓" forState:UIControlStateNormal];
        [self.actionButton setTitleColor:[UIColor colorWithWhite:0.45 alpha:1] forState:UIControlStateNormal];
        self.actionButton.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
    } else {
        // 可加入状态：绿色加号
        [self.actionButton setTitle:@"+" forState:UIControlStateNormal];
        [self.actionButton setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
        self.actionButton.backgroundColor = [UIColor colorWithRed:0.88 green:0.97 blue:0.93 alpha:1.0];
    }
}

#pragma mark - Lazy

- (UILabel *)rankLabel {
    if (!_rankLabel) {
        _rankLabel = [[UILabel alloc] init];
        _rankLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
        _rankLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
    }
    return _rankLabel;
}

- (UIView *)avatarView {
    if (!_avatarView) {
        _avatarView = [[UIView alloc] init];
        _avatarView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        _avatarView.layer.cornerRadius = 28;
        _avatarView.layer.masksToBounds = YES;
    }
    return _avatarView;
}

- (UIView *)yuanView {
    if (!_yuanView) {
        _yuanView = [[UIView alloc] init];
        _yuanView.backgroundColor = [UIColor colorWithHex:0x26D3AA];
        _yuanView.layer.cornerRadius = 3;
        _yuanView.layer.masksToBounds = YES;
    }
    return _yuanView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        _titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
    }
    return _titleLabel;
}

- (UILabel *)subLabel {
    if (!_subLabel) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.font = [UIFont systemFontOfSize:12];
        _subLabel.textColor = [UIColor colorWithHex:0x9A9A9A];
        _subLabel.numberOfLines = 2;
    }
    return _subLabel;
}

- (UIButton *)actionButton {
    if (!_actionButton) {
        _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionButton.layer.cornerRadius = 12;
        _actionButton.layer.masksToBounds = YES;
        _actionButton.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    }
    return _actionButton;
}

@end

