//
//  HeadNewsViewController.m
//  Open
//
//  Created by 雷俊博 on 2021/7/31.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "HeadNewsViewController.h"
#import "XLPageViewController.h"
#import "HeadTVViewController.h"
#import "HeadNewsTVViewController.h"
#import "NewsListController.h"

@interface HeadNewsViewController ()<XLPageViewControllerDelegate, XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@end

@implementation HeadNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initPageViewController];
    
}
- (void)initPageViewController {
    XLPageViewControllerConfig *config = [[XLPageViewControllerConfig alloc] init];
    config.titleSelectedColor = K_MainColor;
    config.titleNormalColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1];
    config.shadowLineHidden = true;
    config.titleSpace = 20;
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:17];
    config.titleNormalFont = [UIFont systemFontOfSize:17];
    config.titleColorTransition = false;
    config.titleViewHeight = 40;
    config.separatorLineHeight = 0.5;
    config.separatorLineColor = [UIColor colorWithHex:0xcccccc];
    
    config.titleViewInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    self.pageViewController.view.frame = self.view.bounds;
   
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController reloadData];
}

#pragma mark - XLPageViewControllerDelegate
/**
 当页面切换完成时回调该方法，返回切换到的位置

 @param pageViewController 实例
 @param index 切换的位置
 */
- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index
{
    
}

#pragma mark - XLPageViewControllerDataSrouce
/**
 根据index返回对应的ViewController

 @param pageViewController XLPageViewController实例
 @param index 当前位置
 @return 返回要展示的ViewController
 */
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index
{
   
    
    NSArray*types=@[@"hot",@"radio_hot",@"",@"care",@"china",@"local",@"world"];
    if (index == 2) {
        HeadNewsTVViewController *tvVC = [HeadNewsTVViewController new];
        
        return tvVC;
    }
    NSString *type = types[index];
    NewsListController *vc = [[NewsListController alloc]initWithType:type];
    vc.from = type;
    CGFloat red = (CGFloat)(arc4random() %256);
    CGFloat green = (CGFloat)(arc4random() %256);
    CGFloat blue = (CGFloat)(arc4random() %256);
    
    vc.view.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
    
    
    return vc;
}

/**
 根据index返回对应的标题

 @param pageViewController XLPageViewController实例
 @param index 当前位置
 @return 返回要展示的标题
 */
- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index
{
    NSArray *titleArray = @[@"热点",@"听新闻",@"推荐",@"养老",@"本地",@"国内",@"国际"];
    return titleArray[index];
}

/**
 要展示分页数

 @return 返回分页数
 */
- (NSInteger)pageViewControllerNumberOfPage
{
    return 7;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
