//
//  SMAMoPubSmaatoInterstitialAdapter.m
//  SmaatoSDKAdapters
//
//  Created by Henning Buck on 13.11.18.
//  Copyright © 2018 Smaato. All rights reserved.
//

#import <SmaatoSDKInterstitial/SmaatoSDKInterstitial.h>
#import "SMAMoPubSmaatoInterstitialAdapter.h"

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

@interface SMAMoPubSmaatoInterstitialAdapter () <SMAInterstitialDelegate>
@property (nonatomic) SMAInterstitial *interstitial;
@end

@implementation SMAMoPubSmaatoInterstitialAdapter

+ (NSString *)version
{
    return kSMAMoPubInterstitialAdapterVersion;
}

- (void)requestInterstitialWithCustomEventInfo:(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;
    }
   
    // Pass user location
    CLLocation *location = self.delegate.location;
    if (location) {
        SMALocation *userLocation = [[SMALocation alloc] initWithLatitude:location.coordinate.latitude
                                                                longitude:location.coordinate.longitude
                                                       horizontalAccuracy:location.horizontalAccuracy
                                                                timestamp:location.timestamp];
        SmaatoSDK.userLocation = userLocation;
    }

    /**
     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
    SMAInterstitial.mediationNetworkName = NSStringFromClass([self class]);
    SMAInterstitial.mediationAdapterVersion = kSMAMoPubInterstitialAdapterVersion;
    SMAInterstitial.mediationNetworkSDKVersion = [[MoPub sharedInstance] version];
    [SmaatoSDK loadInterstitialForPublisherId: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 interstitialCustomEvent:self didFailToLoadAdWithError:error];
    }
    
    return NO;
}

- (void)showInterstitialFromRootViewController:(UIViewController *)rootViewController
{
    if (self.interstitial.availableForPresentation) {
        [self.interstitial showFromViewController:rootViewController];
    }
}

#pragma mark - SMAInterstitialDelegate

- (void)interstitialDidLoad:(SMAInterstitial *)interstitial
{
    self.interstitial = interstitial;
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEvent:didLoadAd:)]) {
        [self.delegate interstitialCustomEvent:self didLoadAd:interstitial];
    }
}

- (void)interstitial:(SMAInterstitial *)interstitial didFailWithError:(NSError *)error
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEvent:didFailToLoadAdWithError:)]) {
        [self.delegate interstitialCustomEvent:self didFailToLoadAdWithError:error];
    }
}

- (void)interstitialDidTTLExpire:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventDidExpire:)]) {
        [self.delegate interstitialCustomEventDidExpire:self];
    }
}

- (void)interstitialWillAppear:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventWillAppear:)]) {
        [self.delegate interstitialCustomEventWillAppear:self];
    }
}

- (void)interstitialDidAppear:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventDidAppear:)]) {
        [self.delegate interstitialCustomEventDidAppear:self];
    }
}

- (void)interstitialWillDisappear:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventWillDisappear:)]) {
        [self.delegate interstitialCustomEventWillDisappear:self];
    }
}

- (void)interstitialDidDisappear:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventDidDisappear:)]) {
        [self.delegate interstitialCustomEventDidDisappear:self];
    }
}

- (void)interstitialDidClick:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventDidReceiveTapEvent:)]) {
        [self.delegate interstitialCustomEventDidReceiveTapEvent:self];
    }
}

- (void)interstitialWillLeaveApplication:(SMAInterstitial *)interstitial
{
    if ([self.delegate respondsToSelector:@selector(interstitialCustomEventWillLeaveApplication:)]) {
        [self.delegate interstitialCustomEventWillLeaveApplication:self];
    }
}

@end
