//
//  KBSkinTagsContainerCell.m
//  keyBoard
//
//  Created by Mac on 2025/11/8.
//

#import "KBSkinTagsContainerCell.h"
#import "KBSkinDetailTagCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
static NSString * const kInnerTagCellId = @"kInnerTagCellId";

@implementation KBSkinTagsContainerCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.tagsView];
        [self.tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)configWithTags:(NSArray<NSString *> *)tags {
    self.tags = tags;
    [self.tagsView reloadData];
}

#pragma mark - UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.tags.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KBSkinDetailTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kInnerTagCellId forIndexPath:indexPath];
    [cell config:self.tags[indexPath.item]];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 根据文案自适应宽度，高度固定 32
    return [KBSkinDetailTagCell sizeForText:self.tags[indexPath.item]];
}

#pragma mark - Height Helper

+ (CGFloat)heightForTags:(NSArray<NSString *> *)tags width:(CGFloat)width {
    if (tags.count == 0) { return 0; }
    // 计算在给定宽度下的自动换行高度（与内部布局保持一致：行间距 8，item 间距 8，sectionInsets = {0,16,0,16}）
    CGFloat leftRight = 16 * 2; // 外部 VC 会给整体 sectionInset=16，这里额外留一点安全边距
    CGFloat maxWidth = width - leftRight;
    CGFloat x = 0;
    CGFloat rows = 1;
    for (NSString *t in tags) {
        CGSize s = [KBSkinDetailTagCell sizeForText:t];
        CGFloat iw = ceil(s.width);
        if (x == 0) {
            x = iw;
        } else {
            // item 间距 8
            if (x + 8 + iw > maxWidth) { // 换行
                rows += 1;
                x = iw;
            } else {
                x += 8 + iw;
            }
        }
    }
    // 行高固定 32；行间距 8；上下额外增加 8 视觉留白
    return rows * 32 + (rows - 1) * 8 + 16;
}

#pragma mark - Lazy
- (UICollectionView *)tagsView {
    if (!_tagsView) {
        UICollectionViewLeftAlignedLayout *layout = [UICollectionViewLeftAlignedLayout new];
        layout.minimumInteritemSpacing = 8;
        layout.minimumLineSpacing = 8;
        layout.sectionInset = UIEdgeInsetsMake(8, 0, 8, 16);

        _tagsView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _tagsView.backgroundColor = [UIColor whiteColor];
        _tagsView.dataSource = self;
        _tagsView.delegate = self;
        [_tagsView registerClass:KBSkinDetailTagCell.class forCellWithReuseIdentifier:kInnerTagCellId];
        _tagsView.scrollEnabled = NO; // 由外层滚动
    }
    return _tagsView;
}
@end
