//
//  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.adSpaceId;
    self.bannerView.rootViewController = self;
    self.bannerView.delegate = self;
}

- (void)setupUI
{
    self.title = @"AdMob Banner";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adUnitIDLabel.text = self.adMobModel.adSpaceId;
    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 AdMobBannerSizesSkyscrapperSize:
            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)adViewDidReceiveAd:(GADBannerView *)bannerView
{
    self.adBlock.tip = AdMobBannerAdsTip_Loaded;
    self.bannerDidReceiveLabel.alpha = 1;
    [self.showButton setEnabled:YES];
}

- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
{
    self.bannerDidFailToReceiveAdLabel.alpha = 1;
}

- (void)adViewWillPresentScreen:(GADBannerView *)bannerView
{
    self.bannerWillPresentScreenLabel.alpha = 1;
}

- (void)adViewWillDismissScreen:(GADBannerView *)bannerView
{
    self.bannerWillDismissScreenLabel.alpha = 1;
}

- (void)adViewDidDismissScreen:(GADBannerView *)bannerView
{
    self.bannerDidDismissScreenLabel.alpha = 1;
}

- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView
{
    self.bannerWillLeaveAppLabel.alpha = 1;
}

@end
