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

#import "SMAUbMoPubRewardedViewController.h"
#import <MoPub/MoPub.h>
#import <SMAMoPubSmaatoRewardedVideoAdapter.h>
#import <SmaatoSDKUnifiedBidding/SmaatoSDKUnifiedBidding.h>

NSString *const UbMoPubRewardedAdsTip_TryLoad = @"Tap ‘Load’ to load a rewarded ad";
NSString *const UbMoPubRewardedAdsTip_Loading = @"Rewarded ad is loading…";
NSString *const UbMoPubRewardedAdsTip_Loaded = @"Rewarded ad was loaded successfully";
NSString *const UbMoPubRewardedAdsTip_FailedToPlay = @"Rewarded ad failed to play";
NSString *const UbMoPubRewardedAdsTip_Expired = @"The loaded rewarded ad is expired. Tap ‘Load’ to load a new rewarded ad.";
NSString *const UbMoPubRewardedAdsTip_Error = @"Something failed. See console logs";
NSString *const UbMoPubRewardedAdsTip_LoadNew = @"Tap ‘Load’ again to load a new rewarded ad. The old rewarded ad could not be shown a second time";

@interface SMAUbMoPubRewardedViewController () <MPRewardedVideoDelegate>

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

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

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

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

- (void)setupUI
{
    self.title = @"MoPub Rewarded";
    self.adBlock.tip = UbMoPubRewardedAdsTip_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];

    __weak typeof(self) weakSelf = self;
    [SmaatoSDK prebidInterstitialForAdSpaceId:self.mopubModel.smaatoAdspaceId
                                   completion:^(SMAUbBid *_Nullable bid, NSError *_Nullable error) {
                                       __strong typeof(self) self = weakSelf;
                                       NSString *bidKeyword = nil;
                                       NSDictionary *bidExtra = nil;
                                       if (bid) {
                                           bidKeyword = bid.mopubPrebidKeyword;
                                           bidExtra = bid.metaData;
                                       } else {
                                           NSLog(@"SmaatoSDK PreBid error: %@", error);
                                       }
                                       
                                       [MPRewardedVideo loadRewardedVideoAdWithAdUnitID:self.mopubModel.adUnitId
                                                                               keywords:bidKeyword
                                                                       userDataKeywords:nil
                                                                               location:nil
                                                                             customerId:nil
                                                                      mediationSettings:@[]
                                                                            localExtras:bidExtra];
                                   }];
}

- (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 = UbMoPubRewardedAdsTip_Loaded;
    self.rewardedAdDidloadLabel.alpha = 1;
}

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