//
//  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;

NSString *const HbMoPubBannerAdsTip_TryLoad = @"Tap ‘Load’ to load a banner";
NSString *const HbMoPubBannerAdsTip_Loading = @"Banner is loading…";
NSString *const HbMoPubBannerAdsTip_Loaded = @"Banner was loaded successfully";
NSString *const HbMoPubBannerAdsTip_Clicked = @"Banner has been clicked";
NSString *const HbMoPubBannerAdsTip_Error = @"Something failed. See console logs";

NSString *const kHeaderBiddingBannerAdSpaceId = @"130563103";

@interface SMAUbMoPubBannerViewController () <MPAdViewDelegate>

@property(nonatomic, strong) MPAdView *adView;
@property(weak, nonatomic) IBOutlet UISwitch *autoreloadSwitch;
@property(weak, nonatomic) 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(weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property(weak, nonatomic) IBOutlet UILabel *bannerDidLoadLabel;
@property(weak, nonatomic) IBOutlet UILabel *bannerDidFailLabel;
@property(weak, nonatomic) IBOutlet UILabel *bannerWillPresentModalContentLabel;
@property(weak, nonatomic) IBOutlet UILabel *bannerDidDismissModalContentLabel;
@property(weak, nonatomic) IBOutlet UILabel *bannerWillLeaveAppLabel;

@end

@implementation SMAUbMoPubBannerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

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

- (IBAction)autoUpdateSwitch:(UISwitch *)sender
{
    if (sender.isOn) {
        [self.adView startAutomaticallyRefreshingContents];
    } else {
        [self.adView stopAutomaticallyRefreshingContents];
    }
}

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

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

- (void)loadAd
{
    __weak SMAUbMoPubBannerViewController *weakSelf = self;
    [SmaatoSDK applyPreBidKeyForAdObject:self.adView adSpaceId:kHeaderBiddingBannerAdSpaceId completion:^(BOOL success, NSError *error) {
        if (error) {
            NSLog(@"SmaatoSDK PreBid error: %@", error);
        }

        [weakSelf.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 MoPubBannerSizesWideSkyscrapperSize:
            return MOPUB_WIDE_SKYSCRAPER_SIZE;
    }
}

- (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 = HbMoPubBannerAdsTip_Loaded;
    self.bannerDidLoadLabel.alpha = 1;
}

- (void)adViewDidFailToLoadAd:(MPAdView *)view
{
    self.adBlock.tip = HbMoPubBannerAdsTip_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
