using System;

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

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

	public class MainActivity : Activity
    {
        static int i = 0;
        string TAG = "PokktXamarinSample";
        private TextView txtVersionName;
        private EditText editTextScreenName;
        private Button buttonPokktInit, buttonNoRewarded, buttonRewardedAd, buttonBanner, buttonOfferwall, buttonExportLog;
		private bool isPokktReady = false;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            PokktManager.SetNativeExtentions(new AndroidExtension(this));
            PokktManager.SetDebug(true);
            SetContentView(Resource.Layout.main_screen_layout);

			this.Window.AddFlags(WindowManagerFlags.Fullscreen);

            String versionName = "v";
            try
            {

                versionName += PokktManager.GetPokktSDKVersion();
                Log.Debug(TAG, "Version No: " + versionName);
            }
            catch (Exception e)
            {
                Log.Debug(TAG, e.Message);
            }

            initUI();
            txtVersionName.Text = versionName;

            // Pokkt init
            PokktManager.Dispatcher.PokktInitialisedEvent += onPokktInitialised;
            PokktManager.Dispatcher.BannerLoadedEvent += onBannerLoaded;
            PokktManager.Dispatcher.BannerLoadFailedEvent += onBannerLoadFailed;
            buttonPokktInit.Click += delegate
            {
				if (isPokktReady) return;

                buttonPokktInit.Text = "Initializing";
                PokktConfig config = new PokktConfig();

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

                PokktManager.InitPokkt(config);
            };

            //Non-Rewarded Ad
            buttonNoRewarded.Click += delegate
            {
                if (editTextScreenName.Text.Length > 0)
                {
                    Intent intent = new Intent(this, typeof(VideoActivity));
                    intent.PutExtra("isRewarded", false);
                    intent.PutExtra("screenName", editTextScreenName.Text.ToString().Trim());
                    StartActivity(intent);
                }
                else
                {
                    Toast.MakeText(this, "screen name cannot be empty", ToastLength.Long).Show();
                }
            };

            //Rewarded Ad
            buttonRewardedAd.Click += delegate
            {
                if (editTextScreenName.Text.Length > 0)
                {
                    Intent intent = new Intent(this, typeof(VideoActivity));
                    intent.PutExtra("isRewarded", true);
                    intent.PutExtra("screenName", editTextScreenName.Text.ToString().Trim());
                    StartActivity(intent);
                }
                else
                {
                    Toast.MakeText(this, "screen name cannot be empty", ToastLength.Long).Show();
                }
            };

            // Pokkt banner
            buttonBanner.Click += delegate
            {
				if (editTextScreenName.Text.Length > 0)
				{
					PokktManager.LoadBanner(editTextScreenName.Text.ToString().Trim(), (int)BannerPosition.TopCenter, this);
					// PokktManager.BannerAutoRefresh(false);
				}
				else
				{
					Toast.MakeText(this, "screen name cannot be empty", ToastLength.Long).Show();
				}
            };

            //Offerwall
            buttonOfferwall.Click += delegate
            {
                StartActivity(new Intent(this, typeof(OfferwallActivity)));
            };

            // Export Log
           buttonExportLog.Click += delegate
           {
               PokktManager.ExportLog();
           };
        }


        private void initUI()
        {
            txtVersionName = FindViewById<TextView>(Resource.Id.tvVersion);
            editTextScreenName = (EditText)FindViewById(Resource.Id.editScreenName);
            buttonPokktInit = FindViewById<Button>(Resource.Id.button_pokkt_init);
            buttonNoRewarded = FindViewById<Button>(Resource.Id.button_no_rewarded);
            buttonRewardedAd = FindViewById<Button>(Resource.Id.button_rewarded_ad);
            buttonBanner = FindViewById<Button>(Resource.Id.button_banner);
            buttonOfferwall = FindViewById<Button>(Resource.Id.button_offerwall);
            buttonExportLog = FindViewById<Button>(Resource.Id.button_export_log);

            buttonNoRewarded.Enabled = false;
            buttonRewardedAd.Enabled = false;
            buttonBanner.Enabled = false;
            buttonOfferwall.Enabled = false;
        }

        public void onPokktInitialised(bool isReady, String message)
        {
            if (isReady)
            {
				isPokktReady = isReady;
                buttonPokktInit.Text = "Initialized";
				buttonPokktInit.Enabled = false;
                buttonNoRewarded.Enabled = true;
                buttonRewardedAd.Enabled = true;
                buttonBanner.Enabled = true;
                buttonOfferwall.Enabled = true;
            }
            else
            {
                Toast.MakeText(this, "Failed to initialize Pokkt SDK", ToastLength.Long).Show();
            }
        }

		private void showToast(string msg)
		{
			//TODO: Need to check how can we toast once we get callback from native
			//RunOnUiThread(() => Toast.MakeText(this, msg, ToastLength.Long));
		}

        public void onBannerLoaded(String screenName)
        {
			Console.WriteLine("Banner loaded" + i++);
			showToast("Banner loaded for Screen: " + screenName);
        }

        public void onBannerLoadFailed(String screenName, String errorMessage)
        {
			Console.WriteLine("Banner load failed");
				showToast("Banner load failed for Screen: " + screenName);
        }

        protected override void OnResume()
        {
            base.OnResume();
            PokktManager.resumeBanner(this, editTextScreenName.Text);           
        }



    }
}
