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

#import "HeadTVViewController.h"
#import <KSAdSDK/KSCUFeedPage.h>
#import <KSAdSDK/KSCUContentPage.h>
//#import "XLPageViewController.h"
#import "MFPageTitleView.h"
#import "MFPageContentView.h"
@interface HeadTVViewController ()<KSCUVideoStateDelegate,KSCUContentStateDelegate,KSCUFeedPageCallBackProtocol,MFPageTitleViewDelegate,MFPageContentViewDelegate>

//@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong)MFPageTitleView *pageTitleView;

@property (nonatomic, strong)MFPageContentView *pageContentView;

@property (nonatomic, strong) KSCUFeedPage *feedPage;

//@property (nonatomic, strong)NSArray *adIdArray;

@property (nonatomic, assign)NSInteger currentIndex;

@end

@implementation HeadTVViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //[self addTvSubVC];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self initPageViewController];
    
    [self.view addSubview:self.pageTitleView];
    [self.view addSubview:self.pageContentView];
}
#pragma mark - getters and setters
- (MFPageTitleView *)pageTitleView
{
    if (!_pageTitleView) {
        MFPageTitleViewConfig *config = [[MFPageTitleViewConfig alloc]init];
        config.titleColor = K_RGB(43, 43, 43);
        config.titleSelectedColor = K_MainColor;
        config.titleFont = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular];
        config.indicatorColor = K_MainColor;
        config.selectedIndex = self.currentIndex;

        _pageTitleView = [[MFPageTitleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 41) titles:@[@"资讯",@"搞笑",@"三农",@"健康",@"舞蹈",@"美食",@"生活",@"亲子",@"教育",@"旅游",@"运动"] config:config];
        _pageTitleView.delegate = self;
    }
    return _pageTitleView;
}
- (MFPageContentView *)pageContentView
{
    if (!_pageContentView) {
        NSMutableArray <UIViewController *> *childControllers = [[NSMutableArray alloc]init];
        NSArray*types= @[@"6030000238",@"6030000189",@"6030000224",@"6030000217",@"6030000182",@"6030000168",@"6030000203",@"6030000231",@"6030000210",@"6030000196",@"6030000175"];
//        for (int i =0 ; i < types.count; i++) {
//            //KSCUFeedPage *feedPage = [[KSCUFeedPage alloc] initWithPosId:types[i]];
//            KSCUContentPage *feedPage=[[KSCUContentPage alloc] initWithPosId:types[i]];
//            feedPage.videoStateDelegate = self;
//            feedPage.stateDelegate = self;
//            feedPage.callBackDelegate = self;
//            UIViewController* vc = feedPage.viewController;
//
//            [childControllers addObject:vc];
//        }
        KSCUContentPage *feedPage=[[KSCUContentPage alloc] initWithPosId:@"6030000238"];
        feedPage.videoStateDelegate = self;
        feedPage.stateDelegate = self;
        feedPage.callBackDelegate = self;
        UIViewController* vc = feedPage.viewController;
        [childControllers addObject:vc];
        
        //CGFloat pageContentViewY = self.pageTitleView.maxY;//- self.pageTitleView.height - Height_TapBar
        _pageContentView = [[MFPageContentView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight ) parentVC:self childVCs:childControllers];
        _pageContentView.delegate = self;
    }
    return _pageContentView;
}
#pragma mark - MFPageTitleViewDelegate
- (void)selectedIndexInPageTitleView:(MFPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex
{
    [self.pageContentView setPageContentViewCurrentIndex:selectedIndex];
}
#pragma mark - MFPageContentViewDelegate
- (void)pageContentViewScroll:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex
{
    [self.pageTitleView setPageTitleView:progress originalIndex:originalIndex targetIndex:targetIndex];
}
//- (void)addTvSubVC
//{
//    KSCUFeedPage *feedPage = [[KSCUFeedPage alloc] initWithPosId:@"6030000009"];
//    
//    feedPage.videoStateDelegate = self;
//    feedPage.stateDelegate = self;
//    feedPage.callBackDelegate = self;
//    feedPage.hidesBottomBarWhenPushed = YES;
//    
//    self.feedPage = feedPage;
//    UIViewController *vc1 = self.feedPage.feedViewController;
//    
//    [self addChildViewController:vc1];
//    [self.view addSubview:vc1.view];
//    [vc1.view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.bottom.left.offset(0);
//    }];
//}
//
//- (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, 10, 0, 10);
////    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];
////    self.pageViewController.delegate = self;
////    self.pageViewController.dataSource = self;
////    self.pageViewController.selectedIndex = 0;
////    self.pageViewController.view.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.view.bounds));
////
////    [self addChildViewController:self.pageViewController];
////    [self.view addSubview:self.pageViewController.view];
////    [self.pageViewController reloadData];
//
//
//    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
//{
//
//    KSCUFeedPage *feedPage = [[KSCUFeedPage alloc] initWithPosId:self.adIdArray[index]];
//    feedPage.videoStateDelegate = self;
//    feedPage.stateDelegate = self;
//    feedPage.callBackDelegate = self;
//    UIViewController* vc = feedPage.feedViewController;
//    return vc;
//}
//
///**
// 根据index返回对应的标题
//
// @param pageViewController XLPageViewController实例
// @param index 当前位置
// @return 返回要展示的标题
// */
//- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index
//{
//    //NSArray *titleArray = @[@"资讯",@"亲子",@"三农",@"健康",@"教育",@"生活",@"旅游",@"搞笑",@"舞蹈",@"运动",@"美食"];
//    NSArray *titleArray = @[@"资讯",@"搞笑",@"三农",@"健康",@"舞蹈",@"美食",@"生活",@"亲子",@"教育",@"旅游",@"运动"];
//    return titleArray[index];
//}
//
///**
// 要展示分页数
//
// @return 返回分页数
// */
//- (NSInteger)pageViewControllerNumberOfPage
//{
//    return self.adIdArray.count;
//}

#pragma mark - KSCUVideoStateDelegate
/**
 * 视频开始播放
 * @param videoContent 内容模型
 */
- (void)kscu_videoDidStartPlay:(id<KSCUContentInfo>)videoContent
{
    
}
/**
* 视频暂停播放
* @param videoContent 内容模型
*/
- (void)kscu_videoDidPause:(id<KSCUContentInfo>)videoContent
{
    
}
/**
* 视频恢复播放
* @param videoContent 内容模型
*/
- (void)kscu_videoDidResume:(id<KSCUContentInfo>)videoContent
{
    
}
/**
* 视频停止播放
* @param videoContent 内容模型
* @param finished     是否播放完成
*/
- (void)kscu_videoDidEndPlay:(id<KSCUContentInfo>)videoContent isFinished:(BOOL)finished
{
    
}
/**
* 视频播放失败
* @param videoContent 内容模型
* @param error        失败原因
*/
- (void)kscu_videoDidFailedToPlay:(id<KSCUContentInfo>)videoContent withError:(NSError *)error
{
    
}
#pragma mark - KSCUContentStateDelegate
/**
* 内容展示
* @param content 内容模型
*/
- (void)kscu_contentDidFullDisplay:(id<KSCUContentInfo>)content
{
    
}
/**
* 内容隐藏
* @param content 内容模型
*/
- (void)kscu_contentDidEndDisplay:(id<KSCUContentInfo>)content
{
    
}
/**
* 内容暂停显示，ViewController disappear或者Application resign active
* @param content 内容模型
*/
- (void)kscu_contentDidPause:(id<KSCUContentInfo>)content
{
    
}
/**
* 内容恢复显示，ViewController appear或者Application become active
* @param content 内容模型
*/
- (void)kscu_contentDidResume:(id<KSCUContentInfo>)content
{
    
}

#pragma mark - KSCUFeedPageCallBackProtocol
/// 请求开始回调
- (void)kscuContentRequestStart:(KSCUFeedEvent *)event
{
    
}
/// 请求成功回调
- (void)kscuContentRequestSuccess:(KSCUFeedEvent *)event
                         callBack:(KSCUResponseObj *)responseObj
{
    
}
/// 请求开失败回调
- (void)kscuContentRequestFail:(KSCUFeedEvent *)event
{
    
}
//#pragma mark - getters and setters

//- (void)setSelectIndex:(NSInteger)selectIndex
//{
//    self.pageTitleView. = selectIndex;
//}
//- (NSArray *)adIdArray
//{
//    if (!_adIdArray) {
//        //_adIdArray = @[@"6030000238",@"6030000231",@"6030000224",@"6030000217",@"6030000210",@"6030000203",@"6030000196",@"6030000189",@"6030000182",@"6030000175",@"6030000168"];
//        _adIdArray = @[@"6030000238",@"6030000189",@"6030000224",@"6030000217",@"6030000182",@"6030000168",@"6030000203",@"6030000231",@"6030000210",@"6030000196",@"6030000175"];
//    }
//    return _adIdArray;
//}
@end
