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

#import "SMAAdMobBannerAdapterViewController.h"
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <SMAAdMobSmaatoBannerAdapter.h>

NSString *const AdMobBannerAdsTip_TryLoad = @"Tap ‘Load’ to load a banner";
NSString *const AdMobBannerAdsTip_Loading = @"Banner is loading…";
NSString *const AdMobBannerAdsTip_Loaded = @"Banner was loaded successfully";
NSString *const AdMobBannerAdsTip_Clicked = @"Banner has been clicked";
NSString *const AdMobBannerAdsTip_Error = @"Something failed. See console logs";

@interface SMAAdMobBannerAdapterViewController () <GADBannerViewDelegate>

@property(nonatomic) GADBannerView *bannerView;
@property(nonatomic, weak) IBOutlet UIButton *showButton;
@property(nonatomic, weak) IBOutlet UILabel *adUnitIDLabel;
@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;

@property(nonatomic, weak) IBOutlet UILabel *bannerDidReceiveLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidFailToReceiveAdLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillPresentScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillLeaveAppLabel;

@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlockViewHeightConstraint;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlockViewWidthConstraint;
@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;

@end

@implementation SMAAdMobBannerAdapterViewController

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

    self.bannerView = [[GADBannerView alloc] initWithAdSize:[self adMobBannerSize]];
    self.bannerView.adUnitID = self.adMobModel.adUnitId;
    self.bannerView.rootViewController = self;
    self.bannerView.delegate = self;
}

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

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

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

- (GADAdSize)adMobBannerSize
{
    switch (self.adMobModel.adMobsize) {
        case AdMobBannerSizesBannerSize:
            return kGADAdSizeBanner;
        case AdMobBannerSizesLargeBannerSize:
            return kGADAdSizeLargeBanner;
        case AdMobBannerSizesMedRectSize:
            return kGADAdSizeMediumRectangle;
        case AdMobBannerSizesLeaderBoardSize:
            return kGADAdSizeLeaderboard;
        case AdMobBannerSizesSkyscraperSize:
            return kGADAdSizeSkyscraper;
    }
}

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    [self.showButton setEnabled:NO];
    self.adBlock.tip = AdMobBannerAdsTip_Loading;
    [self.bannerView removeFromSuperview];
    [self.bannerView loadRequest:[GADRequest request]];
}

- (IBAction)showButtonTapped:(id)sender
{
    [self.showButton setEnabled:NO];
    [self.adBlock addSubview:self.bannerView];
}

- (IBAction)autoUpdateSwitch:(UISwitch *)sender
{
    if (sender.isOn) {
        self.bannerView.autoloadEnabled = YES;
    } else {
        self.bannerView.autoloadEnabled = NO;
    }
}

- (void)updateAdBlockSize
{
    self.adBlockViewWidthConstraint.constant = [self adMobBannerSize].size.width;
    self.adBlockViewHeightConstraint.constant = [self adMobBannerSize].size.height;
}

- (void)resetLabels
{
    self.bannerDidReceiveLabel.alpha = 0.2;
    self.bannerDidFailToReceiveAdLabel.alpha = 0.2;
    self.bannerWillPresentScreenLabel.alpha = 0.2;
    self.bannerWillDismissScreenLabel.alpha = 0.2;
    self.bannerDidDismissScreenLabel.alpha = 0.2;
    self.bannerWillLeaveAppLabel.alpha = 0.2;
}

#pragma mark - GADBannerViewDelegate

- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
    self.adBlock.tip = AdMobBannerAdsTip_Loaded;
    self.bannerDidReceiveLabel.alpha = 1;
    [self.showButton setEnabled:YES];
}

/// Tells the delegate that an ad request failed. The failure is normally due to network
/// connectivity or ad availablility (i.e., no fill).
- (void)bannerView:(nonnull GADBannerView *)bannerView
    didFailToReceiveAdWithError:(nonnull NSError *)error {
    self.bannerDidFailToReceiveAdLabel.alpha = 1;
}

#pragma mark Click-Time Lifecycle Notifications

/// Tells the delegate that a full screen view will be presented in response to the user clicking on
/// an ad. The delegate may want to pause animations and time sensitive interactions.
- (void)bannerViewWillPresentScreen:(nonnull GADBannerView *)bannerView {
    self.bannerWillPresentScreenLabel.alpha = 1;
}

/// Tells the delegate that the full screen view will be dismissed.
- (void)bannerViewWillDismissScreen:(nonnull GADBannerView *)bannerView {
    self.bannerWillDismissScreenLabel.alpha = 1;
}

/// Tells the delegate that the full screen view has been dismissed. The delegate should restart
/// anything paused while handling bannerViewWillPresentScreen:.
- (void)bannerViewDidDismissScreen:(nonnull GADBannerView *)bannerView {
    self.bannerDidDismissScreenLabel.alpha = 1;
}

@end
