//
//  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 () <GADFullScreenContentDelegate>

@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;

@property(nonatomic) GADRewardedAd *rewardedAd;

@end

@implementation SMAAdMobRewardedAdapterViewController

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

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

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    [self.showButton setEnabled:NO];
    self.adBlockView.tip = AdMobRewardedAdsTip_Loading;
    [GADRewardedAd loadWithAdUnitID:self.adMobModel.adUnitId
                                request:[GADRequest request]
                      completionHandler:^(GADRewardedAd * _Nullable rewardedAd, NSError * _Nullable error) {
        if (error) {
            NSLog(@"Failed to load interstitial ad with error: \(error.localizedDescription)");
            return;
        }
        self.rewardedAd = rewardedAd;
        self.rewardedAd.fullScreenContentDelegate = self;
        
        [self resetLabels];
        [self.showButton setEnabled:YES];
        self.adBlockView.tip = AdMobRewardedAdsTip_Loaded;
        self.rewardedAdDidReceiveAd.alpha = 1;
    }];
}

- (IBAction)showButtonTapped:(id)sender
{
//    self.adBlockView.tip = AdMobRewardedAdsTip_LoadNew;
    [self.showButton setEnabled:NO];
    
    if (self.rewardedAd) {
        [self.rewardedAd presentFromRootViewController:self userDidEarnRewardHandler:^{
            self.rewardedAdDidRewardLabel.alpha = 1;
        }];
    } else {
        NSLog(@"Ad wasn't ready");
    }
}

- (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;
//}

/// Tells the delegate that an impression has been recorded for the ad.
- (void)adDidRecordImpression:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.rewardedAdDidStartLabel.alpha = 1;
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    self.rewardedAdDidFailLabel.alpha = 1;
    self.adBlockView.tip = AdMobRewardedAdsTip_Error;
}

/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.rewardedAdDidOpenLabel.alpha = 1;
}

/// Tells the delegate that the ad will dismiss full screen content.
- (void)adWillDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    self.rewardedAdDidCloseLabel.alpha = 1;
}

@end
