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

#import "SMAAdMobRewardedAdapterViewController.h"
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <SmaatoSDKRewardedAds/SmaatoSDKRewardedAds.h>
#import <SMAAdMobSmaatoRewardedVideoAdapter.h>

NSString *const AdMobRewardedAdsTip_TryLoad = @"Tap ‘Load’ to load a rewarded ad";
NSString *const AdMobRewardedAdsTip_Loading = @"Rewarded ad is loading…";
NSString *const AdMobRewardedAdsTip_Loaded = @"Rewarded ad was loaded successfully";
NSString *const AdMobRewardedAdsTip_FailedToPlay = @"Rewarded ad failed to play";
NSString *const AdMobRewardedAdsTip_Expired = @"The loaded rewarded ad is expired. Tap ‘Load’ to load a new rewarded ad.";
NSString *const AdMobRewardedAdsTip_Error = @"Something failed. See console logs";
NSString *const AdMobRewardedAdsTip_LoadNew = @"Tap ‘Load’ again to load a new rewarded ad. The old rewarded ad could not be shown a second time";

@interface SMAAdMobRewardedAdapterViewController () <GADRewardBasedVideoAdDelegate>

@property(nonatomic, weak) IBOutlet AdBlockView *adBlockView;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidReceiveAd;
@property(nonatomic, weak) IBOutlet UIButton *showButton;
@property(nonatomic, weak) IBOutlet UILabel *adUnitIDLabel;
@property(nonatomic, weak) IBOutlet UIScrollView *scrollView;
@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;

@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidOpenLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidStartLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidCompleteLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidCloseLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillLeaveAppLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidRewardLabel;

@end

@implementation SMAAdMobRewardedAdapterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [GADRewardBasedVideoAd sharedInstance].delegate = self;
}

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

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    [self.showButton setEnabled:NO];
    self.adBlockView.tip = AdMobRewardedAdsTip_Loading;
    [[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request] withAdUnitID:self.adMobModel.adUnitId];

}

- (IBAction)showButtonTapped:(id)sender
{
    self.adBlockView.tip = AdMobRewardedAdsTip_LoadNew;
    [self.showButton setEnabled:NO];

    if ([GADRewardBasedVideoAd sharedInstance].isReady) {
        [[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self];
    }
}

- (void)setupUI
{
    self.title = @"AdMob Rewarded";
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adUnitIDLabel.text = self.adMobModel.adUnitId;
    self.adBlockView.tip = AdMobRewardedAdsTip_TryLoad;

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

- (void)resetLabels
{
    self.rewardedAdDidReceiveAd.alpha = 0.2;
    self.rewardedAdDidFailLabel.alpha = 0.2;
    self.rewardedAdDidOpenLabel.alpha = 0.2;
    self.rewardedAdDidStartLabel.alpha = 0.2;
    self.rewardedAdDidCompleteLabel.alpha = 0.2;
    self.rewardedAdDidCloseLabel.alpha = 0.2;
    self.rewardedAdWillLeaveAppLabel.alpha = 0.2;
    self.rewardedAdDidRewardLabel.alpha = 0.2;
}

#pragma mark - GADRewardBasedVideoAdDelegate methods

- (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    [self.showButton setEnabled:YES];
    self.adBlockView.tip = AdMobRewardedAdsTip_Loaded;
    self.rewardedAdDidReceiveAd.alpha = 1;
}

- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didFailToLoadWithError:(NSError *)error
{
    self.rewardedAdDidFailLabel.alpha = 1;
    self.adBlockView.tip = AdMobRewardedAdsTip_Error;
}

- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didRewardUserWithReward:(GADAdReward *)reward
{
    self.rewardedAdDidRewardLabel.alpha = 1;
}

- (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    self.rewardedAdDidOpenLabel.alpha = 1;
}

- (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    self.rewardedAdDidStartLabel.alpha = 1;
}

- (void)rewardBasedVideoAdDidCompletePlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    self.rewardedAdDidCompleteLabel.alpha = 1;
}

- (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    self.rewardedAdDidCloseLabel.alpha = 1;
}

- (void)rewardBasedVideoAdWillLeaveApplication:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    self.rewardedAdWillLeaveAppLabel.alpha = 1;
}

@end
