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

#import "SMAMoPubBannerAdapterViewController.h"
#import "MPAdView.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoBannerAdapter.h>

NSString *const MoPubBannerAdsTip_TryLoad = @"Tap ‘Load’ to load a banner";
NSString *const MoPubBannerAdsTip_Loading = @"Banner is loading…";
NSString *const MoPubBannerAdsTip_Loaded = @"Banner was loaded successfully";
NSString *const MoPubBannerAdsTip_Clicked = @"Banner has been clicked";
NSString *const MoPubBannerAdsTip_Error = @"Something failed. See console logs";

@interface SMAMoPubBannerAdapterViewController () <MPAdViewDelegate>

@property(nonatomic, strong) MPAdView *adView;
@property(nonatomic, weak) IBOutlet UISwitch *autoreloadSwitch;
@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;
@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;

@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 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 SMAMoPubBannerAdapterViewController

- (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 = MoPubBannerAdsTip_Loading;
    [self.adView 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 = MoPubBannerAdsTip_TryLoad;
    self.adspaceIdLabel.text = self.mopubModel.adSpaceId;
    [self updateAdBlockSize];

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

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

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