//
//  HomeRankVC.m
//  keyBoard
//
//  Created by Mac on 2025/11/6.
//

#import "HomeRankVC.h"
#import "HomeRankContentVC.h"
#import "KBCategoryTitleImageView.h"

@interface HomeRankVC ()<JXCategoryViewDelegate>
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) JXCategoryTitleImageView *myCategoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, assign) JXCategoryTitleImageType currentType;

@end

@implementation HomeRankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    self.titles = @[@"螃蟹啊斯柯达积分卡", @"小龙虾", @"苹果", @"胡萝卜", @"葡萄", @"西瓜"];
    [self.view addSubview:self.myCategoryView];
    [self.view addSubview:self.listContainerView];
    self.listContainerView.scrollView.scrollEnabled = false;
    NSArray *imageNames = @[@"home_item_normal", @"home_item_normal", @"home_item_normal", @"home_item_normal", @"home_item_normal", @"home_item_normal"];
    NSArray *selectedImageNames = @[@"home_item_selected", @"home_item_selected", @"home_item_selected", @"home_item_selected", @"home_item_selected", @"home_item_selected"];

    self.myCategoryView.titles = self.titles;
//    self.myCategoryView.titleColorGradientEnabled = YES;
    self.myCategoryView.imageInfoArray = imageNames;
    self.myCategoryView.cellBackgroundSelectedColor = [UIColor blackColor];
    self.myCategoryView.cellBackgroundUnselectedColor = [UIColor colorWithHex:0xEAF8F4];
    self.myCategoryView.titleColor = [UIColor colorWithHex:0x1B1F1A];
    self.myCategoryView.titleSelectedColor = [UIColor whiteColor];
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:12];
    // 关闭各种“放大”效果，保持选中与未选中宽度一致
    self.myCategoryView.titleLabelZoomEnabled = NO;      // 关闭文字缩放
    self.myCategoryView.titleLabelZoomScale = 1.0;
    self.myCategoryView.cellWidthZoomEnabled = NO;       // 关闭cell宽度缩放
    self.myCategoryView.cellWidthZoomScale = 1.0;

    self.myCategoryView.selectedImageInfoArray = selectedImageNames;
    self.myCategoryView.loadImageBlock = ^(UIImageView *imageView, id info) {
        NSString *imageName = info;
        imageView.image = [UIImage imageNamed:imageName];
    };
    self.myCategoryView.imageZoomEnabled = false;
    self.myCategoryView.imageZoomScale = 1.3;
    self.myCategoryView.averageCellSpacingEnabled = NO;

    self.myCategoryView.cellBackgroundColorGradientEnabled = true;
    self.myCategoryView.cellWidthIncrement = 20;
//    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
//    lineView.indicatorWidth = 20;
//    self.myCategoryView.indicators = @[lineView];
//    JXCategoryIndicatorLineView *indicatorlineView = [[JXCategoryIndicatorLineView alloc]init];
//    indicatorlineView.indicatorHeight = 30;
//    indicatorlineView.indicatorColor = UIColor.blackColor;
//    indicatorlineView.verticalMargin = 10;
//    indicatorlineView.indicatorWidthIncrement = 20;
//    indicatorlineView.indicatorCornerRadius = 4;
//    UIImage *bgImage = [UIImage kb_imageWithColor:[UIColor blackColor] size:CGSizeMake(30, 30)];
////    UIImage *bgImage = [UIImage imageNamed:@"get_ppd_btn"];
//    UIImageView *imageV = [[UIImageView alloc]initWithImage:bgImage];
//    imageV.frame  = indicatorlineView.bounds;
//    imageV.contentMode = UIViewContentModeScaleToFill;
//    [indicatorlineView addSubview:imageV];
//    self.myCategoryView.indicators = @[indicatorlineView];
//    [self configCategoryViewWithType:JXCategoryTitleImageType_LeftImage];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.myCategoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, [self preferredCategoryViewHeight]);
    self.listContainerView.frame = CGRectMake(0, [self preferredCategoryViewHeight], self.view.bounds.size.width, self.view.bounds.size.height - [self preferredCategoryViewHeight]);
}

//- (JXCategoryBaseView *)preferredCategoryView {
//    return [[JXCategoryBaseView alloc] init];
//}

- (CGFloat)preferredCategoryViewHeight {
    return 30;
}

- (void)configCategoryViewWithType:(JXCategoryTitleImageType)imageType {
    self.currentType = imageType;
    if ((NSInteger)imageType == 100) {
        NSMutableArray *types = [NSMutableArray array];
        for (int i = 0; i < self.titles.count; i++) {
            if (i == 2) {
                [types addObject:@(JXCategoryTitleImageType_OnlyImage)];
            }else if (i == 4) {
                [types addObject:@(JXCategoryTitleImageType_LeftImage)];
            }else {
                [types addObject:@(JXCategoryTitleImageType_OnlyTitle)];
            }
        }
        self.myCategoryView.imageTypes = types;
    }else {
        NSMutableArray *types = [NSMutableArray array];
        for (int i = 0; i < self.titles.count; i++) {
            [types addObject:@(imageType)];
        }
        self.myCategoryView.imageTypes = types;
    }
    [self.myCategoryView reloadData];
}

// 分页菜单视图
- (JXCategoryTitleImageView *)myCategoryView {
    if (!_myCategoryView) {
        // Use our subclass so cells get a 4pt rounded background locally
        _myCategoryView = (JXCategoryTitleImageView *)[[KBCategoryTitleImageView alloc] init];
        _myCategoryView.delegate = self;

        // !!!: 将列表容器视图关联到 categoryView
        _myCategoryView.listContainer = self.listContainerView;
    }
    return _myCategoryView;
}

// 列表容器视图
- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 拿到当前显示的列表 VC（已创建/显示过的会记录在 validListDict 中）
    id<JXCategoryListContentViewDelegate> list = self.listContainerView.validListDict[@(index)];
    if ([list isKindOfClass:[HomeRankContentVC class]]) {
        HomeRankContentVC *currentVC = (HomeRankContentVC *)list;
        // 如需拿到对应的 collectionView，可以在这里同步一下
        self.collectionView = currentVC.collectionView;
        // TODO: 在这里对 currentVC 做需要的操作
    }
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    HomeRankContentVC *list = [[HomeRankContentVC alloc] init];
    self.collectionView = list.collectionView;
    return list;
}


#pragma mark - TitleImageSettingViewControllerDelegate

- (void)titleImageSettingVCDidSelectedImageType:(JXCategoryTitleImageType)imageType {
    [self configCategoryViewWithType:imageType];
}

@end
