//
//  KBMyListCell.m
//  keyBoard
//
//  Created by Mac on 2025/11/10.
//

#import "KBMyListCell.h"

@implementation KBMyListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

- (void)setItemInfoDic:(NSDictionary *)itemInfoDic{
    _itemInfoDic = itemInfoDic;
    self.titleLabel.text = itemInfoDic[@"title"];
    NSString *icon = itemInfoDic[@"icon"];
    self.iconView.image = [UIImage imageNamed:icon];
}

- (void)setupUI {
    [super setupUI];
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.container];
    [self.container addSubview:self.iconBg];
    [self.iconBg addSubview:self.iconView];
    [self.container addSubview:self.titleLabel];
    [self.container addSubview:self.arrowView];
    [self.container addSubview:self.bottomLine];

    // 使用自定义 UIImageView 作为右箭头，替代系统的 accessoryType

    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.top.bottom.equalTo(self.contentView);
    }];

    [self.iconBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.container).offset(16);
        make.centerY.equalTo(self.container);
        make.width.height.mas_equalTo(28);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.iconBg);
        make.width.height.mas_equalTo(24);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconBg.mas_right).offset(12);
        make.centerY.equalTo(self.container);
        // 标题与右箭头保持间距，避免文本被遮挡
        make.right.lessThanOrEqualTo(self.arrowView.mas_left).offset(-12);
    }];
    [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.container);
        make.right.equalTo(self.container).offset(-16);
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(14);
    }];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.container).offset(-12);
        make.bottom.equalTo(self.container);
        make.height.mas_equalTo(KB_ONE_PIXEL);
    }];
}

- (UIView *)container {
    if (!_container) {
        _container = [UIView new];
        _container.backgroundColor = [UIColor whiteColor];
        _container.layer.masksToBounds = YES; // 需要裁剪圆角区域
    }
    return _container;
}

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

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

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithHex:0x222222];
        _titleLabel.text = @"Title";
    }
    return _titleLabel;
}

- (UIImageView *)arrowView {
    if (!_arrowView) {
        _arrowView = [[UIImageView alloc] init];
        _arrowView.contentMode = UIViewContentModeScaleAspectFit;
        _arrowView.image = [UIImage imageNamed:@"black_right_arrow"];
    }
    return _arrowView;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [UIView new];
        _bottomLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    }
    return _bottomLine;
}

- (void)setLineHidden:(BOOL)hidden { self.bottomLine.hidden = hidden; }

- (void)applyCornersWithFirst:(BOOL)isFirst last:(BOOL)isLast {
        self.container.layer.cornerRadius = 8;
        if (@available(iOS 13.0, *)) { self.container.layer.cornerCurve = kCACornerCurveContinuous; }
        CACornerMask mask = 0;
        if (isFirst && isLast) {
            mask = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
        } else if (isFirst) {
            mask = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
        } else if (isLast) {
            mask = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
        } else {
            mask = 0;
        }
        self.container.layer.maskedCorners = mask;
}

@end
