//
//  InMobiInterstitial.m
//  Unity-iPhone
//
//  Created by Vineet Srivastava on 11/24/16.
//
//

#import <Foundation/Foundation.h>
#import "InMobiInterstitial.h"
#import "InMobiPlugin.h"


@interface InMobiInterstitial()<IMInterstitialDelegate>

@end

@implementation InMobiInterstitial

-(id)initInterstitialAd:(InMobiInterstitialClientRef *)interstitialClient placementId:(NSString *)placementId
{
    self = [super init];
    if(self){
        _interstitialClient = interstitialClient;
        _interstitial = [[IMInterstitial alloc] initWithPlacementId:[placementId longLongValue]];
        _interstitial.delegate = self;
    }
    return self;
}

-(void)setKeywords:(NSString*) keyWords{
    if(!_interstitial){
        NSLog(@"InMobi Interstitial is nil. Ignoring call to setKeywords");
        return;
    }
    [_interstitial setKeywords:keyWords];
}

-(void)setExtras:(NSDictionary<NSString *, id>*) extras {
    if(!_interstitial){
        NSLog(@"InMobi Interstitial is nil. Ignoring call to extras");
        return;
    }
    // Assuming _banner and extras are already defined
    NSMutableDictionary *currentExtras = [[_interstitial extras] mutableCopy];
    if (!currentExtras) {
        currentExtras = [NSMutableDictionary dictionary];
    }
    [currentExtras addEntriesFromDictionary:extras];
    [_interstitial setExtras:currentExtras];
}


-(void)loadAd
{
    if(!_interstitial){
        NSLog(@"InMobi Interstitial is nil. Please initialize Interstitial Object before calling load");
        return;
    }
    // Assuming _banner and extras are already defined
    NSDictionary *unityDictionary =@{@"tp": @"p_unity", @"tp-ver": @"1.0.0"};
    NSMutableDictionary *currentExtras = [[_interstitial extras] mutableCopy];
    if (!currentExtras) {
        currentExtras = [NSMutableDictionary dictionary];
    }
    [currentExtras addEntriesFromDictionary:unityDictionary];
    [_interstitial setExtras:currentExtras];
    
    [_interstitial load];
}

-(bool)isReady
{
    if(!_interstitial){
        NSLog(@"InMobi Interstitial is nil.");
        return false;
    }
    return _interstitial.isReady;
}

-(void)showAd
{
    if(!_interstitial){
        NSLog(@"InMobi Interstitial is nil. Please initialize Interstitial Object before calling show");
        return;
    }
    
    [_interstitial showFrom:[InMobiPlugin unityGLViewController]];
}

#pragma IMInterstitialDelegate Callbacks


-(void)interstitial:(IMInterstitial*)interstitial didReceiveWithMetaInfo:(IMAdMetaInfo*)metaInfo {
    NSLog(@"interstitialDidReceiveAd");
    self.onAdFetchSuccessful(_interstitialClient, [metaInfo.creativeID UTF8String], [metaInfo getBid], [InMobiPlugin JSONfromObject:metaInfo.bidInfo].UTF8String);
}

- (void)interstitial:(IMInterstitial*)interstitial didFailToReceiveWithError:(NSError*)error {
    NSLog(@"didFailToReceiveWithError");
    NSLog(@"Error : %@",error.description);
    self.onAdFetchFailed(_interstitialClient, [error.localizedDescription UTF8String]);
}

/*Indicates that the interstitial is ready to be shown */
- (void)interstitialDidFinishLoading:(IMInterstitial *)interstitial {
    NSLog(@"interstitialDidFinishLoading");
    self.onAdLoadSucceeded(_interstitialClient);
}

/* Indicates that the interstitial has failed to receive an ad. */
- (void)interstitial:(IMInterstitial *)interstitial didFailToLoadWithError:(IMRequestStatus *)error {
    NSLog(@"Interstitial failed to load ad");
    NSLog(@"Error : %@",error.description);
    self.onAdLoadFailed(_interstitialClient, [error.description UTF8String]);
}

/* Indicates that the interstitial has failed to present itself. */
- (void)interstitial:(IMInterstitial *)interstitial didFailToPresentWithError:(IMRequestStatus *)error {
    NSLog(@"Interstitial didFailToPresentWithError");
    NSLog(@"Error : %@",error.description);
    self.onAdDisplayFailed(_interstitialClient);
}

/* indicates that the interstitial is going to present itself. */
- (void)interstitialWillPresent:(IMInterstitial *)interstitial {
    NSLog(@"interstitialWillPresent");
    self.onAdWillDisplay(_interstitialClient);
}

/* Indicates that the interstitial has presented itself */
- (void)interstitialDidPresent:(IMInterstitial *)interstitial {
    NSLog(@"interstitialDidPresent");
    self.onAdDisplayed(_interstitialClient);
}

/* Indicates that the interstitial is going to dismiss itself. */
- (void)interstitialWillDismiss:(IMInterstitial *)interstitial {
    NSLog(@"interstitialWillDismiss");
    //self.onAdDismissed(_interstitialClient);
}

/* Indicates that the interstitial has dismissed itself. */
- (void)interstitialDidDismiss:(IMInterstitial *)interstitial {
    NSLog(@"interstitialDidDismiss");
    self.onAdDismissed(_interstitialClient);
}

/* Indicates that the user will leave the app. */
- (void)userWillLeaveApplicationFromInterstitial:(IMInterstitial *)interstitial {
    NSLog(@"userWillLeaveApplicationFromInterstitial");
    self.onUserLeftApplication(_interstitialClient);
}

/* Indicates that a reward action is completed */
- (void)interstitial:(IMInterstitial *)interstitial rewardActionCompletedWithRewards:(NSDictionary *)rewards {
    NSLog(@"rewardActionCompletedWithRewards");
    self.onAdRewardActionCompleted(_interstitialClient, [InMobiPlugin JSONfromObject:rewards].UTF8String);
}

/* interstitial:didInteractWithParams: Indicates that the interstitial was interacted with. */
- (void)interstitial:(IMInterstitial *)interstitial didInteractWithParams:(NSDictionary *)params {
    NSLog(@"InterstitialDidInteractWithParams");
    self.onAdInteraction(_interstitialClient, [InMobiPlugin JSONfromObject:params].UTF8String);
}


-(void)interstitialAdImpressed:(IMInterstitial *)interstitial {
    NSLog(@"interstitialAdImpressed");
    self.onAdImpression(_interstitialClient);
}

@end
