//
//  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 () <GADFullScreenContentDelegate>

@property(nonatomic) GADInterstitialAd *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;
    
    [GADInterstitialAd loadWithAdUnitID:self.adMobModel.adUnitId request:[GADRequest request] completionHandler:^(GADInterstitialAd * _Nullable interstitialAd, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%s%@", "Failed to load interstitial ad with error: ", error.localizedDescription);
            return;
        }
        self.interstitial = interstitialAd;
        self.interstitial.fullScreenContentDelegate = self;
        
        [self resetLabels];
        [self.showButoon setEnabled:YES];
        self.adBlock.tip = AdMobInterstitialTip_Loaded;
        self.interstitialDidReceiveAdLabel.alpha = 1;
    }];
}

- (IBAction)showButtonTapped:(id)sender
{
    if (self.interstitial) {
        [self.interstitial presentFromRootViewController:self];
    } else {
        NSLog(@"%s error=%s", __func__, "An interstitial is not ready yet");
        return;
    }
    [self.showButoon setEnabled:NO];
}

- (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;
}

/// Tells the delegate that an impression has been recorded for the ad.
- (void)adDidRecordImpression:(nonnull id<GADFullScreenPresentingAd>)ad
{
    NSLog(@"%s", __func__);
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    self.adBlock.tip = AdMobInterstitialAdsTip_Error;
    self.interstitialDidFailToReceiveLabel.alpha = 1;
    NSLog(@"%s error=%@", __func__, error.localizedDescription);
}

/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.interstitialWillPresentScreenLabel.alpha = 1;
    NSLog(@"%s", __func__);
}

/// Tells the delegate that the ad will dismiss full screen content.
- (void)adWillDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.interstitialWillDismissScreenLabel.alpha = 1;
    NSLog(@"%s", __func__);
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.interstitialDidDismissScreenLabel.alpha = 1;
    NSLog(@"%s", __func__);
}

@end
