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

#import "SMAHbMoPubInterstitialViewController.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoInterstitialAdapter.h>
@import SmaatoSDKHeaderBidding;

NSString *const HbMoPubInterstitialAdsTip_TryLoad = @"Tap ‘Load’ to load an interstitial";
NSString *const HbMoPubInterstitialAdsTip_Loading = @"Interstitial is loading…";
NSString *const HbMoPubInterstitialAdsTip_Loaded = @"Interstitial is loaded successfully";
NSString *const HbMoPubInterstitialAdsTip_Clicked = @"Interstitial clicked";
NSString *const HbMoPubInterstitialAdsTip_Error = @"Something failed. See console logs";
NSString *const HbMoPubInterstitialAdsTip_LoadNew = @"Tap ‘Load’ again to load a new interstitial ad. The old interstitial could not be shown a second time";

NSString *const kHeaderBiddingInterstitialAdSpaceId = @"130560861";

@interface SMAHbMoPubInterstitialViewController ()<MPInterstitialAdControllerDelegate>

@property (nonatomic, strong) MPInterstitialAdController *interstitial;

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

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

@end

@implementation SMAHbMoPubInterstitialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    self.interstitial = [MPInterstitialAdController
                         interstitialAdControllerForAdUnitId:self.mopubModel.adSpaceId];
    self.interstitial.delegate = self;
}

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

- (IBAction)showButtonTapped:(id)sender {
    self.adBlock.tip = HbMoPubInterstitialAdsTip_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 = HbMoPubInterstitialAdsTip_TryLoad;
    self.adUnitIDLabel.text = self.mopubModel.adSpaceId;
    
    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 SMAHbMoPubInterstitialViewController *weakSelf = self;
    [SmaatoSDK applyPreBidKeyForAdObject:self.interstitial adSpaceId:kHeaderBiddingInterstitialAdSpaceId completion:^(BOOL success, NSError *error) {
        if (error) {
            NSLog(@"SmaatoSDK PreBid error: %@", error);
        }
        
        [weakSelf.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 = HbMoPubInterstitialAdsTip_Loaded;
}

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