//
//  KBFunctionTagListView.m
//

#import "KBFunctionTagListView.h"
#import "KBFunctionTagCell.h"

static NSString * const kKBFunctionTagCellId2 = @"KBFunctionTagCellId2";

@interface KBFunctionTagListView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionViewInternal;
@property (nonatomic, copy) NSArray<NSString *> *items;
@property (nonatomic, strong) NSMutableSet<NSNumber *> *loadingIndexes; // 记录需要展示loading的index
@end

@implementation KBFunctionTagListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionViewInternal = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionViewInternal.backgroundColor = [UIColor clearColor];
        _collectionViewInternal.dataSource = self;
        _collectionViewInternal.delegate = self;
        [_collectionViewInternal registerClass:[KBFunctionTagCell class] forCellWithReuseIdentifier:kKBFunctionTagCellId2];
        [self addSubview:_collectionViewInternal];
        _collectionViewInternal.translatesAutoresizingMaskIntoConstraints = NO;
        [NSLayoutConstraint activateConstraints:@[
            [_collectionViewInternal.topAnchor constraintEqualToAnchor:self.topAnchor],
            [_collectionViewInternal.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
            [_collectionViewInternal.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
            [_collectionViewInternal.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        ]];
        _items = @[];
        _loadingIndexes = [NSMutableSet set];
    }
    return self;
}

- (void)setItems:(NSArray<NSString *> *)items { _items = [items copy]; [self.collectionViewInternal reloadData]; }

- (UICollectionView *)collectionView { return self.collectionViewInternal; }

#pragma mark - UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.items.count; }

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KBFunctionTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kKBFunctionTagCellId2 forIndexPath:indexPath];
    cell.titleLabel.text = (indexPath.item < self.items.count) ? self.items[indexPath.item] : @"";
    BOOL loading = [self.loadingIndexes containsObject:@(indexPath.item)];
    [cell setLoading:loading];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat totalW = collectionView.bounds.size.width; CGFloat space = 10.0; NSInteger columns = 3;
    CGFloat width = floor((totalW - space * (columns - 1)) / columns);
    return CGSizeMake(width, 48);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10.0; }
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 12.0; }

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(tagListView:didSelectIndex:title:)]) {
        NSString *title = (indexPath.item < self.items.count) ? self.items[indexPath.item] : @"";
        [self.delegate tagListView:self didSelectIndex:indexPath.item title:title];
    }
}

- (void)setLoading:(BOOL)loading atIndex:(NSInteger)index {
    NSNumber *key = @(index);
    if (loading) { [self.loadingIndexes addObject:key]; }
    else { [self.loadingIndexes removeObject:key]; }
    NSIndexPath *ip = [NSIndexPath indexPathForItem:index inSection:0];
    if (ip && ip.item < [self.collectionViewInternal numberOfItemsInSection:0]) {
        KBFunctionTagCell *cell = (KBFunctionTagCell *)[self.collectionViewInternal cellForItemAtIndexPath:ip];
        if ([cell isKindOfClass:[KBFunctionTagCell class]]) { [cell setLoading:loading]; }
    }
}

@end
