//Copyright (c) 2014 Sang Ki Kwon (Cranberrygame)
//Email: cranberrygame@yahoo.com
//Homepage: http://cranberrygame.github.io
//License: MIT (http://opensource.org/licenses/MIT)
#import "Admob.h"
//
#import "AdmobOverlap.h"
#import "AdmobSplit.h"
#import <CommonCrypto/CommonDigest.h> //md5

@implementation Admob

@synthesize callbackIdKeepCallback;
//
@synthesize pluginDelegate;
//
@synthesize email;
@synthesize licenseKey_;
@synthesize validLicenseKey;
static NSString *TEST_BANNER_AD_UNIT = @"ca-app-pub-8518241683236937/3754433805";
static NSString *TEST_INTERSTITIAL_AD_UNIT = @"ca-app-pub-8518241683236937/8184633406";
	
- (void) pluginInitialize {
    [super pluginInitialize];    
    //
}
	
- (void) setLicenseKey: (CDVInvokedUrlCommand*)command {
    NSString *email = [command.arguments objectAtIndex: 0];
    NSString *licenseKey = [command.arguments objectAtIndex: 1];
    NSLog(@"%@", email);
    NSLog(@"%@", licenseKey);
    
    [self.commandDelegate runInBackground:^{
        [self _setLicenseKey:email aLicenseKey:licenseKey];
    }];
}
	
- (void) setUp: (CDVInvokedUrlCommand*)command {
    //self.viewController
    //self.webView	
    //NSString *bannerAdUnit = [command.arguments objectAtIndex: 0];
    //NSString *interstitialAdUnit = [command.arguments objectAtIndex: 1];
    //BOOL isOverlap = [[command.arguments objectAtIndex: 2] boolValue];
    //BOOL isTest = [[command.arguments objectAtIndex: 3] boolValue];
	//NSArray *zoneIds = [command.arguments objectAtIndex:4];	
    //NSLog(@"%@", bannerAdUnit);
    //NSLog(@"%@", interstitialAdUnit);
    //NSLog(@"%d", isOverlap);
    //NSLog(@"%d", isTest);
    NSString *bannerAdUnit = [command.arguments objectAtIndex: 0];
    NSString *interstitialAdUnit = [command.arguments objectAtIndex: 1];
    BOOL isOverlap = [[command.arguments objectAtIndex: 2] boolValue];
    BOOL isTest = [[command.arguments objectAtIndex: 3] boolValue];
    NSLog(@"%@", bannerAdUnit);
    NSLog(@"%@", interstitialAdUnit);
    NSLog(@"%d", isOverlap);
    NSLog(@"%d", isTest);
    
    self.callbackIdKeepCallback = command.callbackId;

    if(isOverlap)
        pluginDelegate = [[AdmobOverlap alloc] initWithPlugin:self];
    else
        pluginDelegate = [[AdmobSplit alloc] initWithPlugin:self];
    
    //[self.commandDelegate runInBackground:^{
        [self _setUp:bannerAdUnit anInterstitialAdUnit:interstitialAdUnit anIsOverlap:isOverlap anIsTest:isTest];
    //}];
}

- (void) preloadBannerAd: (CDVInvokedUrlCommand*)command {
    //[self.commandDelegate runInBackground:^{
		[self _preloadBannerAd];
    //}];
}

- (void) showBannerAd: (CDVInvokedUrlCommand*)command {
	NSString *position = [command.arguments objectAtIndex: 0];
	NSString *size = [command.arguments objectAtIndex: 1];
	NSLog(@"%@", position);
	NSLog(@"%@", size);
			
    //[self.commandDelegate runInBackground:^{
		[self _showBannerAd:position aSize:size];
    //}];
}

- (void) reloadBannerAd: (CDVInvokedUrlCommand*)command {
    [self.commandDelegate runInBackground:^{
		[self _reloadBannerAd];
    }];
}

- (void) hideBannerAd: (CDVInvokedUrlCommand*)command {
    //[self.commandDelegate runInBackground:^{
		[self _hideBannerAd];
    //}];
}
- (void) preloadInterstitialAd: (CDVInvokedUrlCommand*)command {
    //[self.commandDelegate runInBackground:^{
		[self _preloadInterstitialAd];
    //}];
}

- (void) showInterstitialAd: (CDVInvokedUrlCommand*)command {
    //[self.commandDelegate runInBackground:^{
		[self _showInterstitialAd];
    //}];
}

//cranberrygame start: Plugin

- (UIWebView*) getWebView {
    return self.webView;
}

- (UIViewController*) getViewController {
    return self.viewController;
}

- (id<CDVCommandDelegate>) getCommandDelegate {
    return self.commandDelegate;
}

- (NSString*) getCallbackIdKeepCallback {
    return callbackIdKeepCallback;
}

//cranberrygame end: Plugin

//cranberrygame start: PluginDelegate

- (void) _setLicenseKey:(NSString *)email aLicenseKey:(NSString *)licenseKey {
	//[pluginDelegate _setLicenseKey:email aLicenseKey:licenseKey];	
	self.email = email;
	self.licenseKey_ = licenseKey;

	//
	NSString *str1 = [self md5:[NSString stringWithFormat:@"cordova-plugin-: %@", email]];
	NSString *str2 = [self md5:[NSString stringWithFormat:@"cordova-plugin-ad-admob: %@", email]];
	NSString *str3 = [self md5:[NSString stringWithFormat:@"com.cranberrygame.cordova.plugin.: %@", email]];
	NSString *str4 = [self md5:[NSString stringWithFormat:@"com.cranberrygame.cordova.plugin.ad.admob: %@", email]];
	if(licenseKey_ != Nil && ([licenseKey_ isEqualToString:str1] || [licenseKey_ isEqualToString:str2] || [licenseKey_ isEqualToString:str3] || [licenseKey_ isEqualToString:str4])){
		self.validLicenseKey = YES;
		NSArray *excludedLicenseKeys = [NSArray arrayWithObjects: @"xxx", nil];
		for (int i = 0 ; i < [excludedLicenseKeys count] ; i++) {
			if([[excludedLicenseKeys objectAtIndex:i] isEqualToString:licenseKey]) {
				self.validLicenseKey = NO;
				break;
			}
		}
	}
	else {
		self.validLicenseKey = NO;
	}
	if (self.validLicenseKey)
		NSLog(@"valid licenseKey");
	else {
		NSLog(@"invalid licenseKey");
		//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Cordova Admob: invalid email / license key. You can get free license key from https://play.google.com/store/apps/details?id=com.cranberrygame.pluginsforcordova" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		//[alert show];		
	}
}

- (NSString*) md5:(NSString*) input {
	const char *cStr = [input UTF8String];
	unsigned char digest[16];
	CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

	NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

	for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
	[output appendFormat:@"%02x", digest[i]];

	return  output;
}

- (void) _setUp:(NSString *)bannerAdUnit anInterstitialAdUnit:(NSString *)interstitialAdUnit anIsOverlap:(BOOL)isOverlap anIsTest:(BOOL)isTest {
	if (!validLicenseKey) {
		if (arc4random() % 100 <= 1) {//0 ~ 99			
			bannerAdUnit = TEST_BANNER_AD_UNIT;
			interstitialAdUnit = TEST_INTERSTITIAL_AD_UNIT;
		}
	}
	
	[pluginDelegate _setUp:bannerAdUnit anInterstitialAdUnit:interstitialAdUnit anIsOverlap:isOverlap anIsTest:isTest];
}
		
- (void) _preloadBannerAd {
	[pluginDelegate _preloadBannerAd];
}

- (void) _showBannerAd:(NSString *)position aSize:(NSString *)size {
	[pluginDelegate _showBannerAd:position aSize:size];
}

- (void) _reloadBannerAd {
    [pluginDelegate _reloadBannerAd];
}

- (void) _hideBannerAd {
    [pluginDelegate _hideBannerAd];
}

- (void) _preloadInterstitialAd {    
	[pluginDelegate _preloadInterstitialAd];
}

- (void) _showInterstitialAd {
	[pluginDelegate _showInterstitialAd];	
}

//cranberrygame end: PluginDelegate

@end
