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

#import "KKActivityViewController.h"
#import <BXMediaSDK/BXMediaCenterSDK.h>
#import <BUAdSDK/BUAdSDK.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import <GDTMobSDK/GDTRewardVideoAd.h>
#import <GDTMobSDK/GDTUnifiedBannerView.h>
#import <GDTMobSDK/GDTUnifiedInterstitialAd.h>
#import "UIViewController+BackButtonHandler.h"

@interface KKActivityViewController ()<BXMediaCenterSDKDelegate,BUNativeExpressRewardedVideoAdDelegate,GDTRewardedVideoAdDelegate,BUNativeExpressFullscreenVideoAdDelegate>
@property (nonatomic,strong) BXMediaCenterSDK *center;
@property (nonatomic,strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;
@property (nonatomic, strong) GDTRewardVideoAd *rewardVideoAd;
@property (nonatomic,strong) NSString *adId;
@property (nonatomic,strong) NSString *bannerAdId;
// 视频验证是否通过
@property (nonatomic,assign) BOOL videoVerify;


@property (nonatomic,strong) UIView *gdtExpressAdContentView;

@end

@implementation KKActivityViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.center = [[BXMediaCenterSDK alloc] init];
    // 设置代理
    self.center.delegate = self;
    // 设置当前版本支持的广告源
    [self.center setAdSources:@"1,2"];
    /**
     thirdUserId: 媒体的用户的id
     placeId: 资源位id
     */
//    NSString*adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    UIView *adView = [self.center getViewWithFram:CGRectMake(0, [self getStatusBarHeight]+[self getNavigationBarHeight], self.view.frame.size.width, self.view.frame.size.height-[self getStatusBarHeight]-[self getNavigationBarHeight]) thirdUserId:@"" placeId:self.placeId];
    
    [self.view addSubview:adView];
    
}

#pragma mark - BXMediaCenterSDKDelegate
/// 展示视频
- (void)showVideoAd:(BXMediaModel *)model{
    self.adId = model.requestId;
    
    /**
     model中的 adType: 平台类型
              pid   : 对应平台的代码位
     */
    
    // 这里媒体调用自己的加载激励视频的方法
    NSLog(@"展示激励视频广告");
    NSLog(@"平台类型=======> %ld",model.adType);
    NSLog(@"获取对应平台的代码位ID ==========> %@",model.pid);
    self.adId = model.requestId;
    if (model.adType == 1) { // 穿山甲激励视屏广告
//        NSString*adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        BURewardedVideoModel *rewardModel = [[BURewardedVideoModel alloc] init];
        rewardModel.userId = @"";
        if (self.buId.length > 0) {
            self.rewardedVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:self.buId rewardedVideoModel:rewardModel];
        }else{
            self.rewardedVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:model.pid rewardedVideoModel:rewardModel];
        }
        
    
        self.rewardedVideoAd.delegate = self;
        [self.rewardedVideoAd loadAdData];
    }else if(model.adType == 2){ // 广点通激励视频广告
        if (self.gdtAdId.length > 0) {
            self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:self.gdtAdId];
        }else{
            self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:model.pid];
        }
        self.rewardVideoAd.delegate = self;
        self.rewardVideoAd.videoMuted = NO; // 设置激励视频是否静音
        [self.rewardVideoAd loadAd];
    }
}
/// 展示分享
- (void)showShare:(BXMediaModel *)model{

    // 这里要媒体自己调用分享模块
    [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"分享中\n requestID-%@",model.requestId]];
    __weak typeof(self) weakSelf = self;
    [SVProgressHUD dismissWithDelay:2 completion:^{
        [weakSelf.center exportShareSuccessWithId:model.requestId];
    }];

}


/// 媒体自提现
- (void)withdraw:(BXMOrderModel *)orderModel{

    // 这里要媒体自己调用提现模块
    [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"提现中\n giveOrderNo-%@",orderModel.giveOrderNo]];
//    __weak typeof(self) weakSelf = self;
    [SVProgressHUD dismissWithDelay:2 completion:^{

    }];

}

/// 显示banner类广告
/// @param model model
- (void)showBannerAd:(BXMediaModel *)model{
    NSLog(@"展示bannerAd");
    NSLog(@"获取对应平台的代码位ID ==========> %@",model.pid);
  
    
}

/// 隐藏banner类广告
- (void)hideBannerAd{
    NSLog(@"隐藏ADbanner");
    
}

/// 获取视图加载进度
/// @param progress 加载进度
- (void)loadProgress:(double)progress{
    NSLog(@"%f",progress);
}

/// 获取当前活动标题
/// @param title 活动标题
- (void)activityTitle:(NSString *)title{
    self.navigationItem.title = title;
}
// ************************************************
//
//   如果没有特殊定制功能,不用实现下边四个方法
//
// ************************************************

/// sdk获取活动数据
/// @param activityData 传递活动参数
/// @param callBack 回调
- (void)getActivityData:(NSString *)activityData callBack:(NSString *)callBack{

    NSString *jsonData = @"{\"bizInfo\":{\"headImg\":\"https://static.hixiaoman.com/saas/act/task/ggk.png\",\"nickName\":\"智慧小宝\"     },     \"bizType\": 1 }";
    [self.center exportActivityData:jsonData callBack:callBack];
}

/// 跳转具体任务
/// @param page 任务数据
/// @param callBack 回调
- (void)openPage:(NSString *)page callBack:(NSString *)callBack{

    [SVProgressHUD showInfoWithStatus:page];
    [SVProgressHUD dismissWithDelay:2];
    // 这里需要跳转到任务页面,完成任务后,要把完成任务后的数据按照协商文档回传给sdk
    NSString *jsonData = @"组装的任务相关数据";
    [self.center exportPageData:jsonData callBack:callBack];
}


/// sdk同步数据给媒体 更新活动数据
/// @param activityData 要同步的数据
/// @param callBack 回调
- (void)updateActivityData:(NSString *)activityData callBack:(NSString *)callBack{
    [SVProgressHUD showInfoWithStatus:activityData];
    [SVProgressHUD dismissWithDelay:2];
    NSString *jsonData = @"组装上报数据";
    [self.center exportActivityData:jsonData callBack:callBack];
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate

- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    NSLog(@"穿山甲-激励视频广告-物料-加载成功");
    [self.rewardedVideoAd showAdFromRootViewController:self.navigationController ritScene:BURitSceneType_home_get_bonus ritSceneDescribe:nil];
    [self.center exportVideoLoadSuccess:self.adId];
    self.videoVerify = NO;
}

- (void)nativeExpressRewardedVideoAdViewRenderSuccess:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    NSLog(@"穿山甲-激励视频广告-展示");
    [self.center exportVideoExposeSuccess:self.adId];
}

- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    NSLog(@"穿山甲-视频关闭");
    if (self.videoVerify) {
        [self.center exportVideoPlaySuccessWithId:self.adId];
    }else{
        [self.center exportVideoSkipWithId:self.adId];
    }
}

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
    NSLog(@"穿山甲-视频或素材加载失败: %@",error);
    [self.center exportVideoPlayFaildWithId:self.adId error:error];
}

- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    NSLog(@"穿山甲-激励视频广告-点击");
    [self.center exportVideoClick:self.adId];
}

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify{
    NSLog(@"穿山甲-激励视屏-验证结果-%d",verify);
    self.videoVerify = verify;
}




#pragma mark - GDTRewardedVideoAdDelegate

- (void)gdt_rewardVideoAdVideoDidLoad:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"广点通-视频数据下载成功回调，已经下载过的视频会直接回调");
    self.videoVerify = NO;
    if (self.rewardVideoAd.isAdValid) {
        [self.rewardVideoAd showAdFromRootViewController:self];
        [self.center exportVideoLoadSuccess:self.adId];
    }
}

- (void)gdt_rewardVideoAdDidExposed:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"广点通-视频广告曝光回调");
    [self.center exportVideoExposeSuccess:self.adId];
}

- (void)gdt_rewardVideoAdDidClose:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"广点通-视频播放页关闭回调");
    if (self.videoVerify) {
        [self.center exportVideoPlaySuccessWithId:self.adId];
    }else{
        NSLog(@"调用跳过方法");
        [self.center exportVideoSkipWithId:self.adId];
    }
}

- (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    NSLog(@"广点通-视频广告各种错误信息回调 %@",error);
    [self.center exportVideoPlayFaildWithId:self.adId error:error];
}

- (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd info:(NSDictionary *)info{
    NSLog(@"广点通-视频广告播放达到激励条件回调");
    self.videoVerify = YES;
}

- (void)gdt_rewardVideoAdDidClicked:(GDTRewardVideoAd *)rewardedVideoAd{
    NSLog(@"广点通-视频广告点击");
    [self.center exportVideoClick:self.adId];
}


/// 拦截返回按钮事件
- (BOOL)navigationShouldPopOnBackButton{
    __weak typeof(self) weakSelf = self;
    [self.center backButtonClick:^(BOOL result) {
        if (result) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    return NO;
}

#pragma mark - method
//禁用屏幕左滑返回手势
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        
    }
}




/**
 *  获取状态栏高度
 */
-(CGFloat)getStatusBarHeight{
    return [[UIApplication sharedApplication] statusBarFrame].size.height;
}
/**
 *  获取导航栏高度
 */
-(CGFloat)getNavigationBarHeight{
    return self.navigationController.navigationBar.frame.size.height;
}

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