//
//  HomeViewController.m
//  
//
//  Created by sunchunlei on 16/1/23.
//
//

#import "HTNewsViewController.h"
#import "HTItem.h"
#import "HTTitleButton.h"
#import "HTTitleLabel.h"

#import "UIView+Extension.h"

#import "HTHeadLineViewController.h"
#import "HTInternationalViewController.h"


@interface HTNewsViewController ()<UIScrollViewDelegate>
/**
 *  顶部scrollView
 */
@property (nonatomic, weak) UIScrollView *titleScrollView;
/**
 *  底部scrollView
 */
@property (nonatomic, weak) UIScrollView *contentScorllView;


@property (nonatomic, strong) NSMutableArray *itemArray;

@property(nonatomic,strong) HTTitleLabel *selectedLabel;

@end

@implementation HTNewsViewController

- (NSMutableArray *)itemArray{
    if (_itemArray == nil) {
        
        _itemArray = [NSMutableArray array];
        
        HTItem *item0 = [[HTItem alloc] initWithTitle:@"头条" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item0];
        
        HTItem *item1 = [[HTItem alloc] initWithTitle:@"北京" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item1];
        
        
        HTItem *item2 = [[HTItem alloc] initWithTitle:@"国际" classVc:[HTInternationalViewController class]];
        [_itemArray addObject:item2];
        
        HTItem *item3 = [[HTItem alloc] initWithTitle:@"英语" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item3];
        
        HTItem *item4 = [[HTItem alloc] initWithTitle:@"互联网" classVc:[HTInternationalViewController class]];
        [_itemArray addObject:item4];
        
        HTItem *item5 = [[HTItem alloc] initWithTitle:@"科技" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item5];
        
        HTItem *item6 = [[HTItem alloc] initWithTitle:@"娱乐" classVc:[HTInternationalViewController class]];
        [_itemArray addObject:item6];
        
        HTItem *item7 = [[HTItem alloc] initWithTitle:@"八卦" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item7];
        
        HTItem *item8 = [[HTItem alloc] initWithTitle:@"政治" classVc:[HTInternationalViewController class]];
        [_itemArray addObject:item8];
        
        HTItem *item9 = [[HTItem alloc] initWithTitle:@"汽车" classVc:[HTHeadLineViewController class]];
        [_itemArray addObject:item9];
    
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.tag = 4000;
    
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
    
#define kNavigationBarY 64
#define kTabBarH 49
#define kTitleViewH 30

    // 添加穿透效果
    UIScrollView *contentScorllView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, kScreenW,kScreenH)];
    [self.view addSubview:contentScorllView];
    self.contentScorllView = contentScorllView;
    self.contentScorllView.pagingEnabled = YES;
    self.contentScorllView.delegate = self;
    self.contentScorllView.backgroundColor = [UIColor yellowColor];
    
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64,kScreenW,kTitleViewH)];
    [self.view addSubview:titleScrollView];
    titleScrollView.showsHorizontalScrollIndicator = NO;
    titleScrollView.showsVerticalScrollIndicator = NO;
    titleScrollView.backgroundColor = [UIColor whiteColor];
    titleScrollView.alpha = 1.0;
    self.titleScrollView = titleScrollView;
    
    // 设置标签和子控制器
    [self setupLablesAndChildVc];
}

- (void)setupLablesAndChildVc{

    // 不要可以调整scrollView的contentInset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 添加标题
    CGFloat titleW = 60;
    NSInteger count = self.itemArray.count;
    for (int i = 0; i < count; i++) {
        // 添加标签
        HTItem *item = (HTItem *)self.itemArray[i];
        HTTitleLabel *titleLabel = [[HTTitleLabel alloc] init];
        
        titleLabel.width = titleW;
        titleLabel.height = self.titleScrollView.height;
        titleLabel.x = i * titleLabel.width;
        titleLabel.y = 2;
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelDidClick:)];
        [titleLabel addGestureRecognizer:tap];
        titleLabel.userInteractionEnabled = YES;
        titleLabel.text = item.title;
        [self.titleScrollView addSubview:titleLabel];
        // 添加子控制器
        [self addChildViewController:item.vc];
        
        if (i == 0) {
            [self titleLabelDidClick:titleLabel.gestureRecognizers[0]];
        }
    }
    // 设置contentSize
    self.titleScrollView.contentSize = CGSizeMake(titleW * count,0);
    
    self.contentScorllView.contentSize = CGSizeMake(count * self.view.width, 0);
}

#pragma mark - privateMethod

/**
    点击title时 title单独做缩放动画(而不是通过contentScroll的滚动做动画) title缩放动画结束后 titlescrollView做动画 其结束后contentScrollView 显示需要显示的view(没有加动画,如果使用动画这套代码需要改动--时间研究一下为什么)
    到拖动contentView的content时 title做联动动画(拖动结束后会调用)
    
    总结:点击动画 和 联动动画是分开的
 
 */

- (void)titleLabelDidClick:(UIGestureRecognizer *)gesture{
    
    HTTitleLabel *titleLabel = (HTTitleLabel *)gesture.view;

    // 处理缩放问题
    self.selectedLabel.selected = NO;
    titleLabel.selected = YES;
    self.selectedLabel = titleLabel;
    
    // 1.1标签居中处理 - contentOffset为 标签的center.x - titleScrollView.center.x
    // title的frame是固定不变的 如果需要title居中需要设置scrollview的center比较
    CGFloat offsetX = titleLabel.center.x - self.titleScrollView.center.x;
    CGFloat offMaxX = self.titleScrollView.contentSize.width - self.titleScrollView.width;
    if (offsetX < 0) {
        offsetX = 0;
    } else if(offsetX > offMaxX){
        offsetX = offMaxX;
    }
    
    // title滚动
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    
    // 2.切换子控制器
    // 添加view
    NSInteger index = [self.titleScrollView.subviews indexOfObject:titleLabel];
    UIViewController *childVc = self.childViewControllers[index];
    
    if (childVc.view.superview == nil) { // 防止重复添加
        childVc.view.width = self.contentScorllView.width;
        childVc.view.height = self.contentScorllView.height;
        childVc.view.y = 0;
        childVc.view.x = self.view.width * index;
        [self.contentScorllView addSubview:childVc.view];
    }

    // 显示view
    CGFloat viewOffsetX = index * self.contentScorllView.width;
    CGPoint viewOffset =  CGPointMake(viewOffsetX, 0);
    
    // content滚动
    // 这里设置animated设置no scrollViewDidScroll方法之调用一次 yes会多次调用
    // 这里如果设置animted = yes 当点击title时title的缩放会做两次
    [self.contentScorllView setContentOffset:viewOffset animated:NO];
    
}

#pragma mark - <UIScorllViewDelegate>

// 此方法在scrollViewDidScroll调用结束后调用 如果scrollViewDidScroll不使用动画 则不对调用此方法
// 即:当scrollVeiw有加速度 其加速度结束变化时才会调用-- 在此项目中只有用户拖拽view停止时才会调用 单独点击title时是不会调动的(因为点击title时contentscrollView的滚动没有设置动画 没有动画就没有加速度也就不会调用此方法)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    // 停止减速时获取offset
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    HTTitleLabel *titleLabel = self.titleScrollView.subviews[index];
    ;
    [self titleLabelDidClick:titleLabel.gestureRecognizers[0]];
    
    
//    NSLog(@"scrollViewDidEndDecelerating");
}

// 标签关联动画
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//    NSLog(@"scrollViewDidScroll");

    // 获取需要动画的标签
    CGFloat value = scrollView.contentOffset.x / scrollView.width;
    NSInteger leftIndex = (NSInteger)value ; // 四舍五入
    NSInteger rightIndex = leftIndex + 1;
//    NSLog(@"%f--%zd--%zd",value,leftIndex,rightIndex);
    
    // 计算title缩放比例
    CGFloat rightScale = value - leftIndex;
    CGFloat leftScale = 1 - rightScale;
    HTTitleLabel *leftBtn = self.titleScrollView.subviews[leftIndex];
    leftBtn.scale = leftScale;
    
    // 防止数组越界
//    NSLog(@"leftScale%f---rightScale%f,",leftScale,rightScale);
    if (rightIndex < self.titleScrollView.subviews.count) {
        HTTitleLabel *rightBtn = self.titleScrollView.subviews[rightIndex];
        rightBtn.scale = rightScale;
    }
}


@end
