//
//  SMAMoPubSmaatoRewardedVideoAdapter.m
//  SmaatoSDKAdapters
//
//  Created by Ankur Shivani on 13.11.18.
//  Copyright © 2018 Smaato. All rights reserved.
//

#import <SmaatoSDKRewardedAds/SmaatoSDKRewardedAds.h>
#import "SMAMoPubSmaatoRewardedVideoAdapter.h"

static NSString *const kSMAMoPubCustomEventInfoPublisherIdKey = @"publisherId";
static NSString *const kSMAMoPubCustomEventInfoAdSpaceIdKey = @"adspaceId";

static NSString *const kSMAMoPubVideoRewardedVideoAdapterVersion = @"1.0.0";

@interface SMAMoPubSmaatoRewardedVideoAdapter () <SMARewardedInterstitialDelegate>
@property (nonatomic) SMARewardedInterstitial *rewardedAd;
@end

@implementation SMAMoPubSmaatoRewardedVideoAdapter

+ (NSString *)version
{
    return kSMAMoPubVideoRewardedVideoAdapterVersion;
}

- (void)requestRewardedVideoWithCustomEventInfo:(NSDictionary *)info
{
    // Extract ad space information
    NSString *publisherId = info[kSMAMoPubCustomEventInfoPublisherIdKey];
    NSString *adSpaceId = info[kSMAMoPubCustomEventInfoAdSpaceIdKey];
    
    // Verify ad space information
    if (![self checkCredentialsWithAdSpaceId:adSpaceId publisherId:publisherId]) {
        return;
    }

    /**
     Optional:
     You can also set specific user profile targeting parameters here.
     Please check the Smaato wiki for all available properties and further details.

     Examples:
     SmaatoSDK.userAge = 30;
     SmaatoSDK.userGender = kSMAGenderMale;
     */

    // Load ad
    SMARewardedInterstitial.mediationNetworkName = NSStringFromClass([self class]);
    SMARewardedInterstitial.mediationAdapterVersion = kSMAMoPubVideoRewardedVideoAdapterVersion;
    SMARewardedInterstitial.mediationNetworkSDKVersion = [[MoPub sharedInstance] version];
    [SmaatoSDK loadRewardedInterstitialForPublisherId:publisherId adSpaceId:adSpaceId delegate:self];
}

- (BOOL)checkCredentialsWithAdSpaceId:(NSString *)adSpaceId publisherId:(NSString *)publisherId
{
    if (publisherId && adSpaceId) {
        return YES;
    }
    
    NSString *errorMessage = @"PublisherId and/or AdSpaceId can not be extracted. Please check your configuration on MoPub dashboard.";
    NSLog(@"[SmaatoSDK] [Error] %@: %@", NSStringFromClass([self class]), errorMessage);
    
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEvent:didFailToLoadAdWithError:)]) {
        NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
        NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:kSMAErrorCodeInvalidRequest userInfo:userInfo];
        
        [self.delegate rewardedVideoDidFailToLoadAdForCustomEvent:self error:error];
    }
    
    return NO;
}

- (void)presentRewardedVideoFromViewController:(UIViewController *)viewController
{
    if (self.rewardedAd.availableForPresentation) {
        [self.rewardedAd showFromViewController:viewController];
    }
}

- (BOOL)hasAdAvailable
{
    return self.rewardedAd.availableForPresentation;
}

#pragma mark - SMARewardedInterstitialDelegate

- (void)rewardedInterstitialDidFail:(SMARewardedInterstitial *)rewardedInterstitial withError:(NSError *)error
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidFailToLoadAdForCustomEvent:error:)]) {
        [self.delegate rewardedVideoDidFailToLoadAdForCustomEvent:self error:error];
    }
}

- (void)rewardedInterstitialDidLoad:(SMARewardedInterstitial *)rewardedInterstitial
{
    self.rewardedAd = rewardedInterstitial;
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidLoadAdForCustomEvent:)]) {
        [self.delegate rewardedVideoDidLoadAdForCustomEvent:self];
    }
}

- (void)rewardedInterstitialDidReward:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoShouldRewardUserForCustomEvent:reward:)]) {
        // Smaato doesn't retrieve exact reward (currency and amount) after video ad completion, due to this reason
        // publisher should supply whole RewardedVideo workflow with mandatory rewardedVideoAdShouldReward() function calling himself.
        // This publisher has to add new reward in Mopub Dashboard for given RewardedVideo adUnitId and this reward will be forwarded to
        // publusher app, after rewarded video completion
        [self.delegate rewardedVideoShouldRewardUserForCustomEvent:self reward:nil];
    }
}

- (void)rewardedInterstitialDidTTLExpire:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidExpireForCustomEvent:)]) {
        [self.delegate rewardedVideoDidExpireForCustomEvent:self];
    }
}

- (void)rewardedInterstitialWillAppear:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoWillAppearForCustomEvent:)]) {
        [self.delegate rewardedVideoWillAppearForCustomEvent:self];
    }
}

- (void)rewardedInterstitialDidAppear:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidAppearForCustomEvent:)]) {
        [self.delegate rewardedVideoDidAppearForCustomEvent:self];
    }
}

- (void)rewardedInterstitialWillDisappear:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoWillDisappearForCustomEvent:)]) {
        [self.delegate rewardedVideoWillDisappearForCustomEvent:self];
    }
}

- (void)rewardedInterstitialDidDisappear:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidDisappearForCustomEvent:)]) {
        [self.delegate rewardedVideoDidDisappearForCustomEvent:self];
    }
}

- (void)rewardedInterstitialDidStart:(SMARewardedInterstitial *)rewardedInterstitial
{
    // No corresponding method from MoPub SDK available.
}

- (void)rewardedInterstitialDidClick:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoDidReceiveTapEventForCustomEvent:)]) {
        [self.delegate rewardedVideoDidReceiveTapEventForCustomEvent:self];
    }
}

- (void)rewardedInterstitialWillLeaveApplication:(SMARewardedInterstitial *)rewardedInterstitial
{
    if ([self.delegate respondsToSelector:@selector(rewardedVideoWillLeaveApplicationForCustomEvent:)]) {
        [self.delegate rewardedVideoWillLeaveApplicationForCustomEvent:self];
    }
}

@end
