//
//  SMAAdMobInterstitialAdapterViewController.m
//  sdk-demo-ios-objc
//
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

#import "SMAAdMobInterstitialAdapterViewController.h"
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <SmaatoSDKInterstitial/SmaatoSDKInterstitial.h>
#import <SMAAdMobSmaatoInterstitialAdapter.h>

NSString *const AdMobInterstitialAdsTip_TryLoad = @"Tap ‘Load’ to load an interstitial";
NSString *const AdMobInterstitialTip_Loading = @"Interstitial is loading…";
NSString *const AdMobInterstitialTip_Loaded = @"Interstitial was loaded successfully";
NSString *const AdMobInterstitialAdsTip_Clicked = @"Interstitial has been clicked";
NSString *const AdMobInterstitialAdsTip_Error = @"Something failed. See console logs";

@interface SMAAdMobInterstitialAdapterViewController () <GADInterstitialDelegate>

@property(nonatomic) GADInterstitial *interstitial;

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

@property(nonatomic, weak) IBOutlet UILabel *interstitialDidReceiveAdLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidFailToReceiveLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialWillPresentScreenLabel;

@property(nonatomic, weak) IBOutlet UILabel *interstitialWillDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialWillLeaveAppLabel;
@property(nonatomic, weak) IBOutlet UILabel *interstitialDidFailToPresentLabel;

@end

@implementation SMAAdMobInterstitialAdapterViewController

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

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

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    self.adBlock.tip = AdMobInterstitialTip_Loading;
    self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:self.adMobModel.adUnitId];
    self.interstitial.delegate = self;
    GADRequest *request = [GADRequest request];
    [self.interstitial loadRequest:request];
}

- (IBAction)showButtonTapped:(id)sender
{
    [self.showButoon setEnabled:NO];
    if (self.interstitial.isReady) {
        [self.interstitial presentFromRootViewController:self];
    }
}

- (void)setupUI
{
    self.title = @"AdMob Interstitial";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adUnitIDLabel.text = self.adMobModel.adUnitId;
    self.adBlock.tip = AdMobInterstitialAdsTip_TryLoad;

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

- (void)resetLabels
{
    self.interstitialDidReceiveAdLabel.alpha = 0.2;
    self.interstitialDidFailToReceiveLabel.alpha = 0.2;
    self.interstitialWillPresentScreenLabel.alpha = 0.2;
    self.interstitialWillDismissScreenLabel.alpha = 0.2;
    self.interstitialDidDismissScreenLabel.alpha = 0.2;
    self.interstitialWillLeaveAppLabel.alpha = 0.2;
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    [self.showButoon setEnabled:YES];
    self.adBlock.tip = AdMobInterstitialTip_Loaded;
    self.interstitialDidReceiveAdLabel.alpha = 1;
    self.adBlock.tip = AdMobInterstitialTip_Loaded;
}

- (void)interstitialWillPresentScreen:(GADInterstitial *)ad
{
    self.interstitialWillPresentScreenLabel.alpha = 1;
}

- (void)interstitialDidFailToPresentScreen:(GADInterstitial *)ad
{
    self.interstitialDidFailToPresentLabel.alpha = 1;
}

- (void)interstitialWillDismissScreen:(GADInterstitial *)ad
{
    self.interstitialWillDismissScreenLabel.alpha = 1;
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)ad
{
    self.interstitialDidDismissScreenLabel.alpha = 1;
}

- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad
{
    self.interstitialWillLeaveAppLabel.alpha = 1;
}

- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
    self.adBlock.tip = AdMobInterstitialAdsTip_Error;
    self.interstitialDidFailToReceiveLabel.alpha = 1;
}

@end
