using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Util;
using PokktExtension;
using PokktExtension.Enums;
using PokktExtension.Droid;

namespace SampleApp.Droid
{
    [Activity(Label = "VideoActivity", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]

	public class VideoActivity : Activity
    {
        private string TAG = "PokktXamarinSample";
        private bool isRewarded = false;
        private string screenName = "";

        private Button btnPlayVideoWithIncent;
        private Button btnPlayVideoWithoutIncent;
        private Button btnStartVideoCaching;

        private Button btnInterstitialWithIncent;
        private Button btnInterstitialWithoutIncent;
        private Button btnStartInterstitialCaching;

        private TextView screenNameTextView;
        private AdConfig videoAdConfig;
        private AdConfig interstitialAdConfig;
		private float totalRewardPoint = 0;

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

			this.Window.AddFlags(WindowManagerFlags.Fullscreen);

            screenNameTextView = (TextView)FindViewById(Resource.Id.textScreenName);

            btnStartVideoCaching = FindViewById<Button>(Resource.Id.button_start_video_cache);
            btnPlayVideoWithIncent = FindViewById<Button>(Resource.Id.button_incent_video_without_cache);
            btnPlayVideoWithoutIncent = FindViewById<Button>(Resource.Id.button_non_incent_video_without_cache);

            btnStartInterstitialCaching = FindViewById<Button>(Resource.Id.button_start_interstitial_cache);
            btnInterstitialWithIncent = FindViewById<Button>(Resource.Id.button_incent_interstitial_without_cache);
            btnInterstitialWithoutIncent = FindViewById<Button>(Resource.Id.button_non_incent_interstitial_without_cache);

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

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

            //Adding event

            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;

           // testOpetionalMethods();
            screenNameTextView.Text = "Given Screen Name: " + screenName;

            btnStartVideoCaching.Click += delegate
            {
				btnStartVideoCaching.Text = "Caching started. Please wait!";
                btnPlayVideoWithIncent.Visibility = ViewStates.Gone;
                btnPlayVideoWithoutIncent.Visibility = ViewStates.Gone;

                PokktManager.CacheAd(videoAdConfig);
            };

            btnStartInterstitialCaching.Click += delegate
            {
				btnStartInterstitialCaching.Text = "Caching started. Please wait!";
                btnInterstitialWithIncent.Visibility = ViewStates.Gone;
                btnInterstitialWithoutIncent.Visibility = ViewStates.Gone;

                PokktManager.CacheAd(interstitialAdConfig);
            };


            //configure video adConfig
            videoAdConfig = new AdConfig();
            videoAdConfig.ScreenName = screenName;
            videoAdConfig.IsRewarded = isRewarded;
            videoAdConfig.AdFormat = (int)AdFormat.Video;
            videoAdConfig.ShouldAllowSkip=true;
            videoAdConfig.DefaultSkipTime = 10;
            videoAdConfig.BackButtonDisabled = true;

            //configure interstitial adConfig
            interstitialAdConfig = new AdConfig();
            interstitialAdConfig.ScreenName = screenName;
            interstitialAdConfig.IsRewarded = isRewarded;
            interstitialAdConfig.AdFormat = (int)AdFormat.Interstitial;

            if (isRewarded)
            {
                btnPlayVideoWithIncent.Visibility = ViewStates.Visible;
                btnPlayVideoWithoutIncent.Visibility = ViewStates.Gone;

                btnInterstitialWithIncent.Visibility = ViewStates.Visible;
                btnInterstitialWithoutIncent.Visibility = ViewStates.Gone;
            }
            else
            {
                btnPlayVideoWithIncent.Visibility = ViewStates.Gone;
                btnPlayVideoWithoutIncent.Visibility = ViewStates.Visible;

                btnInterstitialWithIncent.Visibility = ViewStates.Gone;
                btnInterstitialWithoutIncent.Visibility = ViewStates.Visible;
            }

            btnPlayVideoWithIncent.Click += delegate
            {
                PokktManager.CheckAdAvailability(videoAdConfig);
            };

            btnPlayVideoWithoutIncent.Click += delegate
        	{
            	PokktManager.CheckAdAvailability(videoAdConfig);
        	};

            btnInterstitialWithIncent.Click += delegate
            {
                PokktManager.CheckAdAvailability(interstitialAdConfig);
            };

            btnInterstitialWithoutIncent.Click += delegate
            {
                PokktManager.CheckAdAvailability(interstitialAdConfig);
            };
        }

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

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

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

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

            //Hard coded value for testing API is working or not.
            //InAppPurchaseDetails purchaseDetails = new InAppPurchaseDetails();
            //purchaseDetails.CurrencyCode = "IN";
            //purchaseDetails.Description = "description";
            //purchaseDetails.Price = 10;
            //purchaseDetails.ProductId = "Product Id";
            //purchaseDetails.PurchaseData = "date";
            //purchaseDetails.PurchaseSignature = "signature";
            //purchaseDetails.PurchaseStore = IAPStoreType.AMAZON;
            //PokktManager.TrackIAP(purchaseDetails);
        }

        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)
        {
			totalRewardPoint = totalRewardPoint + rewardPoint;
            ((TextView)FindViewById(Resource.Id.points)).Text = "Points earned: " + totalRewardPoint;
        }

        void showAd(bool isAdAvailable, AdConfig adConfig)
        {

            if (isAdAvailable)
            {
                PokktManager.ShowAd(adConfig, this);
            }
            else
            {
                Toast.MakeText(this, "Ad not available ", 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 ();
        }

		void resetButtonsText(AdConfig config)
		{
			if (config.AdFormat == (int) AdFormat.Video)
			{
				btnStartVideoCaching.Text = "Start Video Caching";
				if (config.IsRewarded)
				{
					btnPlayVideoWithIncent.Visibility = ViewStates.Visible;
					btnPlayVideoWithIncent.Text = "Show Incent Video (Without Caching)";
				}
				else
				{
					btnPlayVideoWithoutIncent.Visibility = ViewStates.Visible;
					btnPlayVideoWithoutIncent.Text = "Show Non-Incent Video (Without Caching))";
				}	
			}
			if (config.AdFormat == (int) AdFormat.Interstitial)
			{
				btnStartInterstitialCaching.Text = "Start Interstitial Caching";
				if (config.IsRewarded)
				{
					btnInterstitialWithIncent.Visibility = ViewStates.Visible;
					btnInterstitialWithIncent.Text = "Show Incent Interstitial (Without Caching)";
				}
				else
				{
					btnInterstitialWithoutIncent.Visibility = ViewStates.Visible;
					btnInterstitialWithoutIncent.Text = "Show Non-Incent Interstitial (Without Caching))";
				}
			}
		}

		void showButtons(AdConfig config, float value, bool isAdAvailable)
		{
			btnStartVideoCaching.Text = "Start Video Caching";
			if (isAdAvailable)
			{
				if (isRewarded)
				{
					if (config.AdFormat == (int) AdFormat.Video)
					{
						btnPlayVideoWithIncent.Text = "Show Incent Ad To Earn " + value + " (Cached)";
						btnPlayVideoWithIncent.Visibility = ViewStates.Visible;
						btnPlayVideoWithoutIncent.Visibility = ViewStates.Gone;
					}
					else if (config.AdFormat == (int) AdFormat.Interstitial)
					{
						btnInterstitialWithIncent.Text = "Show Incent Ad To Earn " + value + " (Cached)";
						btnInterstitialWithIncent.Visibility = ViewStates.Visible;
						btnInterstitialWithoutIncent.Visibility = ViewStates.Gone;
					}
				}
				else {
					if (config.AdFormat == (int)AdFormat.Video)
					{
						btnPlayVideoWithoutIncent.Text = "Show Non Incent Ad To Earn 0.0 (Cached)";
						btnPlayVideoWithIncent.Visibility = ViewStates.Gone;
						btnPlayVideoWithoutIncent.Visibility = ViewStates.Visible;
					}
					else if (config.AdFormat == (int) AdFormat.Interstitial)
					{
						btnInterstitialWithoutIncent.Text = "Show Non Incent Ad To Earn 0.0 (Cached)";
						btnInterstitialWithIncent.Visibility = ViewStates.Gone;
						btnInterstitialWithoutIncent.Visibility = ViewStates.Visible;
					}
				}
			}
			else {
				if (isRewarded)
				{
					if (config.AdFormat == (int) AdFormat.Video)
					{
						btnPlayVideoWithIncent.Visibility = ViewStates.Visible;
						btnPlayVideoWithoutIncent.Visibility = ViewStates.Gone;
					}
					else if (config.AdFormat == (int) AdFormat.Interstitial)
					{
						btnInterstitialWithIncent.Visibility = ViewStates.Visible;
						btnInterstitialWithoutIncent.Visibility = ViewStates.Gone;
					}
				}
				else {
					if (config.AdFormat == (int)AdFormat.Video)
					{
						btnPlayVideoWithIncent.Visibility = ViewStates.Gone;
						btnPlayVideoWithoutIncent.Visibility = ViewStates.Visible;
					}
					else if (config.AdFormat == (int)AdFormat.Interstitial)
					{
						btnInterstitialWithIncent.Visibility = ViewStates.Gone;
						btnInterstitialWithoutIncent.Visibility = ViewStates.Visible;
					}
				}
			}
		}

        protected override void OnDestroy()
        {
            Log.Debug(TAG, "onDestroy called");
            base.OnDestroy();
            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;
        }

    }
}

