//
//  AdViewCustomEventNative.m
//  admobTest
//
//  Created by hupan on 2022/3/15.
//

#import "AdViewCustomEventNative.h"
#import <AdViewSDK/AdViewSDK.h>
#import "AdViewMediatedNativeAd.h"

@interface AdViewCustomEventNative()<AdViewNativeAdDelegate, GADNativeAdLoaderDelegate>

@property (nonatomic, strong) AdViewNativeAd * native;

@property (nonatomic, copy) NSString *appID;

@property (nonatomic, copy) NSString *positionId;

@property (nonatomic, assign) int adCount;

@property (nonatomic, strong) AdViewMediatedNativeAd *mediatedAd;

@end

@implementation AdViewCustomEventNative

@synthesize delegate;

- (void)requestNativeAdWithParameter:(NSString *)serverParameter
                             request:(GADCustomEventRequest *)request
                             adTypes:(NSArray *)adTypes
                             options:(NSArray *)options
                  rootViewController:(UIViewController *)rootViewController {
    if (![self checkAppParameter:serverParameter]) {
        return;
    }
    
    
    self.native = [[AdViewNativeAd alloc] initWithAppKey:self.appID positionID:self.positionId];
    self.native.delegate = self;
    self.native.controller = rootViewController;
    [self.native loadNativeAdWithCount:1];
}

- (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 customEventNativeAd:self didFailToLoadWithError:error];
    return NO;
}

- (BOOL)handlesUserClicks
{
    return NO;
}

- (BOOL)handlesUserImpressions
{
    return NO;
}


#pragma mark AdViewNativeDelegate implementation
/*
 *
 */
- (void)adViewNativeAdSuccessToLoadAd:(AdViewNativeAd *)adViewNativeAd NativeData:(NSArray <AdViewNativeData *>*)nativeDataArray{
    self.mediatedAd = [[AdViewMediatedNativeAd alloc] initWithAdViewNativeAd:adViewNativeAd NativeData:nativeDataArray];
    [self.delegate customEventNativeAd:self didReceiveMediatedUnifiedNativeAd:self.mediatedAd];
}

/*
 *
 */
- (void)adViewNativeAdFailToLoadAd:(AdViewNativeAd *)adViewNativeAd WithError:(NSError *)error{
    [self.delegate customEventNativeAd:self didFailToLoadWithError:error];
}

/*
 *
 */
- (void)adViewNativeAdWillShowPresent{
    [GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdDidRecordClick:self.mediatedAd];
    [GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdWillPresentScreen:self.mediatedAd];
}

/*
 *
 */
- (void)adViewNativeAdClosed{
    [GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdDidDismissScreen:self.mediatedAd];
}

/*
 * 
 */
- (void)adViewNativeAdResignActive{
//    [GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdWillLeaveApplication:self.mediatedAd];
}

- (BOOL)logMode {
    return YES;
}

- (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(nonnull NSError *)error {
    
}

- (void)adLoader:(nonnull GADAdLoader *)adLoader didReceiveNativeAd:(nonnull GADNativeAd *)nativeAd {
  
  
}

@end
