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

#import "SMABannerViewController.h"

NSString *const BannerAdsTip_TryLoad = @"Tap ‘Load’ to load a banner";
NSString *const BannerAdsTip_Loading = @"Banner is loading…";
NSString *const BannerAdsTip_Loaded = @"Banner was loaded successfully";
NSString *const BannerAdsTip_Clicked = @"Banner has been clicked";
NSString *const BannerAdsTip_Error = @"Something failed. See console logs";


@interface SMABannerViewController () <SMABannerViewDelegate>

@property(nonatomic, weak) IBOutlet UIButton *buttonShow;
@property(nonatomic, weak) IBOutlet UILabel *publisherIdLabel;
@property(nonatomic, weak) IBOutlet UILabel *adspaceIdLabel;
@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;
@property(nonatomic, weak) IBOutlet UISwitch *switchAutoReload;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlockWidthConstraint;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint *adBlocHeightConstraint;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerWillPresentModalContentLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidPresentModalContentLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidDismissModalContentLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidClickLabel;
@property(nonatomic, weak) IBOutlet UILabel *bannerDidTTLExpireLabel;

@property(nonatomic, strong) SMABannerView *bannerView;

@end

@implementation SMABannerViewController

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

#pragma mark - Actions

- (IBAction)btnLoadTapped:(UIButton *)sender
{
    // Remove old banner from the screen if it exists
    [self.bannerView removeFromSuperview];
    self.adBlock.tip = BannerAdsTip_Loading;

    [self resetLabels];

    // Create new banner view programatically
    SMABannerView *banner = [[SMABannerView alloc] initWithFrame:CGRectMake(0, 0, self.expectedSize.width, self.expectedSize.height)];

    // Send request and listen to callbacks
    banner.delegate = self;
    [banner loadWithAdSpaceId:self.adSpaceId adSize:self.adSize];

    self.bannerView = banner;
}

- (IBAction)btnShowTapped:(UIButton *)sender
{
    if (!self.bannerView) {
        return;
    }
    [self.buttonShow setEnabled:NO];
    self.bannerView.autoreloadInterval = self.switchAutoReload.isOn ? kSMABannerAutoreloadIntervalDefault : kSMABannerAutoreloadIntervalDisabled;
    [self.adBlock addSubview:self.bannerView];
    self.adBlock.tip = @"";
}

- (IBAction)enableDidSwitch:(UISwitch *)sender
{
    //Seconds count before Ad is reloaded. Default value is 0 (Autoreload is disabled).
    self.bannerView.autoreloadInterval = self.switchAutoReload.isOn ? kSMABannerAutoreloadIntervalDefault : kSMABannerAutoreloadIntervalDisabled;
}

- (void)setupUI
{
    self.adBlock.tip = BannerAdsTip_TryLoad;
    [self updateAdBlockSize];
    self.publisherIdLabel.text = SmaatoSDK.publisherId;
    self.adspaceIdLabel.text = self.adSpaceId;

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

- (void)resetLabels
{
    self.bannerDidLoadLabel.alpha = 0.2;
    self.bannerDidFailLabel.alpha = 0.2;
    self.bannerWillPresentModalContentLabel.alpha = 0.2;
    self.bannerDidPresentModalContentLabel.alpha = 0.2;
    self.bannerDidDismissModalContentLabel.alpha = 0.2;
    self.bannerDidClickLabel.alpha = 0.2;
    self.bannerDidTTLExpireLabel.alpha = 0.2;
}

#pragma mark - Private

- (void)updateAdBlockSize
{
    self.adBlockWidthConstraint.constant = self.expectedSize.width;
    self.adBlocHeightConstraint.constant = self.expectedSize.height;
}

#pragma mark - SMABannerViewDelegate
#pragma mark - Required methods

- (UIViewController *)presentingViewControllerForBannerView:(SMABannerView *)bannerView
{
    return self;
}

- (void)bannerViewDidTTLExpire:(nonnull SMABannerView *)bannerView
{
    NSLog(@"Banner TTL has expired and you can not use this banner from now. Please load new one. Tip: show banner before expiration for the best fill rate (you have about 5 minutes after loading completion).");
    self.bannerDidTTLExpireLabel.alpha = 1;
}

#pragma mark - Optional methods

- (void)bannerViewDidLoad:(SMABannerView *)bannerView
{
    NSLog(@"Banner loadded successfully %@", bannerView);
    [self resetLabels];
    self.bannerDidLoadLabel.alpha = 1;
    [self.buttonShow setEnabled:YES];
    self.adBlock.tip = BannerAdsTip_Loaded;
}

- (void)bannerViewDidClick:(SMABannerView *)bannerView
{
    NSLog(@"Banner has been clicked %@", bannerView);
    self.bannerDidClickLabel.alpha = 1;
    self.adBlock.tip = BannerAdsTip_Clicked;
}

- (void)bannerView:(SMABannerView *)bannerView didFailWithError:(NSError *)error
{
    NSLog(@"Banner failed with error: %@", error);
    self.bannerDidFailLabel.alpha = 1;
    self.adBlock.tip = BannerAdsTip_Error;
}

- (void)bannerViewWillPresentModalContent:(nonnull SMABannerView *)bannerView
{
    NSLog(@"Banner will present a modal content");
    self.bannerWillPresentModalContentLabel.alpha = 1;
}

- (void)bannerViewDidPresentModalContent:(nonnull SMABannerView *)bannerView
{
    NSLog(@"Banner did present a modal content");
    self.bannerDidPresentModalContentLabel.alpha = 1;
}

- (void)bannerViewDidDismissModalContent:(nonnull SMABannerView *)bannerView
{
    NSLog(@"Banner did dissmiss a modal content");
    self.bannerDidDismissModalContentLabel.alpha = 1;
}

@end
