//
//  ATSplashAdManager.m
//  anythink_sdk
//
//  Created by GUO PENG on 2023/9/7.
//

#import "ATSplashAdManager.h"
#import "ATSplashDelegate.h"
#import <AnyThinkSDK/AnyThinkSDK.h>
#import "ATCommonTool.h"
#import "ATConfiguration.h"

@interface ATSplashAdManager()

@property(nonatomic, strong) ATSplashDelegate *splashAdDelegate;

@end


@implementation ATSplashAdManager

/// Load splash ad
- (void)loadSplashAd:(NSString *)placementID extraDic:(NSDictionary *)extraDic {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:extraDic];
    NSString *keyString = @"tolerateTimeout";
    if ([extraDic.allKeys containsObject:keyString]) {
        [dic removeObjectForKey:keyString];
        dic[kATSplashExtraTolerateTimeoutKey] = @([extraDic[keyString] floatValue] * 0.001);
    }
    [ATCommonTool applyAtAdRequestAndRemove:dic];
    [[ATAdManager sharedManager] setMultipleLoadingDelegate:self.splashAdDelegate placementId:placementID];

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

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

/// Check ad status
- (NSDictionary *)checkSplashAdLoadStatus:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return [NSDictionary dictionary];
    }
    
    ATCheckLoadModel *checkLoadModel = [[ATAdManager sharedManager] checkSplashLoadStatusForPlacementID: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 *)getSplashAdValidAds:(NSString *)placementID {

    if (kATStringIsEmpty(placementID)) {
        return @"";
    }
    
    NSArray *array = [[ATAdManager sharedManager] getSplashValidAdsForPlacementID:placementID];
    NSString *str = [ATCommonTool toReadableJSONString:array];
    return str;
}

/// Show splash ad
- (void)showSplashAd:(NSString *)placementID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    ATShowConfig * showConfig = [ATShowConfig new];
    [[ATAdManager sharedManager] showSplashWithPlacementID:placementID config:showConfig window:window inViewController:[ATCommonTool getRootViewController] extra:nil delegate:self.splashAdDelegate];
    
}

/// Display scene opening screen advertisement
- (void)showSplashAd:(NSString *)placementID sceneID:(NSString *)sceneID {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    ATShowConfig * showConfig = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:nil];
    
    [[ATAdManager sharedManager] showSplashWithPlacementID:placementID config:showConfig window:window inViewController:[ATCommonTool getRootViewController] extra:nil delegate:self.splashAdDelegate];
}

/// Display open-screen ads through config
- (void)showSplashAdWithShowConfig:(NSString *)placementID sceneID:(NSString *)sceneID showCustomExt:(NSString *)showCustomExt {
    
    if (kATStringIsEmpty(placementID)) {
        return;
    }
     
    sceneID = [ATCommonTool checkStrParamsEmptyAndReturn:sceneID];
    showCustomExt = [ATCommonTool checkStrParamsEmptyAndReturn:showCustomExt];
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    ATShowConfig * showConfig = [[ATShowConfig alloc] initWithScene:sceneID showCustomExt:showCustomExt];
    
    [[ATAdManager sharedManager] showSplashWithPlacementID:placementID config:showConfig window:window inViewController:[ATCommonTool getRootViewController] extra:nil delegate:self.splashAdDelegate];
}

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

    [[ATAdManager sharedManager] entrySplashScenarioWithPlacementID:placementID scene:sceneID];
}

#pragma mark - lazy
- (ATSplashDelegate *)splashAdDelegate {
    if (_splashAdDelegate) return _splashAdDelegate;
    ATSplashDelegate *splashDelegate = [ATSplashDelegate new];
    return _splashAdDelegate = splashDelegate;
}

@end
