#import "AdMobNativeViewController.h"
#import "AdViewDemoMacros.h"

NSString * const nativeStoredImpressionAdMob = @"POSID06zhclyweype";
NSString * const admobRenderingNativeAdUnitId = @"ca-app-pub-7283949697136772/5773334681";

@interface AdMobNativeViewController ()

@property (nonatomic) AdViewAdMobMediationNativeUtils * mediationDelegate;
@property (nonatomic) ADVIEW_MediationNativeAdUnit * admobMediationNativeAdUnit;
@property (nonatomic) GADAdLoader * adLoader;
@property (nonatomic) GADNativeAdView * gadNativeAdView;

@end

@implementation AdMobNativeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self applyFeedNativeLayout];
    [self setupNativeAdViewContainer];
    [self createAd];
}

- (void)loadView {
    [super loadView];
}

- (void)setupNativeAdViewContainer {
    self.gadNativeAdView = [GADNativeAdView new];
    self.gadNativeAdView.translatesAutoresizingMaskIntoConstraints = NO;
    self.gadNativeAdView.backgroundColor = UIColor.clearColor;
    [self.view insertSubview:self.gadNativeAdView atIndex:0];
    [NSLayoutConstraint activateConstraints:@[
        [self.gadNativeAdView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
        [self.gadNativeAdView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.gadNativeAdView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.gadNativeAdView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
    ]];
}

- (void)createAd {
    GADRequest * gadRequest = [GADRequest new];

    self.mediationDelegate = [[AdViewAdMobMediationNativeUtils alloc] initWithGadRequest:gadRequest];

    self.admobMediationNativeAdUnit = [[ADVIEW_MediationNativeAdUnit alloc] initWithConfigId:nativeStoredImpressionAdMob mediationDelegate:self.mediationDelegate];

    [self.admobMediationNativeAdUnit setContextType:ADVIEW_ContextType.Social];
    [self.admobMediationNativeAdUnit setPlacementType:ADVIEW_PlacementType.FeedContent];
    [self.admobMediationNativeAdUnit setContextSubType:ADVIEW_ContextSubType.Social];

    @weakify(self);
    [self.admobMediationNativeAdUnit fetchDemandWithCompletion:^(enum ADVIEW_ResultCode resultCode) {
        @strongify(self);
        if (!self) { return; }

        self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:admobRenderingNativeAdUnitId
                                           rootViewController:self
                                                      adTypes:@[GADAdLoaderAdTypeNative]
                                                      options:@[]];
        self.adLoader.delegate = self;
        [self.adLoader loadRequest:gadRequest];
    }];
}

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
    nativeAd.delegate = self;

    self.titleLable.text = nativeAd.headline;
    self.bodyLabel.text = nativeAd.body;
    [self updateCallToActionButtonTitle:nativeAd.callToAction];
    self.sponsoredLabel.text = nativeAd.advertiser;

    self.iconView.image = nativeAd.icon.image;
    self.mainImageView.image = nativeAd.images.firstObject.image;

    [self registerNativeAdForClicks:nativeAd];
}

- (void)registerNativeAdForClicks:(GADNativeAd *)nativeAd {
    self.gadNativeAdView.headlineView = self.titleLable;
    self.gadNativeAdView.bodyView = self.bodyLabel;
    self.gadNativeAdView.iconView = self.iconView;
    self.gadNativeAdView.callToActionView = self.callToActionButton;
    self.gadNativeAdView.advertiserView = self.sponsoredLabel;
    self.gadNativeAdView.imageView = self.mainImageView;

    nativeAd.rootViewController = self;
    self.gadNativeAdView.nativeAd = nativeAd;
}

- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(NSError *)error {
    AdViewDemoLogError(@"%@", error.localizedDescription);
}

- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
}

@end
