//
//  LocalPlayerVideoManager.m
//  BXMSDKSample
//
//  Created by weicai on 2020/11/25.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "LocalPlayerVideoManager.h"
#import <BUAdSDK/BUNativeExpressRewardedVideoAd.h>
#import <BUAdSDK/BURewardedVideoModel.h>
#import <BUAdSDK/BUAdSDKManager.h>

static LocalPlayerVideoManager *_localVideo = nil;

@interface LocalPlayerVideoManager ()<BUNativeExpressRewardedVideoAdDelegate>

@property(nonatomic,strong)BUNativeExpressRewardedVideoAd *rewardedAd;

@property(nonatomic,strong)DHCWNativeLocalVideoAd *localVideoAd;

@end
@implementation LocalPlayerVideoManager

+ (instancetype)manager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _localVideo = [[LocalPlayerVideoManager alloc]init];
        
    });
    return _localVideo;
}

-(void)localVideoAd:(DHCWNativeLocalVideoAd *)videoAd
{
    self.localVideoAd = videoAd;
    [self loadCSJVideo];
}

-(void)showVideoFromController:(UIViewController *)controller
{
    [self.rewardedAd showAdFromRootViewController:controller];
}


-(void)loadCSJVideo
{
    //[BUAdSDKManager setAppID:@"5109638"]; // 注册应用Id 穿山甲平台应用Id 5060085 仅为demo示例使用
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    model.userId = @"123"; //用户Id
    
//    SlotID: 视频广告位Id  945132252 仅为demo示例使用
    self.rewardedAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:@"946192270" rewardedVideoModel:model];
    self.rewardedAd.delegate = self;
    [self.rewardedAd loadAdData];
}


/**
 This method is called when video ad material loaded successfully.
 And you can call [BUNativeExpressRewardedVideoAd showAdFromRootViewController:].
 */
- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd
{
    
#pragma mark - 回调给SDK通知加载成功
//
    [self.localVideoAd nativeVideoDidSucess];
}

/**
 This method is called when video ad materia failed to load.
 @param error : the reason of error
 */
- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error
{
#pragma mark - 回调给SDK通知加载失败

    [self.localVideoAd nativeVideoDidFaliure:error];
}


/**
 This method is called when video ad slot has been shown.
 */
- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd
{
#pragma mark - 回调给SDK通知视频曝光成功
    [self.localVideoAd nativeVideoVisiable];
}

/**
 This method is called when video ad is closed.
 */
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd
{
#pragma mark - 回调给SDK通知视频关闭
    [self.localVideoAd nativeVideoClose];
}

/**
 This method is called when video ad is clicked.
 */
- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd
{
#pragma mark - 回调给SDK通知视频点击
    [self.localVideoAd nativeVideoClick];
}



/**
 This method is called when video ad play completed or an error occurred.
 @param error : the reason of error
 */
- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error
{
    if (error == nil) {
#pragma mark - 回调给SDK通知视频播放完成
        [self.localVideoAd nativeVideoFinish];
    }else
    {
#pragma mark - 回调给SDK通知视频未播放完成
        [self.localVideoAd nativeVideoUnFinish];
    }
}

/**
 Server verification which is requested asynchronously is succeeded. now include two v erify methods:
      1. C2C need  server verify  2. S2S don't need server verify
 @param verify :return YES when return value is 2000.
   视频验证成功
 */
- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:( id )rewardedVideoAd verify:(BOOL)verify
{
#pragma mark - 回调给SDK是否满足奖励条件
    [self.localVideoAd nativeVideoFinishVerify:verify];
}

@end
