//
//  ATInterstitialManager.m
//  topon_flutter_plugin
//
//  Created by GUO PENG on 2021/6/28.
//

#import "ATInterstitialManager.h"
#import <AnyThinkSDK/AnyThinkSDK.h>
#import "ATCommonTool.h"
#import "ATInterstitialDelegate.h"
#import "ATConfiguration.h"

#define UseRewardedVideoAsInterstitialKey @"UseRewardedVideoAsInterstitialKey"
#define ATInterstitialExtraAdSizeKey @"size"

@interface ATInterstitialManager()

@property(nonatomic, strong) ATInterstitialDelegate *interstitialDelegate;
 
@end

@implementation ATInterstitialManager

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

// The incentive video is used as an interstitial screen (calling Sigmob's incentive video API)
    if ([extraDic.allKeys containsObject:UseRewardedVideoAsInterstitialKey]) {
        [dic removeObjectForKey:UseRewardedVideoAsInterstitialKey];
        dic[kATInterstitialExtraUsesRewardedVideo] = extraDic[UseRewardedVideoAsInterstitialKey];
    }
    
// You can use the following code to set the size of the interstitial image ad on the Pangolin platform
    if ([extraDic.allKeys containsObject:ATInterstitialExtraAdSizeKey]) {
        
        [dic removeObjectForKey:ATInterstitialExtraAdSizeKey];

        NSDictionary *tempDic = extraDic[ATInterstitialExtraAdSizeKey];

        NSNumber *widthNumeber = tempDic[@"width"];
        NSNumber *heightNumeber = tempDic[@"height"];

        CGSize tempSize = CGSizeMake([widthNumeber doubleValue], [heightNumeber doubleValue]);

        dic[kATInterstitialExtraAdSizeKey] = [NSValue valueWithCGSize:tempSize];
    }
    [ATCommonTool applyAtAdRequestAndRemove:dic];
    [[ATAdManager sharedManager] setMultipleLoadingDelegate:self.interstitialDelegate placementId:placementID];
    [[ATAdManager sharedManager] loadADWithPlacementID:placementID extra:dic delegate:self.interstitialDelegate];
}

/// Whether interstitial ad is ready
- (BOOL)hasInterstitialAdReady:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return NO;
    }
    
    BOOL isReady = [[ATAdManager sharedManager] interstitialReadyForPlacementID:placementID];
    return  isReady;
}

/// Get information about all available ads under the current ad slot
- (NSString *)getInterstitialValidAds:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return @"";
    }
    
    NSArray *array = [[ATAdManager sharedManager] getInterstitialValidAdsForPlacementID:placementID];

    NSString *str = [ATCommonTool toReadableJSONString:array];

    return str;
}

/// Get placement status
- (NSDictionary *)checkInterstitialLoadStatus:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return [NSDictionary dictionary];
    }
    
    ATCheckLoadModel *checkLoadModel = [[ATAdManager sharedManager] checkInterstitialLoadStatusForPlacementID:placementID];

    NSDictionary *dic = [ATCommonTool objectToJSONString:checkLoadModel];
    return  dic;
}
/// Show interstitial ad
- (void)showInterstitialAd:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [[ATAdManager sharedManager] showInterstitialWithPlacementID:placementID inViewController:[ATCommonTool currentViewController] delegate:self.interstitialDelegate];
}

/// Show interstitial ad with scenario
- (void)showInterstitialAd:(NSString *)placementID sceneID:(NSString *)sceneID{
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    
    [[ATAdManager sharedManager] showInterstitialWithPlacementID:placementID scene:sceneID inViewController:[ATCommonTool currentViewController] delegate:self.interstitialDelegate];
}

/// Display scene interstitial ads through config
- (void)showInterstitialAdWithShowConfig:(NSString *)placementID sceneID:(NSString *)sceneID showCustomExt:(NSString *)showCustomExt {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    placementID = [ATCommonTool checkStrParamsEmptyAndReturn:placementID];
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    showCustomExt = [ATCommonTool checkStrParamsEmptyAndReturn:showCustomExt];
     
    ATShowConfig * showConfig = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:showCustomExt];
    
    [[ATAdManager sharedManager] showInterstitialWithPlacementID:placementID showConfig:showConfig inViewController:[ATCommonTool getRootViewController] delegate:self.interstitialDelegate nativeMixViewBlock:^(ATNativeMixInterstitialView * _Nonnull selfRenderingMixInterstitialView) {
        
    }]; 
}

/// Report scenario entry rate
- (void)entryScenarioWithPlacementID:(NSString *)placementID sceneID:(NSString *)sceneID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    ATLog(@"entryInterScenarioWithPlacementID: %@ --- sceneID:%@",placementID,sceneID);
    
    [[ATAdManager sharedManager] entryInterstitialScenarioWithPlacementID:placementID scene:sceneID];
}

/// Enable auto-load
- (void)autoLoadInterstitialAD:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [ATInterstitialAutoAdManager sharedInstance].delegate = self.interstitialDelegate;

    [[ATInterstitialAutoAdManager sharedInstance] addAutoLoadAdPlacementIDArray:[placementID componentsSeparatedByString:@","]];
}

/// Cancel auto-load interstitial
- (void)cancelAutoLoadInterstitialAD:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    [[ATInterstitialAutoAdManager sharedInstance] removeAutoLoadAdPlacementIDArray:[placementID componentsSeparatedByString:@","]];
}

/// Show fully automatic loading interstitial screen (scenario only)
- (void)showAutoLoadInterstitialADWithPlacementID:(NSString *)placementID sceneID:(NSString *)sceneID {
    [self showAutoLoadInterstitialADWithPlacementID:placementID sceneID:sceneID showCustomExt:@""];
}

/// Display fully automatic loading interstitial screen (showConfig)
- (void)showAutoLoadInterstitialADWithPlacementID:(NSString *)placementID sceneID:(NSString *)sceneID showCustomExt:(NSString *)showCustomExt {

    if (kATStringIsEmpty(placementID)) {
        return;
    }

    placementID = [ATCommonTool checkStrParamsEmptyAndReturn:placementID];
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];

    if (sceneID.length == 0) {
        [[ATInterstitialAutoAdManager sharedInstance] showAutoLoadInterstitialWithPlacementID:placementID inViewController:[ATCommonTool currentViewController] delegate:self.interstitialDelegate];
    } else {
        showCustomExt = [ATCommonTool checkStrParamsEmptyAndReturn:showCustomExt];
        ATShowConfig *config = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:showCustomExt];
        [[ATInterstitialAutoAdManager sharedInstance] showAutoLoadInterstitialWithPlacementID:placementID showConfig:config inViewController:[ATCommonTool currentViewController] delegate:self.interstitialDelegate];
    }
}

///Set the return parameters for automatically loading interstitial ads. If no extra content is passed in, it can be used to clear it.
- (void)autoLoadInterstitialADSetLocalExtra:(NSString *)placementID extraDic:(NSDictionary *)extraDic {

    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:extraDic];
  
// The incentive video is used as an interstitial screen (calling Sigmob's incentive video API)
    if ([extraDic.allKeys containsObject:UseRewardedVideoAsInterstitialKey]) {
        [dic removeObjectForKey:UseRewardedVideoAsInterstitialKey];
        dic[kATInterstitialExtraUsesRewardedVideo] = extraDic[UseRewardedVideoAsInterstitialKey];
    }
    
// You can use the following code to set the size of the interstitial image ad on the Pangolin platform
    if ([extraDic.allKeys containsObject:ATInterstitialExtraAdSizeKey]) {
        
        [dic removeObjectForKey:ATInterstitialExtraAdSizeKey];

        NSDictionary *tempDic = extraDic[ATInterstitialExtraAdSizeKey];

        NSNumber *widthNumeber = tempDic[@"width"];
        NSNumber *heightNumeber = tempDic[@"height"];

        CGSize tempSize = CGSizeMake([widthNumeber doubleValue], [heightNumeber doubleValue]);

        dic[kATInterstitialExtraAdSizeKey] = [NSValue valueWithCGSize:tempSize];
    }
    
    [[ATInterstitialAutoAdManager sharedInstance] setLocalExtra:dic placementID:placementID];
}

#pragma mark - lazy
- (ATInterstitialDelegate *)interstitialDelegate {

    if (_interstitialDelegate) return _interstitialDelegate;

    ATInterstitialDelegate *interstitialDelegate = [ATInterstitialDelegate new];

    return _interstitialDelegate = interstitialDelegate;
}

@end
