//
//  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 <MoPubSDK/MoPubSDK-Swift.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];
    self.adView.delegate = self;
    
    [self.adView stopAutomaticallyRefreshingContents];
}

- (IBAction)loadButtonTapped:(UIButton *)sender
{
    [self resetLabels];
    [self.buttonShow setEnabled:NO];
    [self.adView removeFromSuperview];
    CGSize size = [self moPubBannerSize];
    self.adView.frame = CGRectMake(0, 0, size.width, size.height);
    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) {
                                     self.adView.keywords = bid.mopubPrebidKeyword;
                                     self.adView.localExtras = bid.metaData;
                                 } 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 CGSizeMake(320, 50);
        case MoPubBannerSizesMedRectSize:
            return CGSizeMake(300, 250);
        case MoPubBannerSizesLeaderBoardSize:
            return CGSizeMake(728, 90);
    }
}

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

- (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 adSize:(CGSize)adSize
{
    [self resetLabels];
    [self.buttonShow setEnabled:YES];
    self.adBlock.tip = UbMoPubBannerAdsTip_Loaded;
    self.bannerDidLoadLabel.alpha = 1;
}

- (void)adViewDidFailToLoadAd:(MPAdView *)view adSize:(CGSize)adSize
{
    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
