//
//  AdViewMediatedNativeAd.m
//  admobTest
//
//  Created by hupan on 2022/3/16.
//

#import "AdViewMediatedNativeAd.h"

@interface AdViewMediatedNativeAd()

@property (nonatomic, strong)AdViewNativeAd *adViewNative;

@property (nonatomic, strong)NSArray<AdViewNativeData *> *nativeDataArray;

@end

@implementation AdViewMediatedNativeAd

- (nullable instancetype)initWithAdViewNativeAd:(AdViewNativeAd *)adViewNativeAd NativeData:(NSArray <AdViewNativeData *>*)nativeDataArray{
    if (!adViewNativeAd) {
      return nil;
    }

    self = [super init];
    if (self) {
        _adViewNative = adViewNativeAd;
        _nativeDataArray = nativeDataArray;
        
    }
    return self;
}

#pragma mark - Assets

- (NSString *)headline
{
    return [self.nativeDataArray.firstObject.adProperties objectForKey:@"title"];
}

- (NSString *)body
{
    return [self.nativeDataArray.firstObject.adProperties objectForKey:@"desc"];
}

- (GADNativeAdImage *)icon
{
    NSString * iconUrlStr = [self.nativeDataArray.firstObject.adProperties objectForKey:@"iconUrlString"];
    NSURL *iconUrl = [[NSURL alloc] initWithString:iconUrlStr];
    return [[GADNativeAdImage alloc] initWithURL:iconUrl scale:1];
}

- (NSArray<GADNativeAdImage *> *)images
{
    NSArray * imageArray = [self.nativeDataArray.firstObject.adProperties objectForKey:@"imageList"];
    NSMutableArray *images = [[NSMutableArray array] init];
    if (imageArray && imageArray.count) {
        for (NSString *imageURL in imageArray) {
            NSURL *url = [NSURL URLWithString:imageURL];
            [images addObject: [[GADNativeAdImage alloc] initWithURL:url scale:1]];
        }
    } else {
        NSString *imageString = [self.nativeDataArray.firstObject.adProperties objectForKey:@"imageUrlString"];
        NSURL *url = [NSURL URLWithString:imageString];
        [images addObject: [[GADNativeAdImage alloc] initWithURL:url scale:1]];
    }
    return images;
}

- (NSString *)callToAction
{
    return nil;
}

- (NSDecimalNumber *)starRating
{
    return nil;
}

- (NSString *)price
{
    return nil;
}

- (NSString *)store
{
    return nil;
}

- (NSDictionary<NSString *, id> *)extraAssets
{
    return nil;
}

- (NSString *)advertiser
{
    return nil;
}

- (UIView *)adChoicesView
{
    return nil;
}

- (GADMediaContent *)mediaContent{
    return nil;
}

- (void)didRenderInView:(nonnull UIView *)view
       clickableAssetViews:
           (nonnull NSDictionary<GADNativeAssetIdentifier, UIView *> *)clickableAssetViews
    nonclickableAssetViews:
        (nonnull NSDictionary<GADNativeAssetIdentifier, UIView *> *)nonclickableAssetViews
         viewController:(nonnull UIViewController *)viewController{
    NSMutableArray * friendlyObstructionArray = [NSMutableArray new];
    for (UIView * subView in view.subviews) {
        [friendlyObstructionArray addObject:subView];
    }
    [friendlyObstructionArray addObject:view];
    [self.adViewNative showNativeAdWithData:self.nativeDataArray.firstObject friendlyObstructionArray:friendlyObstructionArray onView:view];
}

- (void)didRecordImpression{
    [GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdDidRecordImpression:self];
}

- (void)didRecordClickOnAssetWithName:(nonnull GADNativeAssetIdentifier)assetName
                                 view:(nonnull UIView *)view
                       viewController:(nonnull UIViewController *)viewController{
    [self.adViewNative clickNativeAdWithData:self.nativeDataArray.firstObject withClickPoint:CGPointMake(50, 50) onView:view];
}


@end
