//
//  NestViewController.m
//  JXCategoryView
//
//  Created by jiaxin on 2018/8/20.
//  Copyright © 2018年 jiaxin. All rights reserved.
//

#import "SLNestViewController.h"
#import "JXCategoryTitleView.h"
#import "SLNestSubjectViewController.h"

@interface SLNestViewController ()

@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;

@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation SLNestViewController

- (void)viewDidLoad {
    self.title = @"真题写作";
    self.titles = @[@"大作文", @"小作文", @"作文", ];

    [super viewDidLoad];
    self.myCategoryView.titles = self.titles;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc ]init];
    lineView.indicatorColor = SLBlueColor;
    lineView.indicatorWidth = 40;
    self.myCategoryView.indicators = @[lineView];
    self.myCategoryView.titleColor = SL333Color;
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:17];
    self.myCategoryView.titleSelectedColor = SLBlueColor;
    self.myCategoryView.cellWidth = 85;
    self.myCategoryView.cellSpacing = 0;
    self.myCategoryView.averageCellSpacingEnabled = NO;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.myCategoryView.frame = CGRectMake(0, kNavBarHeight, self.view.bounds.size.width, [self preferredCategoryViewHeight]);
    self.listContainerView.frame = CGRectMake(0, [self preferredCategoryViewHeight] + kNavBarHeight, self.view.bounds.size.width, self.view.bounds.size.height);
}

- (JXCategoryTitleView *)myCategoryView {
    return (JXCategoryTitleView *)self.categoryView;
}

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

- (CGFloat)preferredCategoryViewHeight {
    return 50;
}


#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    SLNestSubjectViewController *list = [[SLNestSubjectViewController alloc] init];
    if (index == 0) {
        list.titles = @[@"全部", @"科技", @"社会", @"媒体", @"政治",@"全部", @"科技", @"社会", @"媒体", @"政治",@"全部", @"科技", @"社会", @"媒体", @"政治",@"全部", @"科技", @"社会", @"媒体", @"政治",@"全部", @"科技", @"社会", @"媒体", @"政治"];
    }else if(index == 1) {
        list.titles = @[@"全部", @"科技", @"社会", @"媒体", @"政治"];
    }else if (index == 2) {
        list.titles = @[@"全部", @"科技", @"社会", @"媒体", @"政治"];
    }
    return list;
}

#pragma mark - JXCategoryListContainerViewDelegate

- (void)listContainerViewDidScroll:(UIScrollView *)scrollView{
    if ([self isKindOfClass:[SLNestViewController class]]) {
        CGFloat index = scrollView.contentOffset.x/scrollView.bounds.size.width;
        CGFloat absIndex = fabs(index - self.currentIndex);
        if (absIndex >= 1) {
            //”快速滑动的时候，只响应最外层VC持有的scrollView“，说实话，完全可以不用处理这种情况。如果你们的产品经理坚持认为这是个问题，就把这块代码加上吧。
            //嵌套使用的时候，最外层的VC持有的scrollView在翻页之后，就断掉一次手势。解决快速滑动的时候，只响应最外层VC持有的scrollView。子VC持有的scrollView却没有响应
            self.listContainerView.scrollView.panGestureRecognizer.enabled = NO;
            self.listContainerView.scrollView.panGestureRecognizer.enabled = YES;
            _currentIndex = floor(index);
        }
    }
}

@end
