//
//  SMAInterstitialBannerViewController.m
//  sdk-demo-ios-objc
//
//  Created by Smaato Inc on 16.11.18.
//  Copyright © 2018 Smaato Inc. All rights reserved.
//

#import "SMAInterstitialBannerViewController.h"
#import <SmaatoSDKInterstitial/SmaatoSDKInterstitial.h>

NSString *const SMAInterstitialAdsTip_LoadNew = @"Tap ‘Load’ again to load a new interstitial ad. The old interstitial could not be shown a second time";
NSString *const SMAInterstitialAdsTip_TryLoadTip = @"Tap ‘Load’ to load an interstitial ad";
NSString *const SMAInterstitialAdsTip_LoadingTip = @"Interstitial Ad is loading…";
NSString *const SMAInterstitialAdsTip_LoadedTip = @"Interstitial Ad was loaded successfully";
NSString *const SMAInterstitialAdsTip_ErrorTip = @"Something failed. See console logs";
NSString *const SMAInterstitialAdsTip_ExpireTip = @"The loaded interstitial is expired. Tap ‘Load’ to load a new interstitial ad.";

@interface SMAInterstitialBannerViewController () <SMAInterstitialDelegate>

@property(nonatomic) SMAInterstitial *interstitial;

@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet UIButton *buttonShow;
@property(nonatomic, weak) IBOutlet UILabel *publisherIdLabel;
@property(nonatomic, weak) IBOutlet UILabel *adspaceIdLabel;
@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;

@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 *interstitialDidClickLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidTTLExpireLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialWillLeaveApplicationLabel;

@end

@implementation SMAInterstitialBannerViewController

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

#pragma mark - Load ads

- (void)loadInterstitialAd
{
    [self resetLabels];
    self.interstitial = nil;

    self.adBlock.tip = SMAInterstitialAdsTip_LoadingTip;

    //Send load interstitial request
    [SmaatoSDK loadInterstitialForAdSpaceId:self.adSpaceId
                                     delegate:self];
}

- (void)showInterstitialAd
{
    // Try to show interstitial ad, if it has been already downloaded and is not expired (TTL value)
    if (self.interstitial.availableForPresentation) {
        [self.interstitial showFromViewController:self];
    }
}

- (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.interstitialDidClickLabel.alpha = 0.2;
    self.interstitialDidTTLExpireLabel.alpha = 0.2;
    self.interstitialWillLeaveApplicationLabel.alpha = 0.2;
}

- (void)setupUI
{
    self.adBlock.tip = SMAInterstitialAdsTip_TryLoadTip;
    self.publisherIdLabel.text = SmaatoSDK.publisherId;
    self.adspaceIdLabel.text = self.adSpaceId;

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

#pragma mark - Actions

- (IBAction)btnLoadTapped:(UIButton *)sender
{
    [self loadInterstitialAd];
}

- (IBAction)btnShowTapped:(UIButton *)sender
{
    [self showInterstitialAd];
}

#pragma mark - SMAInterstitialDelegate
#pragma mark - Required methods

- (void)interstitialDidLoad:(nonnull SMAInterstitial *)interstitial
{
    [self resetLabels];
    self.interstitial = interstitial;
    self.adBlock.tip = SMAInterstitialAdsTip_LoadedTip;
    [self.buttonShow setEnabled:YES];
    self.interstitialDidLoadLabel.alpha = 1;
}

- (void)interstitial:(nullable SMAInterstitial *)interstitial didFailWithError:(nonnull NSError *)error
{
    NSLog(@"Interstitial loading error: %@", error.localizedDescription);
    self.adBlock.tip = SMAInterstitialAdsTip_ErrorTip;
    self.interstitialDidFailLabel.alpha = 1;
}

- (void)interstitialDidTTLExpire:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial did TTL expire");
    [self.buttonShow setEnabled:NO];
    self.adBlock.tip = SMAInterstitialAdsTip_ExpireTip;
    self.interstitialDidTTLExpireLabel.alpha = 1;
}

#pragma mark - Optional methods

- (void)interstitialWillAppear:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial will appear");
    self.interstitialWillAppearLabel.alpha = 1;
}

- (void)interstitialDidAppear:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial did appear");
    self.interstitialDidAppearLabel.alpha = 1;
}

- (void)interstitialWillDisappear:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial will disappear");
    self.interstitialWillDisappearLabel.alpha = 1;
    self.adBlock.tip = SMAInterstitialAdsTip_LoadNew;
}

- (void)interstitialDidDisappear:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial did disappear");
    self.interstitialDidDisappearLabel.alpha = 1;
    [self.buttonShow setEnabled:NO];
}

- (void)interstitialDidClick:(nonnull SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial did click");
    self.interstitialDidClickLabel.alpha = 1;
}

- (void)interstitialWillLeaveApplication:(SMAInterstitial *)interstitial
{
    NSLog(@"Interstitial did click");
    self.interstitialWillLeaveApplicationLabel.alpha = 1;
}

@end
