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

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 PokktExtension.Enums;

namespace SampleApp.Droid
{
	[Activity (Label = "OfferwallActivity")]			
	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);

			//setting pokkt related.
			setupPokkt();

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

			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);
					}
					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 setupPokkt() 
		{
			PokktExtension.PokktManager.SetNativeExtentions(new AndroidExtension(this));

			pokktConfig = new PokktConfig ();
			PokktManager.SetDebug(true);

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

			//pokktConfig.SecurityKey = "91f118562047d230f2b907ba93d2c4e2";
			//pokktConfig.ApplicationId = "4317fb1fcbf1ca98badf08ef59b0b86b";

			//Amit: Android
			pokktConfig.SecurityKey = "81f2f0b8e67752ddd552f9b9e2c678f3";
			pokktConfig.ApplicationId = "0f666bc5d176ce734443a4fe00183d63";

			pokktConfig.IntegrationType = PokktIntegrationType.INTEGRATION_TYPE_OFFERWALL;

			testOpetionalMethods ();

			PokktManager.InitPokkt (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)
		{
			isPokktReady = isInitialised;
		}
	}
}

