
using System;
using System.Threading.Tasks;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;

using Android.Util;

using PokktExtension;

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

	public class OfferwallActivity : Activity
	{
		string TAG = "PokktXamarinSample";

		private EditText edt_points;
		private CheckBox chbox_close_success;
		private PokktConfig pokktConfig;
		private bool isPokktReady = false;

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

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

			this.Window.AddFlags(WindowManagerFlags.Fullscreen);

			pokktConfig = new PokktConfig();
			pokktConfig.ApplicationId = "954b975f9c753bcb8fca42624d78139a";
			pokktConfig.SecurityKey = "ae501f1003b0dc927dea807498ceaf3c";
			PokktManager.InitPokkt(pokktConfig);

			//setting pokkt related.
            testOpetionalMethods();
            PokktManager.Dispatcher.PokktInitialisedEvent += PokktInitialisedEvent;
			PokktManager.Dispatcher.CampaignAvailabilityEvent += CampaignAvailabilityEvent;
			PokktManager.Dispatcher.CoinResponseEvent += CoinResponseEvent;
			PokktManager.Dispatcher.CoinResponseFailedEvent += CoinResponseFailedEvent;
			PokktManager.Dispatcher.CoinResponseWithTransIdEvent += CoinResponseWithTransIdEvent;
			PokktManager.Dispatcher.OfferwallClosedEvent += OfferwallClosedEvent;

			chbox_close_success = (CheckBox) FindViewById(Resource.Id.chk_close_on_success);

			chbox_close_success.CheckedChange += (s, e) =>
			{
				if(e.IsChecked) {
					pokktConfig.CloseOnSuccessFlag = e.IsChecked;
				}
			};

			edt_points = (EditText) FindViewById(Resource.Id.edt_max_points);
			edt_points.SetTextColor (Android.Graphics.Color.Black);
			edt_points.SetOnClickListener(new EditTextClickAction(this));

			Button btnCoins = FindViewById<Button> (Resource.Id.get_coins);
			btnCoins.Click += delegate 
			{
				PokktManager.GetCoins(pokktConfig, this);
			};

			Button btnGetFixedCoins = FindViewById<Button> (Resource.Id.get_fixed_coins);
			btnGetFixedCoins.Click += delegate 
			{
				String temp = edt_points.Text;
				try {
					if (!"".Equals(temp)) {
						pokktConfig.OfferWallAssetValue = edt_points.Text;
						PokktManager.GetCoins(pokktConfig, this);
					}
					else{
						Toast.MakeText(this, "Please enter points", ToastLength.Short).Show();
					}
				}
				catch(Exception e) {
					Log.Debug(TAG, e.Message);
					Toast.MakeText(this, "Please enter valid points", ToastLength.Short).Show();
				}
					
			};

			Button btnPendingCoins = FindViewById<Button> (Resource.Id.pending_coins);
			btnPendingCoins.Click += delegate 
			{
				if(isPokktReady)
					PokktManager.GetPendingCoins(pokktConfig);
			};
		}

		
		void testOpetionalMethods()
		{
			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";
		}

		class EditTextClickAction : Java.Lang.Object, Android.Views.View.IOnClickListener
		{   
			public EditTextClickAction(Context Context)
			{
			}
			public void OnClick (View v)
			{
				EditText editText = (EditText)v;
				editText.Focusable = true;
				editText.FocusableInTouchMode = true;
			}
		}

		void updateUI(int points, string trans_id = "") {
			if (trans_id.Length > 0) {
				((TextView)FindViewById (Resource.Id.points)).Text = "Points earned: " + points;
			}
			else {
				((TextView)FindViewById (Resource.Id.points)).Text = "Points earned: " + points + " and transaction ID:" + trans_id;
			}
		}

		protected override void OnResume()
		{
			Task.Factory.StartNew(() => 
				{
					if(isPokktReady) {
						PokktManager.CheckOfferWallCampaign(pokktConfig);
						PokktManager.GetPendingCoins(pokktConfig);
					}
				});
			
			base.OnResume ();
		}

		void OfferwallClosedEvent (string param)
		{
			Log.Debug (TAG, "Offerwall closed");
		}

		void CoinResponseWithTransIdEvent (float earnedPoint, string transId)
		{
			Log.Debug (TAG, earnedPoint.ToString(), transId);
			//updateUI (coins);
		}

		void CoinResponseFailedEvent (string param)
		{
			Log.Debug (TAG, param);
		}

		void CoinResponseEvent (float earnedPoint)
		{
			Log.Debug (TAG, earnedPoint.ToString());
			//updateUI (param);
		}

		void CampaignAvailabilityEvent (string param)
		{
			Log.Debug (TAG, "Offerwall campaign available");
		}

		void PokktInitialisedEvent (bool isInitialised , String message)
		{
			isPokktReady = isInitialised;
		}
	}
}

