//
//  KBFunctionTagCell.m
//  CustomKeyboard
//
//  Created by Codex on 2025/10/28.
//

#import "KBFunctionTagCell.h"
#import "Masonry.h"

@interface KBFunctionTagCell ()
@property (nonatomic, strong) UILabel *titleLabelInternal;
@property (nonatomic, strong) UIImageView *iconViewInternal;
@property (nonatomic, strong) UIActivityIndicatorView *loadingView;
@end

@implementation KBFunctionTagCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
        self.contentView.layer.cornerRadius = 12;
        self.contentView.layer.masksToBounds = YES;

        [self.contentView addSubview:self.iconViewInternal];
        [self.contentView addSubview:self.titleLabelInternal];

        [self.iconViewInternal mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(10);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.width.height.mas_equalTo(24);
        }];
        [self.titleLabelInternal mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconViewInternal.mas_right).offset(6);
            make.right.equalTo(self.contentView.mas_right).offset(-10);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];

        // 小菊花：默认隐藏，放在标题右侧
        [self.contentView addSubview:self.loadingView];
        [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView.mas_right).offset(-10);
            make.width.height.mas_equalTo(16);
        }];
    }
    return self;
}

#pragma mark - Lazy

- (UIImageView *)iconViewInternal {
    if (!_iconViewInternal) {
        _iconViewInternal = [[UIImageView alloc] init];
        UILabel *emoji = [[UILabel alloc] init];
        emoji.text = @"🙂"; // 占位图标
        emoji.textAlignment = NSTextAlignmentCenter;
        emoji.font = [UIFont systemFontOfSize:20];
        [_iconViewInternal addSubview:emoji];
        [emoji mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_iconViewInternal);
        }];
    }
    return _iconViewInternal;
}

- (UILabel *)titleLabelInternal {
    if (!_titleLabelInternal) {
        _titleLabelInternal = [[UILabel alloc] init];
        _titleLabelInternal.font = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
        _titleLabelInternal.textColor = [UIColor blackColor];
    }
    return _titleLabelInternal;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
static UIActivityIndicatorViewStyle KBSpinnerStyle(void) { return UIActivityIndicatorViewStyleMedium; }
#else
static UIActivityIndicatorViewStyle KBSpinnerStyle(void) { return UIActivityIndicatorViewStyleGray; }
#endif

- (UIActivityIndicatorView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:KBSpinnerStyle()];
        _loadingView.hidesWhenStopped = YES;
        _loadingView.color = [UIColor grayColor];
        _loadingView.hidden = YES;
    }
    return _loadingView;
}

#pragma mark - Expose

- (UILabel *)titleLabel { return self.titleLabelInternal; }
- (UIImageView *)iconView { return self.iconViewInternal; }

- (void)setLoading:(BOOL)loading {
    if (loading) {
        self.loadingView.hidden = NO;
        [self.loadingView startAnimating];
    } else {
        [self.loadingView stopAnimating];
        self.loadingView.hidden = YES;
    }
}

@end
