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

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 CacheRewardAdBtn_TouchUpInside (UIButton sender)
		{
			ChangeButtonText(CacheRewardAdBtn, "Caching Reward Ad");
			DisbaleButton(CacheRewardAdBtn);
			DisbaleButton(ShowRewardAdBtn);
			adConfig.IsRewarded = true;
			PokktManager.CacheAd(adConfig);
		}

		partial void CacheNonRewardAdBtn_TouchUpInside (UIButton sender)
		{
			ChangeButtonText(CacheNonRewardAdBtn, "Caching Non-Reward Ad");
			DisbaleButton(CacheNonRewardAdBtn);
			DisbaleButton(ShowNonRewardAdBtn);
			adConfig.IsRewarded = false;
			PokktManager.CacheAd(adConfig);
		}

		partial void ShowRewardAdBtn_TouchUpInside (UIButton sender)
		{
			adConfig.IsRewarded = true;
			PokktManager.ShowAd(adConfig);
		}

		partial void ShowNonRewardAdBtn_TouchUpInside (UIButton sender)
		{
			adConfig.IsRewarded = false;
			PokktManager.ShowAd(adConfig);
		}

		//Delegate handler

		void AdCachingCompletedEvent (AdConfig adConfig, float rewardAmount)
		{
			Console.WriteLine (adConfig);
			if (adConfig.IsRewarded) {
				EnableButton (ShowRewardAdBtn);
				ChangeButtonText (ShowRewardAdBtn, "Show Reward Ad (Cached)");
			} else {
				EnableButton (ShowNonRewardAdBtn);
				ChangeButtonText (ShowNonRewardAdBtn, "Show Non-Reward Ad (Cached)");
			}
		}

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

		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 ();
		}

		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()
		{
			ChangeButtonText (CacheRewardAdBtn, "Cache Reward Ad");
			ChangeButtonText (CacheNonRewardAdBtn, "Cache Non-Reward Ad");
			ChangeButtonText (ShowRewardAdBtn, "Show Reward Ad");
			ChangeButtonText (ShowNonRewardAdBtn, "Show Non-Reward Ad");
			EnableButton (CacheRewardAdBtn);
			EnableButton (CacheNonRewardAdBtn);
			EnableButton (ShowRewardAdBtn);
			EnableButton (ShowNonRewardAdBtn);
		}

	}
}
