//
//  SMAUbMoPubBannerViewController.m
//  sdk-demo-ios-objc
//
//  Created by Smaato Inc. on 03/03/2019.
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

#import "SMAUbMoPubBannerViewController.h"
#import "MPAdView.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoBannerAdapter.h>
#import <SmaatoSDKUnifiedBidding/SmaatoSDKUnifiedBidding.h>

NSString *const UbMoPubBannerAdsTip_TryLoad = @"Tap ‘Load’ to load a banner";
NSString *const UbMoPubBannerAdsTip_Loading = @"Banner is loading…";
NSString *const UbMoPubBannerAdsTip_Loaded = @"Banner was loaded successfully";
NSString *const UbMoPubBannerAdsTip_Clicked = @"Banner has been clicked";
NSString *const UbMoPubBannerAdsTip_Error = @"Something failed. See console logs";

@interface SMAUbMoPubBannerViewController () <MPAdViewDelegate>

@property(nonatomic) MPAdView *adView;

@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;
@property(nonatomic, weak) IBOutlet UIButton *buttonShow;
@property(nonatomic, weak) IBOutlet UILabel *adspaceIdLabel;
@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlockWidthConstraint;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlockHeightConstraint;
@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillPresentModalContentLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidDismissModalContentLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillLeaveAppLabel;

@end

@implementation SMAUbMoPubBannerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupUI];
    self.adView = [[MPAdView alloc] initWithAdUnitId:self.mopubModel.adUnitId
                                                size:[self moPubBannerSize]];
    self.adView.delegate = self;
    
    [self.adView stopAutomaticallyRefreshingContents];
}

- (IBAction)loadButtonTapped:(UIButton *)sender
{
    [self resetLabels];
    [self.buttonShow setEnabled:NO];
    [self.adView removeFromSuperview];;
    self.adBlock.tip = UbMoPubBannerAdsTip_Loading;
    [self loadAd];
}

- (IBAction)showButtonTapped:(UIButton *)sender
{
    [self.buttonShow setEnabled:NO];
    [self.adBlock addSubview:self.adView];
}

- (void)setupUI
{
    self.title = @"MoPub banner";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adBlock.tip = UbMoPubBannerAdsTip_TryLoad;
    self.adspaceIdLabel.text = self.mopubModel.adUnitId;
    [self updateAdBlockSize];

    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)loadAd
{
    __weak typeof(self) weakSelf = self;
    [SmaatoSDK prebidBannerForAdSpaceId:self.mopubModel.smaatoAdspaceId
                             bannerSize:[self ubSmaatoSize]
                             completion:^(SMAUbBid *_Nullable bid, NSError *_Nullable error) {
                                 __strong typeof(self) self = weakSelf;
                                 if (bid) {
                                     NSString *bidKeyword = bid.mopubPrebidKeyword;
                                     self.adView.keywords = bidKeyword;
                                 } else {
                                     NSLog(@"SmaatoSDK PreBid error: %@", error);
                                 }
                                 
                                 [self.adView loadAd];
                             }];
}

- (NSString *)adapterVersion
{
    return [NSString stringWithFormat:@"Adapter version %@", SMAMoPubSmaatoBannerAdapter.version];
}

- (CGSize)moPubBannerSize
{
    switch (self.mopubModel.moPubsize) {
        case MoPubBannerSizesBannerSize:
            return MOPUB_BANNER_SIZE;
        case MoPubBannerSizesMedRectSize:
            return MOPUB_MEDIUM_RECT_SIZE;
        case MoPubBannerSizesLeaderBoardSize:
            return MOPUB_LEADERBOARD_SIZE;
        case MoPubBannerSizesWideSkyscraperSize:
            return MOPUB_WIDE_SKYSCRAPER_SIZE;
    }
}

- (SMAUbBannerSize)ubSmaatoSize
{
    switch (self.mopubModel.moPubsize) {
        case MoPubBannerSizesBannerSize:
            return kSMAUbBannerSizeXXLarge_320x50;
        case MoPubBannerSizesMedRectSize:
            return kSMAUbBannerSizeMediumRectangle_300x250;
        case MoPubBannerSizesLeaderBoardSize:
            return kSMAUbBannerSizeLeaderboard_728x90;
        case MoPubBannerSizesWideSkyscraperSize:
            return kSMAUbBannerSizeSkyscraper_120x600;
    }
}

- (void)updateAdBlockSize
{
    self.adBlockWidthConstraint.constant = [self moPubBannerSize].width;
    self.adBlockHeightConstraint.constant = [self moPubBannerSize].height;
}

- (void)resetLabels
{
    self.bannerDidLoadLabel.alpha = 0.2;
    self.bannerDidFailLabel.alpha = 0.2;
    self.bannerWillPresentModalContentLabel.alpha = 0.2;
    self.bannerDidDismissModalContentLabel.alpha = 0.2;
    self.bannerWillLeaveAppLabel.alpha = 0.2;
}

#pragma mark - <MPAdViewDelegate>

- (UIViewController *)viewControllerForPresentingModalView
{
    return self;
}

- (void)adViewDidLoadAd:(MPAdView *)view
{
    [self resetLabels];
    [self.buttonShow setEnabled:YES];
    self.adBlock.tip = UbMoPubBannerAdsTip_Loaded;
    self.bannerDidLoadLabel.alpha = 1;
}

- (void)adViewDidFailToLoadAd:(MPAdView *)view
{
    self.adBlock.tip = UbMoPubBannerAdsTip_Error;
    self.bannerDidFailLabel.alpha = 1;
}

- (void)willPresentModalViewForAd:(MPAdView *)view
{
    self.bannerWillPresentModalContentLabel.alpha = 1;
}

- (void)didDismissModalViewForAd:(MPAdView *)view
{
    self.bannerDidDismissModalContentLabel.alpha = 1;
}

- (void)willLeaveApplicationFromAd:(MPAdView *)view
{
    self.bannerWillLeaveAppLabel.alpha = 1;
}

@end
