//
//  SMAMoPubRewardedAdapterViewController.m
//  sdk-demo-ios-objc
//
//  Created by Smaato Inc. on 03/03/2019.
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

#import <MoPub/MoPub.h>
#import "SMAMoPubRewardedAdapterViewController.h"
#import <SMAMoPubSmaatoRewardedVideoAdapter.h>

NSString *const MoPubRewardedAdsTip_TryLoad = @"Tap ‘Load’ to load a rewarded ad";
NSString *const MoPubRewardedAdsTip_Loading = @"Rewarded ad is loading…";
NSString *const MoPubRewardedAdsTip_Loaded = @"Rewarded ad was loaded successfully";
NSString *const MoPubRewardedAdsTip_FailedToPlay = @"Rewarded ad failed to play";
NSString *const MoPubRewardedAdsTip_Expired = @"The loaded rewarded ad is expired. Tap ‘Load’ to load a new rewarded ad.";
NSString *const MoPubRewardedAdsTip_Error = @"Something failed. See console logs";
NSString *const MoPubRewardedAdsTip_LoadNew = @"Tap ‘Load’ again to load a new rewarded ad. The old rewarded ad could not be shown a second time";

@interface SMAMoPubRewardedAdapterViewController () <MPRewardedVideoDelegate>

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

@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidloadLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidFailToLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidFailToPlayLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidAppearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillDisappearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidDisappearLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidExpireLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdShouldRewardLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdWillLeaveApplicationLabel;
@property(nonatomic, weak) IBOutlet UILabel *rewardedAdDidReceiveTapLabel;

@end

@implementation SMAMoPubRewardedAdapterViewController

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

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

- (IBAction)loadButtonTapped:(id)sender
{
    self.showButton.enabled = NO;
    self.adBlock.tip = MoPubRewardedAdsTip_Loading;
    [self loadAd];
}

- (IBAction)showButtonTapped:(id)sender
{
    [self.showButton setEnabled:NO];
    self.adBlock.tip = MoPubRewardedAdsTip_LoadNew;
    [MPRewardedVideo presentRewardedVideoAdForAdUnitID:self.mopubModel.adUnitId fromViewController:self withReward:nil];
}

- (void)setupUI
{
    self.title = @"MoPub Rewarded";
    self.adBlock.tip = MoPubRewardedAdsTip_TryLoad;
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adUnitIDLabel.text = self.mopubModel.adUnitId;

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

- (void)loadAd
{
    [MPRewardedVideo setDelegate:self forAdUnitId:self.mopubModel.adUnitId];
    [MPRewardedVideo loadRewardedVideoAdWithAdUnitID:self.mopubModel.adUnitId withMediationSettings:nil];
}

- (void)resetLabels
{
    self.rewardedAdDidloadLabel.alpha = 0.2;
    self.rewardedAdDidFailToLoadLabel.alpha = 0.2;
    self.rewardedAdDidFailToPlayLabel.alpha = 0.2;
    self.rewardedAdWillAppearLabel.alpha = 0.2;
    self.rewardedAdDidAppearLabel.alpha = 0.2;
    self.rewardedAdWillDisappearLabel.alpha = 0.2;
    self.rewardedAdDidDisappearLabel.alpha = 0.2;
    self.rewardedAdDidExpireLabel.alpha = 0.2;
    self.rewardedAdShouldRewardLabel.alpha = 0.2;
    self.rewardedAdWillLeaveApplicationLabel.alpha = 0.2;
}

#pragma mark - MPRewaredVideoDelegate

- (void)rewardedVideoAdDidLoadForAdUnitID:(NSString *)adUnitID
{
    [self resetLabels];
    self.showButton.enabled = YES;
    self.adBlock.tip = MoPubRewardedAdsTip_Loaded;
    self.rewardedAdDidloadLabel.alpha = 1;
}

- (void)rewardedVideoAdDidFailToLoadForAdUnitID:(NSString *)adUnitID error:(NSError *)error
{
    self.adBlock.tip = MoPubRewardedAdsTip_Error;
    self.rewardedAdDidFailToLoadLabel.alpha = 1;
}

- (void)rewardedVideoAdDidFailToPlayForAdUnitID:(NSString *)adUnitID error:(NSError *)error
{
    self.rewardedAdDidFailToPlayLabel.alpha = 1;
}

- (void)rewardedVideoAdWillAppearForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdWillAppearLabel.alpha = 1;
}

- (void)rewardedVideoAdDidAppearForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdDidAppearLabel.alpha = 1;
}

- (void)rewardedVideoAdWillDisappearForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdWillDisappearLabel.alpha = 1;
}

- (void)rewardedVideoAdDidDisappearForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdDidDisappearLabel.alpha = 1;
}

- (void)rewardedVideoAdDidReceiveTapEventForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdDidReceiveTapLabel.alpha = 1;
}

- (void)rewardedVideoAdWillLeaveApplicationForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdWillLeaveApplicationLabel.alpha = 1;
}

- (void)rewardedVideoAdDidExpireForAdUnitID:(NSString *)adUnitID
{
    self.rewardedAdDidExpireLabel.alpha = 1;
}

- (void)rewardedVideoAdShouldRewardForAdUnitID:(NSString *)adUnitID reward:(MPRewardedVideoReward *)reward
{
    self.rewardedAdShouldRewardLabel.alpha = 1;
}

@end
