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

namespace SampleApp.iOS
{
	partial class DemoScreen : UIViewController
	{
		private AdConfig adConfig;

		public DemoScreen (AdConfig adConfig) : base ()
		{
			this.adConfig = adConfig;
		}

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

			//Adding event
			PokktManager.Dispatcher.AdCachingCompletedEvent += AdCachingCompletedEvent;
			PokktManager.Dispatcher.AdCachingFailedEvent += AdCachingFailedEvent;
			PokktManager.Dispatcher.AdAvailabilityEvent += AdAvailabilityEvent;
			PokktManager.Dispatcher.AdDisplayedEvent += AdDisplayedEvent;
			PokktManager.Dispatcher.AdCompletedEvent += AdCompletedEvent;
			PokktManager.Dispatcher.AdClosedEvent += AdClosedEvent;
			PokktManager.Dispatcher.AdSkippedEvent += AdSkippedEvent;
			PokktManager.Dispatcher.AdGratifiedEvent += AdGratifiedEvent;
		}

		partial void CacheVideoBtn_TouchUpInside(UIButton sender)
		{
			ChangeButtonText(CacheVideoBtn, "Caching Video Ad");
			DisbaleButton(CacheVideoBtn);
			DisbaleButton(ShowVideoBtn);
			adConfig.AdFormat = (int)AdFormat.Video;
			PokktManager.CacheAd(adConfig);		
		}

		partial void CacheInterstitialAdBtn_TouchUpInside(UIButton sender)
		{
			ChangeButtonText(CacheInterstitialAdBtn, "Caching Interstitial Ad");
			DisbaleButton(CacheInterstitialAdBtn);
			DisbaleButton(ShowInterstitialAdBtn);
			adConfig.AdFormat = (int)AdFormat.Interstitial;
			PokktManager.CacheAd(adConfig);		
		}

		partial void ShowVideoBtn_TouchUpInside(UIButton sender)
		{
			adConfig.AdFormat = (int)AdFormat.Video;
			PokktManager.ShowAd(adConfig);
		}

		partial void ShowInterstitialAdBtn_TouchUpInside(UIButton sender)
		{
			adConfig.AdFormat = (int)AdFormat.Interstitial;
			PokktManager.ShowAd(adConfig);
		}

		//Delegate handler

		void AdCachingCompletedEvent (AdConfig adConfig, float rewardAmount)
		{
			Console.WriteLine (adConfig);
			if (adConfig.AdFormat == (int)AdFormat.Video) {
				EnableButton (ShowVideoBtn);
				ChangeButtonText (ShowVideoBtn, "Show Video Ad (Cached)");
			} else {
				EnableButton (ShowInterstitialAdBtn);
				ChangeButtonText (ShowInterstitialAdBtn, "Show Interstitial Ad (Cached)");
			}
		}

		void AdCachingFailedEvent (AdConfig adConfig, string failedMsg)
		{
			Console.WriteLine ("AdCachingFailedEvent called: " + failedMsg);
			ResetAllButton (adConfig.AdFormat);
		}

		void AdAvailabilityEvent (AdConfig adConfig, bool isAdAvailable)
		{
			Console.WriteLine ("AdAvailabilityEvent called: " + isAdAvailable);
		}

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

		void AdSkippedEvent (AdConfig adConfig)
		{
			Console.WriteLine ("AdSkippedEvent called");
		}

		void AdClosedEvent (AdConfig adConfig, bool isBackPressed)
		{
			Console.WriteLine ("AdClosedEvent called");
			ResetAllButton (adConfig.AdFormat);
		}

		void AdDisplayedEvent (AdConfig adConfig)
		{
			Console.WriteLine ("AdDisplayedEvent called");
		}

		void AdCompletedEvent (AdConfig adConfig)
		{
			Console.WriteLine ("AdCompletedEvent called");
		}

		//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(int adFormat)
		{
			if (adFormat == 0)
			{
				ChangeButtonText(CacheVideoBtn, "Cache Video Ad");
				ChangeButtonText(ShowVideoBtn, "Show Video Ad");

				EnableButton(CacheVideoBtn);
				EnableButton(ShowVideoBtn);

			}
			else
			{
				ChangeButtonText(CacheInterstitialAdBtn, "Cache Interstitial Ad");
				ChangeButtonText(ShowInterstitialAdBtn, "Show Interstitial Ad");

				EnableButton(CacheInterstitialAdBtn);
				EnableButton(ShowInterstitialAdBtn);
			}
		}

	}
}
