#import "MAXDisplayBannerViewController.h"
#import "AdViewDemoMacros.h"

NSString * const storedImpDisplayBannerMAX = @"POSIDjw2dypnd7yv8";
NSString * const maxAdUnitBannerRendering = @"e9ed8811b9c85332";

@interface MAXDisplayBannerViewController ()

// adview
@property (nonatomic) ADVIEW_MediationBannerAdUnit * maxAdUnit;
@property (nonatomic) AdViewMAXMediationBannerUtils * maxMediationDelegate;

// MAX
@property (nonatomic) MAAdView * maxAdBannerView;

@end

@implementation MAXDisplayBannerViewController

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

- (void)createAd {
    // 1. Create a MAAdView
    self.maxAdBannerView = [[MAAdView alloc] initWithAdUnitIdentifier:maxAdUnitBannerRendering];
    
    // 2. Configure the MAAdView
    self.maxAdBannerView.frame = CGRectMake(0, 0, self.adSize.width, self.adSize.height);
    self.maxAdBannerView.delegate = self;
    self.maxAdBannerView.revenueDelegate = self;
    [self.maxAdBannerView setHidden:NO];

    // Keep MAX adUnitIdentifier <-> AdViewconfigId mapping for adapter refresh fallback.
    [[AdViewMAXBannerConfigIdBridge shared] registerWithAdUnitIdentifier:maxAdUnitBannerRendering configId:storedImpDisplayBannerMAX];
    
    // Add AppLovin SDK banner view to the app UI
    [self.bannerView addSubview:self.maxAdBannerView];
    
    // 3. Setup a AdViewMAXMediationBannerUtils
    self.maxMediationDelegate = [[AdViewMAXMediationBannerUtils alloc] initWithAdView:self.maxAdBannerView];
    
    // 4. Setup a ADVIEW_MediationBannerAdUnit
    self.maxAdUnit = [[ADVIEW_MediationBannerAdUnit alloc] initWithConfigID:storedImpDisplayBannerMAX size:self.adSize mediationDelegate:self.maxMediationDelegate];
    
    // 5. Make a bid request to AdView Server
    @weakify(self);
    [self.maxAdUnit fetchDemandWithCompletion:^(enum ADVIEW_ResultCode resultCode) {
        @strongify(self);
        if (!self) { return; }
        
        // 6. Load the banner ad
        [self.maxAdBannerView loadAd];
    }];
}

// MARK: - MAAdViewAdDelegate

- (void)didLoadAd:(MAAd *)ad {
}

- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {
    AdViewDemoLogError(@"%@", error.message);
    
    NSError * maxError = [[NSError alloc] initWithDomain:@"MAX" code:error.code userInfo:@{NSLocalizedDescriptionKey: error.message}];
    [self.maxAdUnit adObjectDidFailToLoadAdWithAdObject:self.maxAdBannerView with:maxError];
}

- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {
    AdViewDemoLogError(@"%@", error.message);
    
    NSError * maxError = [[NSError alloc] initWithDomain:@"MAX" code:error.code userInfo:@{NSLocalizedDescriptionKey: error.message}];
    [self.maxAdUnit adObjectDidFailToLoadAdWithAdObject:self.maxAdBannerView with:maxError];
}

- (void)didDisplayAd:(MAAd *)ad {
}

- (void)didHideAd:(MAAd *)ad {
}

- (void)didExpandAd:(MAAd *)ad {
}

- (void)didCollapseAd:(MAAd *)ad {
}

- (void)didClickAd:(MAAd *)ad {
}

// MARK: - MAAdRevenueDelegate

- (void)didPayRevenueForAd:(MAAd *)ad {
    AdViewDemoLogError(@"MAX Banner didPayRevenue · revenue=%f · precision=%@ · network=%@ · adUnit=%@",
                       ad.revenue, ad.revenuePrecision, ad.networkName, ad.adUnitIdentifier);
}

@end
