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

namespace SampleApp.iOS
{
	partial class DemoSelector : UIViewController
	{
		private bool isPokktInitialize;
		private PokktConfig pokktConfig;

		public DemoSelector (string nibName, NSBundle bundle) : base (nibName, bundle)
		{
		}

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

			DisableButton ();

			//Setting extension and adding event
			PokktExtension.PokktManager.SetNativeExtentions(new IosExtension());
			PokktManager.Dispatcher.PokktInitialisedEvent += PokktInitialisedEvent;
			PokktManager.Dispatcher.BannerLoadedEvent += BannerLoaded;
			PokktManager.Dispatcher.BannerLoadFailedEvent += BannerLoadFailed;


		}

		private void DisableButton()
		{
			DemoScreen1Btn.Enabled = false;
			DemoScreen2Btn.Enabled = false;
			testBannerBtn.Enabled = false;
			ExportLogBtn.Enabled = false;
			DemoScreen1Btn.Alpha = 0.5f;
			DemoScreen2Btn.Alpha = 0.5f;
			testBannerBtn.Alpha = 0.5f;
			ExportLogBtn.Alpha = 0.5f;
		}

		private void EnableButton()
		{
			InitializeBtn.Enabled = false;
			InitializeBtn.Alpha = 0.5f;
			DemoScreen1Btn.Enabled = true;
			DemoScreen2Btn.Enabled = true;
			testBannerBtn.Enabled = true;
			ExportLogBtn.Enabled = true;
			DemoScreen1Btn.Alpha = 1.0f;
			DemoScreen2Btn.Alpha = 1.0f;
			testBannerBtn.Alpha = 1.0f;
			ExportLogBtn.Alpha = 1.0f;
		}

		public override void DidReceiveMemoryWarning ()
		{
			base.DidReceiveMemoryWarning();
			// Dispose of any resources that can be recreated.
		}


		//Button click handler
		partial void InitializeBtn_TouchUpInside (UIButton sender)
		{
			if (isPokktInitialize) {
				return;
			}

			pokktConfig  = new PokktConfig();

			pokktConfig.ApplicationId = "a2717a45b835b5e9f50284a38d62a74e";
			pokktConfig.SecurityKey = "iJ02lJss0M";

			pokktConfig.ThirdPartyUserId = "123456";

			PokktManager.SetDebug(true);
			PokktManager.InitPokkt(pokktConfig);
		}

		partial void DemoScreen1Btn_TouchUpInside (UIButton sender)
		{
			AdConfig adConfig = new AdConfig();
			//adConfig.BackButtonDisabled = true;
			//adConfig.DefaultSkipTime = 5;
			adConfig.ScreenName = txtFldScreen.Text;
			adConfig.IsRewarded = true;
//			adConfig.ShouldAllowMute = true;
//			adConfig.ShouldAllowSkip = true;
//			adConfig.ShouldConfirmSkip = true;
//			adConfig.SkipConfirmMessage = "Are you sure?";
//			adConfig.SkipConfirmNoLabel = "NO";
//			adConfig.SkipConfirmYesLabel = "YES";
			ShowDemoScreen(adConfig);
		}

		partial void DemoScreen2Btn_TouchUpInside (UIButton sender)
		{
			AdConfig adConfig = new AdConfig();
			//adConfig.BackButtonDisabled = true;
			//adConfig.DefaultSkipTime = 7;
			adConfig.ScreenName = txtFldScreen.Text;
			adConfig.IsRewarded = false;
//			adConfig.ShouldAllowMute = true;
//			adConfig.ShouldAllowSkip = true;
//			adConfig.ShouldConfirmSkip = true;
//			adConfig.SkipConfirmMessage = "Are you sure want to quit?";
//			adConfig.SkipConfirmNoLabel = "No";
//			adConfig.SkipConfirmYesLabel = "Yes";
			ShowDemoScreen(adConfig);
		}

		partial void TestBannerBtn_TouchUpInside(UIButton sender)
		{
			PokktManager.LoadBanner(txtFldScreen.Text, (int)BannerPosition.TopCenter);
			//throw new NotImplementedException();
			//PokktManager
		}


		partial void ExportLogBtn_TouchUpInside (UIButton sender)
		{
			PokktManager.ExportLog();
		}

		private void ShowDemoScreen(AdConfig adConfig)
		{
			DemoScreen demoScreen = new DemoScreen (adConfig);
			this.NavigationController.PushViewController(demoScreen, true);
		}

		//Delegate handler
		void PokktInitialisedEvent (bool isInitialised, string message)
		{
			isPokktInitialize = isInitialised;
			if (isPokktInitialize) {
				InAppPurchaseDetails details = new InAppPurchaseDetails ();
				details.CurrencyCode = "INR";
				details.Description = "Desc";
				details.Price = 20;
				details.ProductId = "prod id";
				details.PurchaseData = "pur date";
				details.PurchaseSignature = "pur signature";
				details.PurchaseStore = IAPStoreType.AMAZON;
				PokktManager.TrackIAP (details);
				EnableButton ();
			}
		}

		void BannerLoaded (string screenName)
		{
			Console.WriteLine("BannerLoaded called:" + screenName);
		}

		void BannerLoadFailed(string screenName, string message)
		{
			Console.WriteLine("BannerLoadFailed called");

		}

	}
}
