#import "AdMobMrecBannerViewController.h"
#import "AdViewDemoMacros.h"

NSString * const storedImpMrecBannerAdMob = @"POSIDcsts715vz17x";
NSString * const adMobAdUnitMrecBannerRendering = @"ca-app-pub-7283949697136772/3397053982";

@interface AdMobMrecBannerViewController ()

@property (nonatomic) ADVIEW_MediationBannerAdUnit * adviewAdMobMediaitonAdUnit;
@property (nonatomic) AdViewAdMobMediationBannerUtils * mediationDelegate;
@property (nonatomic) GADBannerView * gadBanner;

@end

@implementation AdMobMrecBannerViewController

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

- (void)createAd {
    GADRequest * gadRequest = [GADRequest new];
    [AdViewAdMobUtils setConfigIdOnRequest:storedImpMrecBannerAdMob request:gadRequest];

    self.gadBanner = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(self.adSize)];
    self.gadBanner.adUnitID = adMobAdUnitMrecBannerRendering;
    self.gadBanner.delegate = self;
    self.gadBanner.rootViewController = self;

    self.bannerView.backgroundColor = [UIColor clearColor];
    [self.bannerView addSubview:self.gadBanner];

    self.mediationDelegate = [[AdViewAdMobMediationBannerUtils alloc] initWithGadRequest:gadRequest bannerView:self.gadBanner];

    self.adviewAdMobMediaitonAdUnit = [[ADVIEW_MediationBannerAdUnit alloc] initWithConfigID:storedImpMrecBannerAdMob size:self.adSize mediationDelegate:self.mediationDelegate];

    self.adviewAdMobMediaitonAdUnit.adFormat = ADVIEW_AdFormat.banner;

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

        [self.gadBanner loadRequest:gadRequest];
    }];
}

- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
}

- (void)bannerView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(NSError *)error {
    AdViewDemoLogError(@"%@", error.localizedDescription);
    [self.adviewAdMobMediaitonAdUnit adObjectDidFailToLoadAdWithAdObject:self.gadBanner with:error];
}

@end
