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

#import <MoPub/MoPub.h>
#import "SMAMoPubInterstitialAdapterViewController.h"
#import <SMAMoPubSmaatoInterstitialAdapter.h>

NSString *const MoPubInterstitialAdsTip_TryLoad = @"Tap ‘Load’ to load an interstitial";
NSString *const MoPubInterstitialAdsTip_Loading = @"Interstitial is loading…";
NSString *const MoPubInterstitialAdsTip_Loaded = @"Interstitial is loaded successfully";
NSString *const MoPubInterstitialAdsTip_Clicked = @"Interstitial clicked";
NSString *const MoPubInterstitialAdsTip_Error = @"Something failed. See console logs";
NSString *const MoPubInterstitialAdsTip_LoadNew = @"Tap ‘Load’ again to load a new interstitial ad. The old interstitial could not be shown a second time";

@interface SMAMoPubInterstitialAdapterViewController () <MPInterstitialAdControllerDelegate>

@property(nonatomic, strong) MPInterstitialAdController *interstitial;

@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet UILabel *adUnitIDLabel;
@property(nonatomic, weak) IBOutlet UIButton *showButton;
@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;
@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;

@property(nonatomic, weak) IBOutlet UILabel *interstitialDidLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialWillAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialWillDisappearLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidDisappearLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidReceiveTapEventLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidExpireLabel;

@end

@implementation SMAMoPubInterstitialAdapterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];

    self.interstitial = [MPInterstitialAdController
            interstitialAdControllerForAdUnitId:self.mopubModel.adUnitId];
    self.interstitial.delegate = self;
}

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    self.adBlock.tip = MoPubInterstitialAdsTip_Loading;
    [self.interstitial loadAd];
}

- (IBAction)showButtonTapped:(id)sender
{
    self.adBlock.tip = MoPubInterstitialAdsTip_LoadNew;
    self.showButton.enabled = NO;
    if (self.interstitial.ready) {
        [self.interstitial showFromViewController:self];
    }
}

- (void)setupUI
{
    self.title = @"MoPub Interstitial";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adBlock.tip = MoPubInterstitialAdsTip_TryLoad;
    self.adUnitIDLabel.text = self.mopubModel.adUnitId;

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

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

- (void)resetLabels
{
    self.interstitialDidLoadLabel.alpha = 0.2;
    self.interstitialDidFailLabel.alpha = 0.2;
    self.interstitialWillAppearLabel.alpha = 0.2;
    self.interstitialDidAppearLabel.alpha = 0.2;
    self.interstitialWillDisappearLabel.alpha = 0.2;
    self.interstitialDidDisappearLabel.alpha = 0.2;
    self.interstitialDidReceiveTapEventLabel.alpha = 0.2;
    self.interstitialDidExpireLabel.alpha = 0.2;
}

#pragma mark MPInterstitialAdControllerDelegate

- (void)interstitialDidLoadAd:(MPInterstitialAdController *)interstitial
{
    [self resetLabels];
    self.showButton.enabled = YES;
    self.interstitialDidLoadLabel.alpha = 1;
    self.adBlock.tip = MoPubInterstitialAdsTip_Loaded;
}

- (void)interstitialDidFailToLoadAd:(MPInterstitialAdController *)interstitial
{
    self.interstitialDidFailLabel.alpha = 1;
}

- (void)interstitialWillAppear:(MPInterstitialAdController *)interstitial
{
    self.interstitialWillAppearLabel.alpha = 1;
}

- (void)interstitialDidAppear:(MPInterstitialAdController *)interstitial
{
    self.interstitialDidAppearLabel.alpha = 1;
}

- (void)interstitialWillDisappear:(MPInterstitialAdController *)interstitial
{
    self.interstitialWillDisappearLabel.alpha = 1;
}

- (void)interstitialDidDisappear:(MPInterstitialAdController *)interstitial
{
    self.interstitialDidDisappearLabel.alpha = 1;
}

- (void)interstitialDidExpire:(MPInterstitialAdController *)interstitial
{
    self.interstitialDidExpireLabel.alpha = 1;
}

- (void)interstitialDidReceiveTapEvent:(MPInterstitialAdController *)interstitial
{
    self.interstitialDidReceiveTapEventLabel.alpha = 1;
}

@end
