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

#import "SMARewardedAdViewController.h"
#import <SmaatoSDKRewardedAds/SmaatoSDKRewardedAds.h>

NSString *const SMARewardedAdsTip_LoadNew = @"Tap ‘Load’ again to load a new rewarded ad. The old rewarded ad could not be shown a second time";
NSString *const SMARewardedAdsTip_TryLoadTip = @"Tap ‘Load’ to load a rewarded ad";
NSString *const SMARewardedAdsTip_LoadingTip = @"Rewarded ad Ad is loading…";
NSString *const SMARewardedAdsTip_LoadedTip = @"Rewarded ad Ad was loaded successfully";
NSString *const SMARewardedAdsTip_ErrorTip = @"Something failed. See console logs";
NSString *const SMARewardedAdsTip_ExpireTip = @"The loaded rewarded ad is expired. Tap ‘Load’ to load a new rewarded ad.";

@interface SMARewardedAdViewController () <SMARewardedInterstitialDelegate>

@property(nonatomic) SMARewardedInterstitial *rewardedInterstitial;

@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet UIButton *buttonShow;
@property(nonatomic, weak) IBOutlet UILabel *publisherIdLabel;
@property(nonatomic, weak) IBOutlet UILabel *adspaceIdLabel;
@property(nonatomic, weak) IBOutlet AdBlockView *adBlock;

@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillDisppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidDisppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidTTLExpireLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillLeaveAppLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidRewardLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidStartLabel;

@end

@implementation SMARewardedAdViewController

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

#pragma mark - Load ads

- (void)loadRewardedAd
{
    [self resetLabels];
    self.rewardedInterstitial = nil;

    self.adBlock.tip = SMARewardedAdsTip_LoadingTip;

    //Send load rewarded ad request
    [SmaatoSDK loadRewardedInterstitialForAdSpaceId:self.adSpaceId
                                             delegate:self];
}

- (void)showRewardedAd
{
    // Try to show rewarded ad, if it has been already downloaded and is not expired (TTL value)
    if (self.rewardedInterstitial.availableForPresentation) {
        [self.rewardedInterstitial showFromViewController:self];
    }
}

- (void)setupUI
{
    self.adBlock.tip = SMARewardedAdsTip_TryLoadTip;
    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.rewardedAdDidFailLabel.alpha = 0.2;
    self.rewardedAdWillAppearLabel.alpha = 0.2;
    self.rewardedAdDidAppearLabel.alpha = 0.2;
    self.rewardedAdWillDisppearLabel.alpha = 0.2;
    self.rewardedAdDidDisppearLabel.alpha = 0.2;
    self.rewardedAdDidTTLExpireLabel.alpha = 0.2;
    self.rewardedAdWillLeaveAppLabel.alpha = 0.2;
    self.rewardedAdDidRewardLabel.alpha = 0.2;
    self.rewardedAdDidStartLabel.alpha = 0.2;
}

#pragma mark - Actions

- (IBAction)btnLoadTapped:(UIButton *)sender
{
    [self loadRewardedAd];
}

- (IBAction)btnShowTapped:(UIButton *)sender
{
    [self.buttonShow setEnabled:NO];
    [self showRewardedAd];
}

#pragma mark - SMARewardedInterstitialDelegate
#pragma mark - Required methods

- (void)rewardedInterstitialDidLoad:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    [self resetLabels];
    self.rewardedAdDidLoadLabel.alpha = 1;
    self.rewardedInterstitial = rewardedInterstitial;
    self.adBlock.tip = SMARewardedAdsTip_LoadedTip;
    [self.buttonShow setEnabled:YES];
}

- (void)rewardedInterstitialDidFail:(SMARewardedInterstitial *_Nullable)rewardedInterstitial
                          withError:(NSError *_Nonnull)error
{
    NSLog(@"Rewarded ad loading error: %@", error.localizedDescription);
    self.rewardedAdDidFailLabel.alpha = 1;
    self.adBlock.tip = SMARewardedAdsTip_ErrorTip;
}

- (void)rewardedInterstitialDidReward:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    self.rewardedAdDidRewardLabel.alpha = 1;
    NSLog(@"Rewarded ad did reward");
}

- (void)rewardedInterstitialDidTTLExpire:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad did TTL expire");
    self.rewardedAdDidTTLExpireLabel.alpha = 1;
    [self.buttonShow setEnabled:NO];
    self.adBlock.tip = SMARewardedAdsTip_ExpireTip;
}

#pragma mark - Optional methods

- (void)rewardedInterstitialWillAppear:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad will appear");
    self.rewardedAdWillAppearLabel.alpha = 1;
}

- (void)rewardedInterstitialDidAppear:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad did appear");
    self.rewardedAdDidAppearLabel.alpha = 1;
}

- (void)rewardedInterstitialWillDisappear:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad will disappear");
    self.rewardedAdWillDisppearLabel.alpha = 1;
    self.adBlock.tip = SMARewardedAdsTip_LoadNew;
}

- (void)rewardedInterstitialDidDisappear:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad did disappear");
    self.rewardedAdDidDisppearLabel.alpha = 1;
    [self.buttonShow setEnabled:NO];
}

- (void)rewardedInterstitialDidStart:(SMARewardedInterstitial *_Nonnull)rewardedInterstitial
{
    NSLog(@"Rewarded ad did start");
    self.rewardedAdDidStartLabel.alpha = 1;
}

- (void)rewardedInterstitialWillLeaveApplication:(SMARewardedInterstitial *)rewardedInterstitial
{
    NSLog(@"Rewarded ad will leave application");
    self.rewardedAdWillLeaveAppLabel.alpha = 1;
}

@end
