﻿#if UNITY_ANDROID
using System;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

using InMobiAds.Api;
using InMobiAds.Common;

namespace InMobiAds.Platforms.Android
{
	public class InterstitialClient: AndroidJavaProxy, IInterstitialClient
	{
		private AndroidJavaObject interstitialAd;
		public InterstitialClient (): base(Utils.InterstitialAdListenerClassName)
		{
			this.interstitialAd = new AndroidJavaObject (Utils.InterstitialAdClassName, this);
		
		}

		public event EventHandler<AdLoadSuccessEventArgs> OnAdLoadSucceeded;
		public event EventHandler<AdFetchSuccessEventArgs> OnAdFetchSuccessful;
		public event EventHandler<AdLoadFailedEventArgs> OnAdLoadFailed;
		public event EventHandler<AdFetchFailedEventArgs> OnAdFetchFailed;
		public event EventHandler<EventArgs> OnAdImpression;
		public event EventHandler<EventArgs> OnAdDisplayFailed;
		public event EventHandler<EventArgs> OnAdWillDisplay;
		public event EventHandler<EventArgs> OnAdDisplayed;
		public event EventHandler<EventArgs> OnAdDismissed;
		public event EventHandler<AdInteractionEventArgs> OnAdInteraction;
		public event EventHandler<EventArgs> OnUserLeftApplication;
		public event EventHandler<AdRewardActionCompletedEventArgs> OnAdRewardActionCompleted;

		public void CreateInterstitialAd(string placementId){
			this.interstitialAd.Call ("create", new object[1]{ placementId });
		}

		public void LoadAd(){
			this.interstitialAd.Call ("loadAd");
		}

		public bool IsReady(){
			return this.interstitialAd.Call<bool> ("isReady");
		}

		public void Show(){
			this.interstitialAd.Call ("show");
		}

		public void SetKeywords(string keywords){
			this.interstitialAd.Call ("setKeywords", new object[1]{ keywords });
		}

		//Set Extras for Interstitial Request
		public void SetExtras(Dictionary<string, string> extras)
		{
			this.interstitialAd.Call ("setExtras", new object[1]{ Utils.ConvertDictionaryToJavaMap(extras) });
		}

		public void onAdLoadSucceeded(String adMetaInfoStr)
		{
			JObject adMetaInfoJson = JObject.Parse(adMetaInfoStr);
			AdMetaInfo adMetaInfo = new AdMetaInfo () {
				Bid = (double)adMetaInfoJson["bid"],
				BidInfo = (JObject)adMetaInfoJson["BidInfo"],
				BidKeyword = (string)adMetaInfoJson["BidKeyword"],
				CreativeID = (string)adMetaInfoJson["CreativeId"],
			};
			AdLoadSuccessEventArgs args = new AdLoadSuccessEventArgs () {
				AdMetaInfo = adMetaInfo
			};
			this.OnAdLoadSucceeded (this, args);
		}

		public void onAdFetchSuccessful(String adMetaInfoStr)
		{
			JObject adMetaInfoJson = JObject.Parse(adMetaInfoStr);
			AdMetaInfo adMetaInfo = new AdMetaInfo () {
				Bid = (double)adMetaInfoJson["bid"],
				BidInfo = (JObject)adMetaInfoJson["BidInfo"],
				BidKeyword = (string)adMetaInfoJson["BidKeyword"],
				CreativeID = (string)adMetaInfoJson["CreativeId"],
			};
			AdFetchSuccessEventArgs args = new AdFetchSuccessEventArgs () {
				AdMetaInfo = adMetaInfo
			};
			this.OnAdFetchSuccessful (this, args);
		}

		public void onAdLoadFailed(string errorReason)
		{
			AdLoadFailedEventArgs args = new AdLoadFailedEventArgs () {
				Error = errorReason
			};
			this.OnAdLoadFailed(this, args);
		}

		public void onAdFetchFailed(string errorReason)
		{
			AdFetchFailedEventArgs args = new AdFetchFailedEventArgs () {
				Error = errorReason
			};
			this.OnAdFetchFailed(this, args);
		}

		public void onAdImpression() {
			this.OnAdImpression(this, EventArgs.Empty);
		}

		public void onAdDisplayFailed()
		{
			this.OnAdDisplayFailed (this, EventArgs.Empty);
		}

		public void onAdWillDisplay()
		{
			this.OnAdWillDisplay (this, EventArgs.Empty);
		}

		public void onAdDisplayed()
		{
			this.OnAdDisplayed (this, EventArgs.Empty);
		}

		public void onAdDismissed()
		{
			this.OnAdDismissed (this, EventArgs.Empty);
		}

		public void onAdInteraction(string message)
		{
			AdInteractionEventArgs args = new AdInteractionEventArgs () {
				Message = message
			};
			this.OnAdInteraction (this, args);
		}

		public void onUserLeftApplication()
		{
			this.OnUserLeftApplication (this, EventArgs.Empty);
		}

		public void onAdRewardActionCompleted(string rewards)
		{
			AdRewardActionCompletedEventArgs args = new AdRewardActionCompletedEventArgs () {
				Rewards = rewards
			};
			this.OnAdRewardActionCompleted (this, args);
		}
	}
}
#endif

