//
//  ATPlatformBannerManager.m
//  anythink_sdk
//
//  Created by GUO PENG on 2021/7/12.
//

#import "ATPlatformBannerManager.h"
#import <AnyThinkSDK/AnyThinkSDK.h>
#import "ATCommonTool.h"
#import "ATConfiguration.h"
#import "ATBannerDelegate.h"
#import "ATBannerTool.h"
// AdMob adaptive banner supported since 5.6.6 (import header when needed)
//#import <GoogleMobileAds/GoogleMobileAds.h>

static NSString *kATBannerAdLoadingExtraInlineAdaptiveWidthKey = @"adaptive_width";
static NSString *kATBannerAdLoadingExtraInlineAdaptiveOrientationKey = @"adaptive_orientation";

@interface ATPlatformBannerManager()

@property(nonatomic, strong) ATBannerDelegate *bannerDelegate;

@end

@implementation ATPlatformBannerManager

#pragma mark - public

/// Synchronously fetch the loaded SDK Banner UIView and add hostView.
/// The rendering path is consistent with Flutter `ATFBannerPlatformView.m`:
///   ATBannerView * bv = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:config:];
/// bv.delegate / backgroundColor / presentingViewController; returned to the container as a subview.
- (BOOL)attachBannerToHostView:(UIView *)hostView
                   placementID:(NSString *)placementID
                       sceneID:(NSString *)sceneID {
    if (hostView == nil || placementID.length == 0) {
        return NO;
    }
    ATShowConfig *cfg = [[ATShowConfig alloc] initWithScene:(sceneID ?: @"") showCustomExt:@""];
    ATBannerView *bv = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:placementID config:cfg];
    if (bv == nil) {
        ATLog(@"ATPlatformBannerManager.attachBannerToHostView retrieveBannerView=nil (not loaded yet), placementID=%@",
              placementID);
        return NO;
    }
    bv.delegate = self.bannerDelegate;
    bv.backgroundColor = [UIColor whiteColor];
    bv.presentingViewController = [ATCommonTool getRootViewController];
    bv.frame = hostView.bounds;
    bv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [hostView addSubview:bv];
    ATLog(@"ATPlatformBannerManager.attachBannerToHostView attached placementID=%@ bv=%@", placementID, bv);
    return YES;
}

/// Load banner ad
- (void)loadBannerWith:(NSString *)placementID extraDic:(NSDictionary *)extraDic{
    
    CGRect rect = [ATBannerTool getSizeFromExtraDic:extraDic];
    NSValue *adSize = [NSValue valueWithCGSize:CGSizeMake(rect.size.width,  rect.size.height)];
    [[ATAdManager sharedManager] setMultipleLoadingDelegate:self.bannerDelegate placementId:placementID];

    if (extraDic[kATBannerAdLoadingExtraInlineAdaptiveWidthKey] != nil && extraDic[kATBannerAdLoadingExtraInlineAdaptiveOrientationKey] != nil) {

        [[ATAdManager sharedManager] loadADWithPlacementID:placementID extra:@{

            kATAdLoadingExtraBannerAdSizeKey:adSize,
            
            kATAdLoadingExtraAdmobBannerSizeKey:adSize,
            kATAdLoadingExtraAdmobAdSizeFlagsKey:@(YES)

        } delegate:self.bannerDelegate];
        
    } else {
        [[ATAdManager sharedManager] loadADWithPlacementID:placementID extra:@{kATAdLoadingExtraBannerAdSizeKey:adSize} delegate:self.bannerDelegate];
    }
}



#pragma mark - lazy
- (ATBannerDelegate *)bannerDelegate {

    if (_bannerDelegate) return _bannerDelegate;

    ATBannerDelegate *bannerDelegate = [ATBannerDelegate new];

    return _bannerDelegate = bannerDelegate;
}
@end
