//
//  KKSFeedPageViewController.m
//  Open
//
//  Created by 雷俊博 on 2021/5/11.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKSFeedPageViewController.h"
#import <KSAdSDK/KSCUFeedPage.h>

#import "KKTabBarViewController.h"

@interface KKSFeedPageViewController ()<KSCUVideoStateDelegate,KSCUContentStateDelegate,KSCUFeedPageCallBackProtocol>
{
    CGFloat statusBarHeight;
    UIEdgeInsets gSafeAreaInsets;
    BOOL firstStart;
}
@property (nonatomic, strong) KSCUFeedPage *feedPage;


@end

@implementation KKSFeedPageViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    //self.navigationController.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if ([UIScreen mainScreen].bounds.size.height > 811) {
        statusBarHeight = 44;
        gSafeAreaInsets = UIEdgeInsetsMake(44, 0, 34, 0);
    }else{
        statusBarHeight = 20;
        gSafeAreaInsets = UIEdgeInsetsMake(20, 0, 0, 0);
    }
    
    [self configContentPage];
}

- (void)configContentPage
{
    if (self.feedPage) {
        [self.feedPage.feedViewController removeFromParentViewController];
        self.feedPage = nil;
    }
    KSCUFeedPage *feedPage = [[KSCUFeedPage alloc] initWithPosId:@"6030000009"];
    self.feedPage = feedPage;
    self.feedPage.videoStateDelegate = self;
    self.feedPage.stateDelegate = self;
    self.feedPage.callBackDelegate = self;
    self.feedPage.hidesBottomBarWhenPushed = YES;
    UIViewController *vc = self.feedPage.feedViewController;
    [self addChildViewController:vc];
    vc.view.frame = self.view.bounds;
    [self.view addSubview:vc.view];
    
    [self navConfig];
}


- (void)navConfig
{
    UIView*navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, cl_kNavigationBarHeight)];
    navView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:navView];
    
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake( 10 * WIDTHRADIU, cl_kStatusBarHeight + 9.5, 50, 25);
    backBtn.titleLabel.textAlignment =NSTextAlignmentLeft;
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    
    
    
    
    //title
    UILabel* lb_title = [[UILabel alloc]init];
    lb_title.textColor = [UIColor whiteColor];
    lb_title.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [navView addSubview:lb_title];
    
    lb_title.text = @"看视频";
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navView);
        make.height.offset(25);
        make.top.offset(cl_kStatusBarHeight + 9.5);
    }];
    
    
}

#pragma mark - KSCUVideoStateDelegate
/**
 * 视频开始播放
 * @param videoContent 内容模型
 */
- (void)kscu_videoDidStartPlay:(id<KSCUContentInfo>)videoContent
{
    NSLog(@"视频开始播放");
    KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
    if (!firstStart) {
        firstStart = YES;
        
        
        //第一次播放
        if (tabVC.isStart) {
            [tabVC resumeAnimation];
        }else{
            [tabVC startAnimation];
        }
        
        
        if (![tabVC.videoIdArray containsObject:videoContent.publicContentId] && (![Tools isBlankString:videoContent.publicContentId])) {
            //[tabVC resumeAnimation];
            
            [tabVC addVideoId:videoContent.publicContentId];
        }
        
        
        
    }else{
        
        if (![tabVC.videoIdArray containsObject:videoContent.publicContentId] && (![Tools isBlankString:videoContent.publicContentId])) {
            [tabVC resumeAnimation];
            
            [tabVC addVideoId:videoContent.publicContentId];
        }
        
    }
}
/**
* 视频暂停播放
* @param videoContent 内容模型
*/
- (void)kscu_videoDidPause:(id<KSCUContentInfo>)videoContent
{
    NSLog(@"视频暂停播放");
    
    KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
    
    if ([tabVC.videoIdArray containsObject:videoContent.publicContentId] && ![tabVC.endvideoIdArray containsObject:videoContent.publicContentId] && (![Tools isBlankString:videoContent.publicContentId])) {
        [tabVC pausmeAnimation];
    }
    
}
/**
* 视频恢复播放
* @param videoContent 内容模型
*/
- (void)kscu_videoDidResume:(id<KSCUContentInfo>)videoContent
{
    NSLog(@"视频恢复播放");
    
    KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
    
    if ([tabVC.videoIdArray containsObject:videoContent.publicContentId] && ![tabVC.endvideoIdArray containsObject:videoContent.publicContentId] && (![Tools isBlankString:videoContent.publicContentId])) {
        [tabVC resumeAnimation];
    }
    
}
/**
* 视频停止播放
* @param videoContent 内容模型
* @param finished     是否播放完成
*/
- (void)kscu_videoDidEndPlay:(id<KSCUContentInfo>)videoContent isFinished:(BOOL)finished
{
    NSLog(@"视频停止播放");
    
    if (finished) {
        
        
        KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
        
        if (![Tools isBlankString:videoContent.publicContentId]) {
            if (![tabVC.endvideoIdArray containsObject:videoContent.publicContentId]) {
                [tabVC pausmeAnimation];
            }
            
            [tabVC addEndVideoId:videoContent.publicContentId];
        }
        
    }
    
}
/**
* 视频播放失败
* @param videoContent 内容模型
* @param error        失败原因
*/
- (void)kscu_videoDidFailedToPlay:(id<KSCUContentInfo>)videoContent withError:(NSError *)error
{
    NSLog(@"视频播放失败");
    KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
    
    [tabVC pausmeAnimation];
}


#pragma mark - KSCUContentStateDelegate
/**
* 内容展示
* @param content 内容模型
*/
- (void)kscu_contentDidFullDisplay:(id<KSCUContentInfo>)content
{
    NSLog(@"内容展示");
}
/**
* 内容隐藏
* @param content 内容模型
*/
- (void)kscu_contentDidEndDisplay:(id<KSCUContentInfo>)content
{
    NSLog(@"内容隐藏");
}
/**
* 内容暂停显示，ViewController disappear或者Application resign active
* @param content 内容模型
*/
- (void)kscu_contentDidPause:(id<KSCUContentInfo>)content
{
    NSLog(@" 内容暂停显示");
}
/**
* 内容恢复显示，ViewController appear或者Application become active
* @param content 内容模型
*/
- (void)kscu_contentDidResume:(id<KSCUContentInfo>)content
{
    NSLog(@"内容恢复显示");
}

#pragma mark - KSCUContentPageCallback
- (void)kscuClickContentShareWithItem:(NSString *)shareItem {
   // KSDemoShareMediaViewController *vc = [[KSDemoShareMediaViewController alloc] initWithShareItem:shareItem];
    //[self presentViewController:vc animated:YES completion:nil];
}






#pragma mark - event response
- (void)backClick {
    
    KKTabBarViewController* tabVC = (KKTabBarViewController *)self.navigationController.tabBarController;
    
    [tabVC pausmeAnimation];
    
    [self.tabBarController.navigationController popViewControllerAnimated:YES];
}

/*
#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
