//
//  ATRewardVideoManager.m
//  topon_flutter_plugin
//
//  Created by GUO PENG on 2021/6/26.
//

#import "ATRewardVideoManager.h"
//#import <AnyThinkSDK/AnyThinkSDK.h>
#import <AnyThinkSDK/AnyThinkSDK.h>
#import "ATCommonTool.h"
#import "ATRewardVideoDelegate.h"
#import "ATConfiguration.h"

#define kATAdLoadingExtraUserData  @"kATAdLoadingExtraMediaExtraKey"
#define kATAdLoadingExtraUserDataKeywordKey  @"kATAdLoadingExtraUserDataKeywordKey"
#define kATAdLoadingExtraUserID  @"kATAdLoadingExtraUserIDKey"

@interface ATRewardVideoManager()

@property(nonatomic, strong) ATRewardVideoDelegate *rewardedVideoDelegate;



@end


@implementation ATRewardVideoManager

#pragma mark - public
/// Load rewarded video
- (void)loadRewardedVideo:(NSString *)placementID extraDic:(NSDictionary *)extraDic {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:extraDic];

    if ([extraDic.allKeys containsObject:kATAdLoadingExtraUserDataKeywordKey]) {
        [dic removeObjectForKey:kATAdLoadingExtraUserDataKeywordKey];
        dic[kATAdLoadingExtraMediaExtraKey] = extraDic[kATAdLoadingExtraUserDataKeywordKey];
    }
    if ([extraDic.allKeys containsObject:kATAdLoadingExtraUserID]) {
        [dic removeObjectForKey:kATAdLoadingExtraUserID];
        dic[kATAdLoadingExtraUserIDKey] = extraDic[kATAdLoadingExtraUserID];
    }
    [ATCommonTool applyAtAdRequestAndRemove:dic];
    [[ATAdManager sharedManager] setMultipleLoadingDelegate:self.rewardedVideoDelegate placementId:placementID];

    [[ATAdManager sharedManager] loadADWithPlacementID:placementID extra:dic delegate:self.rewardedVideoDelegate];
}


/// Show rewarded video ad
- (void)showRewardedVideo:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [[ATAdManager sharedManager] showRewardedVideoWithPlacementID:placementID inViewController:[ATCommonTool currentViewController] delegate:self.rewardedVideoDelegate];
}

/// Display scene rewarded video ads
- (void)showRewardedVideo:(NSString *)placementID sceneID:(NSString *)sceneID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    
    [[ATAdManager sharedManager] showRewardedVideoWithPlacementID:placementID scene:sceneID inViewController:[ATCommonTool currentViewController] delegate:self.rewardedVideoDelegate];
}

/// Display rewarded video ads through config
- (void)showRewardedVideoWithShowConfig:(NSString *)placementID sceneID:(NSString *)sceneID showCustomExt:(NSString *)showCustomExt {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    showCustomExt = [ATCommonTool checkStrParamsEmptyAndReturn:showCustomExt];
     
    ATShowConfig *showConfig = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:showCustomExt];
    
    [[ATAdManager sharedManager] showRewardedVideoWithPlacementID:placementID config:showConfig inViewController:[ATCommonTool currentViewController] delegate:self.rewardedVideoDelegate];
}


/// Whether ad is cached
- (BOOL)rewardedVideoReady:(NSString *)placementID{
    
    if (kATStringIsEmpty(placementID)) {
        return NO;
    }
    
    BOOL isReady = [[ATAdManager sharedManager] rewardedVideoReadyForPlacementID:placementID];
    return  isReady;
}

/// Check ad status
- (NSDictionary *)checkRewardedVideoLoadStatus:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return [NSDictionary dictionary];
    }
    
    ATCheckLoadModel *checkLoadModel = [[ATAdManager sharedManager] checkRewardedVideoLoadStatusForPlacementID:placementID];

    NSDictionary *dic = [ATCommonTool objectToJSONString:checkLoadModel];
    return  dic;
}

/// Get information about all available ads under the current ad slot, supported by v5.7.53 and above.
- (NSString *)getRewardedVideoValidAds:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return @"";
    }
    
    NSArray *array = [[ATAdManager sharedManager] getRewardedVideoValidAdsForPlacementID:placementID];
    
    NSString *str = [ATCommonTool toReadableJSONString:array];
    
    return str;
}

/// Report scenario entry rate
- (void)entryScenarioWithPlacementID:(NSString *)placementID sceneID:(NSString *)sceneID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    ATLog(@"entryRewardedVideoScenarioWithPlacementID: %@ --- sceneID:%@",placementID,sceneID);

    [[ATAdManager sharedManager] entryRewardedVideoScenarioWithPlacementID:placementID scene:sceneID];
}
 
/// Enable auto-load rewarded video
- (void)autoLoadRewardedVideo:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [ATRewardedVideoAutoAdManager sharedInstance].delegate = self.rewardedVideoDelegate;
    [[ATRewardedVideoAutoAdManager sharedInstance] addAutoLoadAdPlacementIDArray:[placementID componentsSeparatedByString:@","]];
}

/// Cancel auto-load rewarded video
- (void)cancelAutoLoadRewardedVideo:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [[ATRewardedVideoAutoAdManager sharedInstance] removeAutoLoadAdPlacementIDArray:[placementID componentsSeparatedByString:@","]];
}

/// Display fully automatically loaded rewarded video ads (scenario only)
- (void)showAutoLoadRewardedVideoAD:(NSString *)placementID sceneID:(NSString *)sceneID {
    [self showAutoLoadRewardedVideoAD:placementID sceneID:sceneID showCustomExt:@""];
}

/// Display fully automatically loaded rewarded video ads (showConfig)
- (void)showAutoLoadRewardedVideoAD:(NSString *)placementID sceneID:(NSString *)sceneID showCustomExt:(NSString *)showCustomExt {

    if (kATStringIsEmpty(placementID)) {
        return;
    }
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
  if (sceneID.length == 0) {
    [[ATRewardedVideoAutoAdManager sharedInstance] showAutoLoadRewardedVideoWithPlacementID:placementID inViewController:[ATCommonTool currentViewController] delegate:self.rewardedVideoDelegate];
  } else {
    showCustomExt = [ATCommonTool checkStrParamsEmptyAndReturn:showCustomExt];
    ATShowConfig *config = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:showCustomExt];
    [[ATRewardedVideoAutoAdManager sharedInstance] showAutoLoadRewardedVideoWithPlacementID:placementID showConfig:config inViewController:[ATCommonTool currentViewController] delegate:self.rewardedVideoDelegate];
  }

}

/// Set the automatic loading rewarded video ad return parameters. If no extra content is passed in, it can be used to clear it.
- (void)autoLoadRewardedVideoSetLocalExtra:(NSString *)placementID extraDic:(NSDictionary *)extraDic {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:extraDic];

    if ([extraDic.allKeys containsObject:kATAdLoadingExtraUserDataKeywordKey]) {
        [dic removeObjectForKey:kATAdLoadingExtraUserDataKeywordKey];
        dic[kATAdLoadingExtraMediaExtraKey] = extraDic[kATAdLoadingExtraUserDataKeywordKey];
    }
    if ([extraDic.allKeys containsObject:kATAdLoadingExtraUserID]) {
        [dic removeObjectForKey:kATAdLoadingExtraUserID];
        dic[kATAdLoadingExtraUserIDKey] = extraDic[kATAdLoadingExtraUserID];
    }
     
    [[ATRewardedVideoAutoAdManager sharedInstance] setLocalExtra:dic placementID:placementID];
}
 
#pragma mark - lazy
- (ATRewardVideoDelegate *)rewardedVideoDelegate {

    if (_rewardedVideoDelegate) return _rewardedVideoDelegate;

    ATRewardVideoDelegate *rewardedVideoDelegate = [ATRewardVideoDelegate new];

    return _rewardedVideoDelegate = rewardedVideoDelegate;
}

@end



