//
//  HomeRankCardCell.m
//  keyBoard
//
//  Created by Codex on 2025/11/06.
//

@import UIKit;
#import "HomeRankCardCell.h"
#import "UIView+KBShadow.h"

@interface HomeRankCardCell ()
@property (nonatomic, strong) UIView *shadowView;     // 阴影容器（不裁剪）
@property (nonatomic, strong) UIView *cardView;       // 白色圆角卡片
@property (nonatomic, strong) UIView *badgeCircle;    // 顶部圆形描边
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UILabel *peopleLabel;
@property (nonatomic, strong) UIButton *actionBtn;    // 加号/勾选按钮
@property (nonatomic, assign) BOOL added;
@end

@implementation HomeRankCardCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = UIColor.clearColor;
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    // 使用懒加载属性，保证首次访问时创建
    [self.contentView addSubview:self.shadowView];
    [self.shadowView addSubview:self.cardView];
    [self.contentView addSubview:self.badgeCircle];
    [self.cardView addSubview:self.titleLabel];
    [self.cardView addSubview:self.descLabel];
    [self.cardView addSubview:self.peopleLabel];
    [self.cardView addSubview:self.actionBtn];

    // 统一阴影封装：在阴影容器上添加卡片阴影，阴影路径根据卡片左右 8pt 内边距进行收缩
    [self.shadowView kb_setCardShadowWithCornerRadius:18.0 insets:UIEdgeInsetsMake(0, 8, 0, 8)];
}

- (void)setupConstraints {
    // Masonry 约束
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.top.equalTo(self.contentView.mas_top).offset(68 * 0.5); // 给圆环留空间
    }];

    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.shadowView).insets(UIEdgeInsetsMake(0, 8, 0, 8));
    }];

    [self.badgeCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.cardView);
        make.centerY.equalTo(self.shadowView.mas_top);
        make.width.height.mas_equalTo(68);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.badgeCircle.mas_bottom).offset(13);
        make.left.equalTo(self.cardView.mas_left).offset(18);
        make.right.equalTo(self.cardView.mas_right).offset(-18);
        make.height.mas_equalTo(23);
    }];

    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(12);
        make.left.equalTo(self.cardView.mas_left).offset(18);
        make.right.equalTo(self.cardView.mas_right).offset(-18);
    }];

    [self.peopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.descLabel.mas_bottom).offset(8);
        make.left.equalTo(self.cardView.mas_left).offset(18);
        make.right.equalTo(self.cardView.mas_right).offset(-18);
    }];

    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cardView.mas_left).offset(18);
        make.right.equalTo(self.cardView.mas_right).offset(-18);
        make.bottom.equalTo(self.cardView.mas_bottom).offset(-12);
        make.height.mas_equalTo(32);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 阴影路径需在尺寸确定后更新（封装方法会基于记录参数自动计算）
    [self.shadowView kb_updateShadowPath];

    _badgeCircle.layer.cornerRadius = 34;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.onTapAction = nil;
}

- (void)tapAction {
    if (self.onTapAction) self.onTapAction();
}

- (void)configureWithTitle:(NSString *)title desc:(NSString *)desc people:(NSString *)people added:(BOOL)added {
    self.added = added;
    self.titleLabel.text = title ?: @"";
    self.descLabel.text = desc ?: @"";
    self.peopleLabel.text = people ?: @"";

    if (added) {
        // 已添加：灰底、对勾
        self.actionBtn.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1.0];
        [self.actionBtn setTitle:@"✓" forState:UIControlStateNormal];
        [self.actionBtn setTitleColor:[UIColor colorWithWhite:0.55 alpha:1.0] forState:UIControlStateNormal];
    } else {
        // 未添加：黑底、加号
        self.actionBtn.backgroundColor = [UIColor blackColor];
        [self.actionBtn setTitle:@"+" forState:UIControlStateNormal];
        [self.actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

#pragma mark - Lazy UI

- (UIView *)shadowView {
    if (!_shadowView) {
        _shadowView = [UIView new];
        _shadowView.backgroundColor = UIColor.clearColor;
    }
    return _shadowView;
}

- (UIView *)cardView {
    if (!_cardView) {
        _cardView = [UIView new];
        _cardView.backgroundColor = UIColor.whiteColor;
        _cardView.layer.cornerRadius = 18.0;
        _cardView.layer.masksToBounds = YES;
    }
    return _cardView;
}

- (UIView *)badgeCircle {
    if (!_badgeCircle) {
        _badgeCircle = [UIView new];
        _badgeCircle.backgroundColor = UIColor.whiteColor;
        _badgeCircle.layer.borderColor = [UIColor colorWithRed:0.78 green:0.90 blue:0.20 alpha:1.0].CGColor;
        _badgeCircle.layer.borderWidth = 3.0;
    }
    return _badgeCircle;
}

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

- (UILabel *)descLabel {
    if (!_descLabel) {
        _descLabel = [UILabel new];
        _descLabel.textColor = [UIColor colorWithHex:0x9A9A9A];
        _descLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _descLabel.textAlignment = NSTextAlignmentCenter;
        _descLabel.numberOfLines = 2;
    }
    return _descLabel;
}

- (UILabel *)peopleLabel {
    if (!_peopleLabel) {
        _peopleLabel = [UILabel new];
        _peopleLabel.textColor = [UIColor colorWithHex:KBColorValue];
        _peopleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        _peopleLabel.textAlignment = NSTextAlignmentCenter;
        _peopleLabel.numberOfLines = 2;
    }
    return _peopleLabel;
}

- (UIButton *)actionBtn {
    if (!_actionBtn) {
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionBtn.layer.cornerRadius = 4.0;
        _actionBtn.layer.masksToBounds = YES;
        [_actionBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _actionBtn.titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
        [_actionBtn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}

@end
