using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using PokktExtension;
using PokktExtension.Droid;
using Java.Lang;

namespace SampleApp.Droid
{
	[Activity (Label = "VideoActivity")]			
	public class VideoActivity : Activity
	{
		private string TAG = "PokktXamarinSample";
		private bool isRewarded = false;
		private string screenName = "";
		private Button btnStartCaching;
		private Button btnPlayVideo;
		private Button btnPlayVideoNonIncent;
		private bool isPokktReady = false;
		private TextView screenNameTextView;
		private PokktConfig pokktConfig;
		private AdConfig adConfig;

		protected override void OnCreate (Bundle bundle)
		{
			Log.Debug (TAG, "OnCreate called");
			RequestWindowFeature (WindowFeatures.NoTitle);
			base.OnCreate (bundle);

			// Create your application here
			SetContentView(Resource.Layout.Video);

			screenNameTextView = (TextView)FindViewById(Resource.Id.textScreenName);
			btnPlayVideo = FindViewById<Button> (Resource.Id.button_video);
			btnPlayVideoNonIncent = FindViewById<Button> (Resource.Id.button_video_non_incent);

			isRewarded = Intent.GetBooleanExtra ("isRewarded", false);
			screenName = Intent.GetStringExtra ("screenName");

			//
			PokktExtension.PokktManager.SetNativeExtentions(new AndroidExtension(this));

			//Adding event
			PokktManager.Dispatcher.PokktInitialisedEvent += PokktManager_Dispatcher_PokktInitialisedEvent;
			PokktManager.Dispatcher.AdCachingCompletedEvent += PokktManager_Dispatcher_AdCachingCompletedEvent;
			PokktManager.Dispatcher.AdCachingFailedEvent += PokktManager_Dispatcher_AdCachingFailedEvent;
			PokktManager.Dispatcher.AdAvailabilityEvent += PokktManager_Dispatcher_AdAvailabilityEvent;
			PokktManager.Dispatcher.AdDisplayedEvent += PokktManager_Dispatcher_AdDisplayedEvent;
			PokktManager.Dispatcher.AdCompletedEvent += PokktManager_Dispatcher_AdCompletedEvent;
			PokktManager.Dispatcher.AdClosedEvent += PokktManager_Dispatcher_AdClosedEvent;
			PokktManager.Dispatcher.AdSkippedEvent += PokktManager_Dispatcher_AdSkippedEvent;
			PokktManager.Dispatcher.AdGratifiedEvent += PokktManager_Dispatcher_AdGratifiedEvent;

			pokktConfig = new PokktConfig ();
			setupPokkt ();

			screenNameTextView.Text = "Given Screen Name: " + screenName;
			adConfig = new AdConfig ();
			adConfig.ScreenName = screenName;
			adConfig.IsRewarded = isRewarded;

			if (isRewarded) {
				btnPlayVideo.Visibility = ViewStates.Visible;
				btnPlayVideoNonIncent.Visibility = ViewStates.Gone;
			} else {
				btnPlayVideo.Visibility = ViewStates.Gone;
				btnPlayVideoNonIncent.Visibility = ViewStates.Visible;
			}

			//Video Start Caching
			btnStartCaching = FindViewById<Button> (Resource.Id.button_start_cache);
			btnStartCaching.Click += delegate 
			{
				btnPlayVideo.Visibility = ViewStates.Gone;
				btnPlayVideoNonIncent.Visibility = ViewStates.Gone;
				checkVideoCache();
			};

			//With Incent
			btnPlayVideo.Click += delegate 
			{
				PokktManager.CheckAdAvailability(adConfig);
			};

			//Without Incent
			btnPlayVideoNonIncent.Click += delegate 
			{
				PokktManager.CheckAdAvailability(adConfig);
			};

			//((TextView) FindViewById(Resource.Id.points)).Text = "Points earned: " + Storage.Points;
		}

		void PokktManager_Dispatcher_PokktInitialisedEvent (bool isInitialised)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_PokktInitialisedEvent: " + isInitialised);
			isPokktReady = isInitialised;
		}

		void PokktManager_Dispatcher_AdCachingCompletedEvent (AdConfig adConfig, float rewardAmount)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdCachingCompletedEvent: " + rewardAmount);
			showButtons (rewardAmount, adConfig, true);
		}

		void PokktManager_Dispatcher_AdCachingFailedEvent (AdConfig adConfig, string failedMsg)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdCachingFailedEvent: " + failedMsg);
		}

		void PokktManager_Dispatcher_AdAvailabilityEvent (AdConfig adConfig, bool isAdAvailable)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdAvailabilityEvent: " + isAdAvailable);
			showButtons (PokktManager.GetRewardedAdVc (adConfig), adConfig, isAdAvailable);
			showAd (isAdAvailable, adConfig);
		}

		void PokktManager_Dispatcher_AdGratifiedEvent (AdConfig adConfig, float rewardPoint)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdGratifiedEvent: " + rewardPoint);
			RunOnUiThread (() => UpdateUI (rewardPoint));
		}

		void PokktManager_Dispatcher_AdSkippedEvent (AdConfig adConfig)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdSkippedEvent: " + adConfig);
		}

		void PokktManager_Dispatcher_AdClosedEvent (AdConfig adConfig, bool isBackPressed)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdClosedEvent: " + adConfig);
		}

		void PokktManager_Dispatcher_AdDisplayedEvent (AdConfig adConfig)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdDisplayedEvent: " + adConfig);
		}

		void PokktManager_Dispatcher_AdCompletedEvent (AdConfig adConfig)
		{
			Log.Info (TAG, "PokktManager_Dispatcher_AdCompletedEvent: " + adConfig);
		}

		void UpdateUI(float rewardPoint)
		{
			((TextView) FindViewById(Resource.Id.points)).Text = "Points earned: " + rewardPoint;
		}

		void setupPokkt() 
		{
			//PokktManager.AdCampaignDelegate = this;
			//PokktManager.SetActivity(this);//This method has to call very first time.

			PokktManager.SetDebug(true);
//			pokktConfig.SecurityKey = "iJ02lJss0M";
//			pokktConfig.ApplicationId = "a2717a45b835b5e9f50284a38d62a74e";
			pokktConfig.SecurityKey = "91f118562047d230f2b907ba93d2c4e2";
			pokktConfig.ApplicationId = "4317fb1fcbf1ca98badf08ef59b0b86b";
			pokktConfig.IntegrationType = "0";
			testOpetionalMethods ();

			PokktManager.InitPokkt (pokktConfig);
		}

		void testOpetionalMethods()
		{
			pokktConfig.ThirdPartyUserId = "1234";
			pokktConfig.CloseOnSuccessFlag = true;

			pokktConfig.Age = "32";
			pokktConfig.Birthday = "20/08/1983";
			pokktConfig.Education = "MCA";
			pokktConfig.EmailAddress = "pankaj454k@gmail.com";
			pokktConfig.Employment = "SE";
			pokktConfig.FacebookId = "pankaj454k";
			pokktConfig.Location = "Bangalore";
			pokktConfig.MaritalStatus = "Married";
			pokktConfig.MaturityRating = "for all";
			pokktConfig.MobileNo = "9900064040";
			pokktConfig.Name = "Pankaj";
			pokktConfig.Nationality = "Indian";
			pokktConfig.Sex = "Male";
			pokktConfig.TwitterHandle = "pankaj454k";
			pokktConfig.SelectedAnalyticsType = AnalyticsType.NONE;
		}

		void showAd(bool isAdAvailable, AdConfig adConfig)
		{
			if (isAdAvailable) {
				AdConfig config = new AdConfig();
				config.ScreenName = screenName;
				config.IsRewarded = isRewarded;
				config.ShouldAllowSkip = true;
				config.DefaultSkipTime = 10;
				config.BackButtonDisabled = true;
				if(isPokktReady) {
					PokktManager.ShowAd(config);
				} else {
					Toast.MakeText(this, "Pokkt SDK Not Ready ", ToastLength.Long).Show();
				}
			}
		}

		void showButtons(float value, AdConfig config, bool isAdAvailable) {

			RunOnUiThread (() => {
			screenNameTextView.Text = "Received Screen Name: " + config.ScreenName;
			});

			if(isAdAvailable) {
				if (isRewarded) {
					RunOnUiThread (() => {
						btnPlayVideo.Text = "Show Incent Ad To Earn " + value + " (Caching)";
						btnPlayVideo.Visibility = ViewStates.Visible;
						btnPlayVideoNonIncent.Visibility = ViewStates.Gone;
					});
				} else {
					RunOnUiThread (() => {
						btnPlayVideoNonIncent.Text = "Show Non Incent Ad To Earn 0.0 (Caching)";
						btnPlayVideo.Visibility = ViewStates.Gone;
						btnPlayVideoNonIncent.Visibility = ViewStates.Visible;
					});
				}
			}
		}

		protected void checkVideoCache()
		{
			Log.Debug("VideoActivity","Cache called");
			if(isPokktReady) {
				PokktManager.CacheAd (adConfig);
			} else {
				Toast.MakeText(this, "Pokkt SDK Not Ready ", ToastLength.Long).Show();
			}
		}

		protected override void OnResume()
		{
			Log.Debug (TAG, "OnResume called");
			base.OnResume ();
		}

		public override void OnBackPressed()
		{
			//This is used for Chartboost.
			//return type : true if chartboost ad is playing otherwise false.
			if (!PokktMediation.PokktMediationExtension.ChartboostBackKeyHandle ())
				base.OnBackPressed ();
		}

		protected override void OnDestroy()
		{
			Log.Debug (TAG, "onDestroy called");
			base.OnDestroy ();
			PokktManager.Dispatcher.PokktInitialisedEvent -= PokktManager_Dispatcher_PokktInitialisedEvent;
			PokktManager.Dispatcher.AdCachingCompletedEvent -= PokktManager_Dispatcher_AdCachingCompletedEvent;
			PokktManager.Dispatcher.AdCachingFailedEvent -= PokktManager_Dispatcher_AdCachingFailedEvent;
			PokktManager.Dispatcher.AdAvailabilityEvent -= PokktManager_Dispatcher_AdAvailabilityEvent;
			PokktManager.Dispatcher.AdDisplayedEvent -= PokktManager_Dispatcher_AdDisplayedEvent;
			PokktManager.Dispatcher.AdCompletedEvent -= PokktManager_Dispatcher_AdCompletedEvent;
			PokktManager.Dispatcher.AdClosedEvent -= PokktManager_Dispatcher_AdClosedEvent;
			PokktManager.Dispatcher.AdSkippedEvent -= PokktManager_Dispatcher_AdSkippedEvent;
			PokktManager.Dispatcher.AdGratifiedEvent -= PokktManager_Dispatcher_AdGratifiedEvent;
		}

	}
}

