using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
using PokktExtension;

namespace SampleApp.iOS
{
	partial class InterstitialScreenVC : UIViewController
	{
		string screenTitle;

		public InterstitialScreenVC (string screenName) : base ()
		{
			this.screenTitle = screenName;
		}

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();

			//Adding event
			PokktExtension.PokktAds.Interstitial.AdCachingCompletedEvent += AdCachingCompletedEvent;
			PokktExtension.PokktAds.Interstitial.AdCachingFailedEvent += AdCachingFailedEvent;
			PokktExtension.PokktAds.Interstitial.AdDisplayedEvent += AdDisplayedEvent;
			PokktExtension.PokktAds.Interstitial.AdCompletedEvent += AdCompletedEvent;
			PokktExtension.PokktAds.Interstitial.AdClosedEvent += AdClosedEvent;
			PokktExtension.PokktAds.Interstitial.AdSkippedEvent += AdSkippedEvent;
			PokktExtension.PokktAds.Interstitial.AdGratifiedEvent += AdGratifiedEvent;
			PokktExtension.PokktAds.Interstitial.AdFailedToShowEvent += AdFailedToShowEvent;
		}

		partial void showNonRewarded_TouchUpInside(UIButton sender)
		{
			PokktAds.Interstitial.ShowNonRewarded(screenTitle);
		}

		partial void showRewarded_TouchUpInside(UIButton sender)
		{
			PokktAds.Interstitial.ShowRewarded(screenTitle);
		}

		partial void CacheRewarded_TouchUpInside(UIButton sender)
		{
            if (!PokktAds.Interstitial.IsAdCached(screenTitle, true))
            {
                PokktAds.Interstitial.CacheRewarded(screenTitle);
                ChangeButtonText(CacheRewardedBtn, "Caching Rewarded");
                DisbaleButton(CacheRewardedBtn);
                DisbaleButton(ShowRewardedBtn);
            }else
            {
                ChangeButtonText(CacheRewardedBtn, "Ad already cached");
            }
			
		}

		partial void cacheNonRewarded_TouchUpInside(UIButton sender)
		{
            if (!PokktAds.Interstitial.IsAdCached(screenTitle, false))
            {
                PokktAds.Interstitial.CacheNonRewarded(screenTitle);
                ChangeButtonText(cacheNonRewardedBtn, "Caching NonRewarded");
                DisbaleButton(cacheNonRewardedBtn);
                DisbaleButton(showNonRewardedAdBtn);
            }
            ChangeButtonText(cacheNonRewardedBtn, "Ad already cached");
		}

		//Extra utility methods

		void ChangeButtonText(UIButton button, string text)
		{
			button.SetTitle (text, UIControlState.Normal);
			button.SetTitle (text, UIControlState.Highlighted);
			button.SetTitle (text, UIControlState.Disabled);
			button.SetTitle (text, UIControlState.Selected);
		}

		void EnableButton(UIButton button)
		{
			button.Enabled = true;
			button.Alpha = 1.0f;
		}

		void DisbaleButton(UIButton button)
		{
			button.Enabled = false;
			button.Alpha = 0.5f;
		}

		void ResetAllButton(bool isRewarded)
		{
			if (isRewarded)
			{
				ChangeButtonText(CacheRewardedBtn, "Cache Rewarded");
				ChangeButtonText(ShowRewardedBtn, "Show Rewarded");

				EnableButton(CacheRewardedBtn);
				EnableButton(ShowRewardedBtn);
			}
			else
			{
				ChangeButtonText(cacheNonRewardedBtn, "Cache NonRewarded");
				ChangeButtonText(showNonRewardedAdBtn, "Show NonRewarded");

				EnableButton(cacheNonRewardedBtn);
				EnableButton(showNonRewardedAdBtn);
			}
		}

		//Delegate handler

		void AdCachingCompletedEvent(string screenName, bool isRewarded, float reward)
		{
			Console.WriteLine(screenName);

			if (isRewarded)
			{
				EnableButton(ShowRewardedBtn);
				ChangeButtonText(ShowRewardedBtn, "Show Rewarded Ad (Cached)");
			}
			else
			{
				EnableButton(showNonRewardedAdBtn);
				ChangeButtonText(showNonRewardedAdBtn, "Show NonRewarded Ad (Cached)");
			}
		}

		void AdCachingFailedEvent(string screenName, bool isRewarded, string errorMessage)
		{
			Console.WriteLine("AdCachingFailedEvent called: " + errorMessage);
			ResetAllButton(isRewarded);
		}

		void AdGratifiedEvent(string screenName, float reward)
		{
			Console.WriteLine("AdGratifiedEvent called and reward point is: " + reward);
			earnedTxt.Text = "Earned point: " + reward.ToString();
		}

		void AdFailedToShowEvent(string screenName, bool isRewarded, string errorMessage)
		{
			Console.WriteLine("AdFailedToShowEvent called: " + errorMessage);
			ResetAllButton(isRewarded);
		}

		void AdSkippedEvent(string screenName, bool isRewarded)
		{
			Console.WriteLine("AdSkippedEvent called");
		}

		void AdClosedEvent(string screenName, bool isRewarded)
		{
			Console.WriteLine("AdClosedEvent called");
			ResetAllButton(isRewarded);
		}

		void AdDisplayedEvent(string screenName, bool isRewarded)
		{
			Console.WriteLine("AdDisplayedEvent called");
		}

		void AdCompletedEvent(string screenName, bool isRewarded)
		{
			Console.WriteLine("AdCompletedEvent called");
		}
	}
}
