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

#import "SMAUbMoPubInterstitialViewController.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoInterstitialAdapter.h>
#import <SmaatoSDKUnifiedBidding/SmaatoSDKUnifiedBidding.h>

NSString *const UbMoPubInterstitialAdsTip_TryLoad = @"Tap ‘Load’ to load an interstitial";
NSString *const UbMoPubInterstitialAdsTip_Loading = @"Interstitial is loading…";
NSString *const UbMoPubInterstitialAdsTip_Loaded = @"Interstitial is loaded successfully";
NSString *const UbMoPubInterstitialAdsTip_Clicked = @"Interstitial clicked";
NSString *const UbMoPubInterstitialAdsTip_Error = @"Something failed. See console logs";
NSString *const UbMoPubInterstitialAdsTip_LoadNew = @"Tap ‘Load’ again to load a new interstitial ad. The old interstitial could not be shown a second time";

@interface SMAUbMoPubInterstitialViewController () <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 SMAUbMoPubInterstitialViewController

- (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 = UbMoPubInterstitialAdsTip_Loading;
    [self loadAd];
}

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

- (void)setupUI
{
    self.title = @"MoPub Interstitial";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adBlock.tip = UbMoPubInterstitialAdsTip_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)loadAd
{
    __weak typeof(self) weakSelf = self;
    [SmaatoSDK prebidInterstitialForAdSpaceId:self.mopubModel.smaatoAdspaceId
                                   completion:^(SMAUbBid *_Nullable bid, NSError *_Nullable error) {
                                       __strong typeof(self) self = weakSelf;
                                       if (bid) {
                                           NSString *bidKeyword = bid.mopubPrebidKeyword;
                                           self.interstitial.keywords = bidKeyword;
                                           self.interstitial.localExtras = bid.metaData;
                                       } else {
                                           NSLog(@"SmaatoSDK PreBid error: %@", error);
                                       }
                                       
                                       [self.interstitial loadAd];
                                   }];
}

- (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 setEnabled:YES];
    self.interstitialDidLoadLabel.alpha = 1;
    self.adBlock.tip = UbMoPubInterstitialAdsTip_Loaded;
}

- (void)interstitialDidFailToLoadAd:(MPInterstitialAdController *)interstitial
{
    self.adBlock.tip = UbMoPubInterstitialAdsTip_Error;
    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
