package com.adsdk.sdk.customevents;

import com.chartboost.sdk.Chartboost;
import com.chartboost.sdk.ChartboostDelegate;
import com.chartboost.sdk.Chartboost.CBAgeGateConfirmation;
import com.chartboost.sdk.Model.CBError.CBClickError;
import com.chartboost.sdk.Model.CBError.CBImpressionError;

import android.app.Activity;

public class ChartboostFullscreen extends CustomEventFullscreen {
	
	private Chartboost chartboost;
	private boolean shouldDisplay;

	@Override
	public void loadFullscreen(Activity activity, CustomEventFullscreenListener customEventFullscreenListener, String optionalParameters, String trackingPixel) {
		String[] adIdParts = optionalParameters.split(";");
		String appId = adIdParts[0];
		String appSignature = adIdParts[1];		
		listener = customEventFullscreenListener;
		this.trackingPixel = trackingPixel;

		try {
			Class.forName("com.chartboost.sdk.Chartboost");
			Class.forName("com.chartboost.sdk.ChartboostDelegate");
			Class.forName("com.chartboost.sdk.Chartboost.CBAgeGateConfirmation");
			Class.forName("com.chartboost.sdk.Model.CBError.CBClickError");
			Class.forName("com.chartboost.sdk.Model.CBError.CBImpressionError");
		} catch (ClassNotFoundException e) {
			if (listener != null) {
				listener.onFullscreenFailed();
			}
			return;
		}
		chartboost = Chartboost.sharedChartboost();
		chartboost.onCreate(activity, appId, appSignature, createListener());

		chartboost.cacheInterstitial();
	}

	private ChartboostDelegate createListener() {
		return new ChartboostDelegate() {
			
			@Override
			public boolean shouldRequestMoreApps() {
				return false;
			}
			
			@Override
			public boolean shouldRequestInterstitialsInFirstSession() {
				return true;
			}
			
			@Override
			public boolean shouldRequestInterstitial(String arg0) {
				return true;
			}
			
			@Override
			public boolean shouldPauseClickForConfirmation(CBAgeGateConfirmation arg0) {
				return false;
			}
			
			@Override
			public boolean shouldDisplayMoreApps() {
				return false;
			}
			
			@Override
			public boolean shouldDisplayLoadingViewForMoreApps() {
				return false;
			}
			
			@Override
			public boolean shouldDisplayInterstitial(String arg0) {
				return shouldDisplay;
			}
			
			@Override
			public void didShowMoreApps() {
			}
			
			@Override
			public void didShowInterstitial(String arg0) {
				reportImpression();
				if (listener != null) {
					listener.onFullscreenOpened();
				}
				shouldDisplay = false;
			}
			
			@Override
			public void didFailToRecordClick(String arg0, CBClickError arg1) {
			}
			
			@Override
			public void didFailToLoadMoreApps(CBImpressionError arg0) {
			}
			
			@Override
			public void didFailToLoadInterstitial(String arg0, CBImpressionError arg1) {
				if (listener != null) {
					listener.onFullscreenFailed();
				}
			}
			
			@Override
			public void didDismissMoreApps() {
			}
			
			@Override
			public void didDismissInterstitial(String arg0) {
			}
			
			@Override
			public void didCloseMoreApps() {
			}
			
			@Override
			public void didCloseInterstitial(String arg0) {
				if (listener != null) {
					listener.onFullscreenClosed();
				}
			}
			
			@Override
			public void didClickMoreApps() {
			}
			
			@Override
			public void didClickInterstitial(String arg0) {
				if (listener != null) {
					listener.onFullscreenLeftApplication();
				}
			}
			
			@Override
			public void didCacheMoreApps() {
			}
			
			@Override
			public void didCacheInterstitial(String arg0) {
				if (listener != null) {
					listener.onFullscreenLoaded(ChartboostFullscreen.this);
				}
			}
		};
	}

	@Override
	public void showFullscreen() {
		if(chartboost!=null && chartboost.hasCachedInterstitial()) {
			shouldDisplay = true;
			chartboost.showInterstitial();
		}
	}

}

@@ -418,16 +418,44 @@
 tboost;%0A
+%09private Activity activity;%0A
 %09private
@@ -478,16 +478,16 @@
 isplay;%0A
-
 %0A%09@Overr
@@ -649,16 +649,44 @@
 ixel) %7B%0A
+%09%09this.activity = activity;%0A
 %09%09String
@@ -1044,174 +1044,16 @@
 sdk.
-Chartboost.CBAgeGateConfirmation%22);%0A%09%09%09Class.forName(%22com.chartboost.sdk.Model.CBError.CBClickError%22);%0A%09%09%09Class.forName(%22com.chartboost.sdk.Model.CBError.CBImpression
+Model.CB
 Erro
@@ -1295,16 +1295,48 @@
 ener());
+%0A%09%09chartboost.onStart(activity);
 %0A%0A%09%09char
@@ -1366,17 +1366,18 @@
 l();%0A%09%7D%0A
+%09
 %0A
-
 %09private
@@ -2865,32 +2865,102 @@
 (String arg0) %7B%0A
+%09%09%09%09if (listener != null) %7B%0A%09%09%09%09%09listener.onFullscreenClosed();%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09%0A%09%09%09@Ove
@@ -3078,78 +3078,8 @@
 ) %7B%0A
-%09%09%09%09if (listener != null) %7B%0A%09%09%09%09%09listener.onFullscreenClosed();%0A%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -3693,14 +3693,213 @@
 ();%0A
-
 %09%09%7D%0A%09%7D
+%0A%09%0A%09@Override%0A%09public void finish() %7B%0A%09%09if(chartboost != null) %7B%09%09%09%0A%09%09%09chartboost.onStop(activity);%0A%09%09%09chartboost.onDestroy(activity);%0A%09%09%7D%0A%09%09chartboost = null;%0A%09%09activity = null;%0A%09%09super.finish();%0A%09%7D
 %0A%0A%7D%0A

Fix Chartboost custom event.