//
//  AdViewCustomEventBanner.m
//  AdViewSDKDemo
//
//  Created by hupan on 2022/3/7.
//  Copyright © 2022 AdView. All rights reserved.
//

#import "AdViewCustomEventBanner.h"
#import <AdViewSDK/AdViewSDK.h>
#import <GoogleMobileAds/GoogleMobileAds.h>

@interface AdViewCustomEventBanner () <GADCustomEventBanner, AdViewViewDelegate>

@property (nonatomic, strong) AdViewView *banner;

@property (nonatomic, copy) NSString *appID;

@property (nonatomic, copy) NSString *positionId;

@end

@implementation AdViewCustomEventBanner
@synthesize delegate;

- (void)requestBannerAd:(GADAdSize)adSize parameter:(NSString *)serverParameter label:(NSString *)serverLabel request:(GADCustomEventRequest *)request {
    
    
    if (![self checkAppParameter:serverParameter]) {
        return;
    }
    AdViewBannerSize bannerSize = [self AdViewBannerAdSizeFromRequestedSize:adSize];
    self.banner = [AdViewView requestBannerSize:bannerSize
                                     bannerType:AdViewBannerType_normal
                                     positionId:self.positionId
                                       delegate:self];

}

- (BOOL)checkAppParameter:(NSString *)parameter{
    if (parameter && ![parameter isEqualToString:@""]) {
        NSArray *array = [parameter componentsSeparatedByString:@";"];
        self.appID = array.firstObject;
        self.positionId = array.lastObject;
        return YES;
    }
    NSString *errorMessage = @"parameter can not be extracted. Please check your configuration on AdMob dashboard.";
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
    NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:0 userInfo:userInfo];
    [self.delegate customEventBanner:self didFailAd:error];
    return NO;
}


- (AdViewBannerSize)AdViewBannerAdSizeFromRequestedSize:(GADAdSize)requestedSize
{
    CGSize requestAdSize = requestedSize.size;

    if ((int)(requestAdSize.height) >= 250 && (int)(requestAdSize.width) >= 300){
        return AdViewBannerSize_300x250;
    }else if ((int)(requestAdSize.height) >= 90 && (int)(requestAdSize.width) >= 728) {
        return AdViewBannerSize_728x90;
    } else if ((int)(requestAdSize.height) >= 60 && (int)(requestAdSize.width) >= 480){
        return AdViewBannerSize_480x60;
    } else if ((int)(requestAdSize.height) >= 44 && (int)(requestAdSize.width) >= 480){
        return AdViewBannerSize_480x44;
    } else {
        return AdViewBannerSize_320x50;
    }
}

#pragma mark AdViewDelegate implementation
- (NSString *)appId {
    return self.appID;
}

- (UIViewController *)viewControllerForShowModal {
    return self.delegate.viewControllerForPresentingModalView;
}

- (int)autoRefreshInterval{
    return 0;
}

- (void)didReceivedAd:(AdViewView *)adView {
    [self.delegate customEventBanner:self didReceiveAd:adView];
}

- (void)didFailToReceiveAd:(AdViewView *)adView Error:(NSError *)error {
    [self.delegate customEventBanner:self didFailAd:error];
}

- (void)adViewWillPresentScreen:(AdViewView *)adView{
    [self.delegate customEventBannerWillPresentModal:self];
    [self.delegate customEventBannerWasClicked:self];
}

- (void)adViewDidDismissScreen:(AdViewView *)adView{
    [self.delegate customEventBannerDidDismissModal:self];
}
- (BOOL)logMode {
    
    return YES;
}
@end
