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

#import "SMAHbMoPubRewardedViewController.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoRewardedVideoAdapter.h>
@import SmaatoSDKHeaderBidding;

NSString *const HbMoPubRewardedAdsTip_TryLoad = @"Tap ‘Load’ to load a rewarded ad";
NSString *const HbMoPubRewardedAdsTip_Loading = @"Rewarded ad is loading…";
NSString *const HbMoPubRewardedAdsTip_Loaded = @"Rewarded ad was loaded successfully";
NSString *const HbMoPubRewardedAdsTip_FailedToPlay = @"Rewarded ad failed to play";
NSString *const HbMoPubRewardedAdsTip_Expired = @"The loaded rewarded ad is expired. Tap ‘Load’ to load a new rewarded ad.";
NSString *const HbMoPubRewardedAdsTip_Error = @"Something failed. See console logs";
NSString *const HbMoPubRewardedAdsTip_LoadNew = @"Tap ‘Load’ again to load a new rewarded ad. The old rewarded ad could not be shown a second time";

NSString *const kHeaderBiddingRewardedAdSpaceId = @"130560861";

@interface SMAHbMoPubRewardedViewController ()<MPRewardedVideoDelegate>

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

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

@end

@implementation SMAHbMoPubRewardedViewController

- (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 = HbMoPubRewardedAdsTip_Loading;
    [self loadAd];
}

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

- (void)setupUI {
    self.title = @"MoPub Rewarded";
    self.adBlock.tip = HbMoPubRewardedAdsTip_TryLoad;
    self.adapterVersionLabel.text = [self adapterVersion];
    self.adUnitIDLabel.text = self.mopubModel.adSpaceId;
    
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)loadAd {
    [MPRewardedVideo setDelegate:self forAdUnitId:self.mopubModel.adSpaceId];
    
    __weak SMAHbMoPubRewardedViewController *weakSelf = self;
    [SmaatoSDK applyPreBidKeyForAdObject:[MPRewardedVideo class]  adSpaceId:kHeaderBiddingRewardedAdSpaceId completion:^(BOOL success, NSError *error) {
        if (error) {
            NSLog(@"SmaatoSDK PreBid error: %@", error);
        }
        
        [MPRewardedVideo loadRewardedVideoAdWithAdUnitID:weakSelf.mopubModel.adSpaceId 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 = HbMoPubRewardedAdsTip_Loaded;
    self.rewardedAdDidloadLabel.alpha = 1;
}

- (void)rewardedVideoAdDidFailToLoadForAdUnitID:(NSString *)adUnitID error:(NSError *)error
{
    self.adBlock.tip = HbMoPubRewardedAdsTip_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
