using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
using Telerik.Blazor.Components;
using TSC2.Components;

namespace TSC2.Components.Pages
{
	public partial class Home
	{
		TelerikMap MapRef { get; set; } = new TelerikMap();
		public double Zoom { get; set; } = 1;
		public static double[] Center = [40.68, 74.04];
		public string MapHeight { get; set; } = "800px";
		public string MapWidth { get; set; } = "1300px";
		public string ShopName { get; set; } = "";
		public string CSZ { get; set; } = "";

		public string UrlTemplate { get; set; } = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png";


		private bool initialized = false;


		// User info
		string ip = "";

		protected override void OnInitialized() // Executes every page load; run inexpensive rendering code here
		{
            InitializeMapMarkers(Center);
			MapRef?.Refresh();
			Center = [42.4649, -83.3684];			
			Zoom = 11;

			InitializeMapMarkers(Center);
            MapRef?.Refresh();	   
		}



		public void OnMarkerClick(MapMarkerClickEventArgs args)
		{

			var dataItem = args.DataItem as MarkerModel;
			CurrentSelection = dataItem.Title;
			CurrentBlurb = dataItem.Blurb;
			CurrentUniqueID = dataItem.UniqueID;

			ToggleModal();
		}



		public List<MarkerModel> MapMarkers { get; set; } = new List<MarkerModel>();



		private void InitializeMapMarkers(double[] coords)
		{
			// Server call to load map markers into a list of tuples
			List<Tuple<double[], string, string>> localMarkers = MapDriver.InitializeMarkers();
			foreach (var current in localMarkers)
			{
				MapMarkers.Add(new MarkerModel()
				{
					LatLng = current.Item1,
					Title = current.Item2,
					UniqueID = current.Item3,
					Blurb = string.Format("This is {0}'s blurb. This will be replaced when a lookup is created.", current.Item2)
				});
			}
		}



		public void SubmitLocation()
		{
			string str = String.Format(("Shop Name: {0} \n CSZ: {1}"), ShopName, CSZ);
			Console.WriteLine(str);

			// Search based on the input provided in the ShopName and CSZ fields
			// TODO

			// Call Geocoding API to get coordinates for making the map jump
			// TODO
			var jsonString = "{\"latitude\": \"42.295920\",\"longitude\":\"-83.444780\"}";
			LocationDetails? json = JsonConvert.DeserializeObject<LocationDetails>(jsonString);

			// Move the map
			if (json != null)
			{
				Center = [json.latitude, json.longitude];
				Zoom = 13;
			}

			InitializeMapMarkers(Center);
			if (MapRef != null)
				MapRef.Refresh();
		}

		public class MarkerModel
		{
			public double[] LatLng { get; set; } = [42.4649, -83.3684];
			public string Title { get; set; } = "";
			public string Blurb { get; set; } = "";
			public string UniqueID { get; set; } = "";
		}

		private class LocationDetails
		{
			public string ip { get; set; } = "";
			public string network { get; set; } = "";
			public string version { get; set; } = "";
			public string city { get; set; } = "";
			public string region { get; set; } = "";
			public string region_code { get; set; } = "";
			public string country { get; set; } = "";
			public string country_code { get; set; } = "";
			public string country_code_iso3 { get; set; } = "";
			public string country_capital { get; set; } = "";
			public string country_tld { get; set; } = "";
			public string continent_code { get; set; } = "";
			public bool in_eu { get; set; } = false;
			public string postal { get; set; } = "";
			public double latitude { get; set; } = -1;
			public double longitude { get; set; } = -1;
			public string timezone { get; set; } = "";
			public string utc_offset { get; set; } = "";
			public string country_calling_code { get; set; } = "";
			public string currency { get; set; } = "";
			public string currency_name { get; set; } = "";
			public string languages { get; set; } = "";
			public string country_area { get; set; } = "";
			public string country_population { get; set; } = "";
			public string asn { get; set; } = "";
			public string org { get; set; } = "";
		}

		public TelerikPopover? Popover { get; set; }
		public string CurrentSelection { get; set; } = "123412341234onetwothreefour";
		public string CurrentBlurb { get; set; } = "This is a blurb for a shop. I believe it is rather dastardly.";
		public string CurrentUniqueID { get; set; } = "";

		public bool isVisible = false;

		public void ToggleModal()
		{
            //await Console.Out.WriteLineAsync("IWNAJNQADJK" + isVisible);
            if (isVisible)
				isVisible = false;
			else
				isVisible = true;
			//await Task.Delay(1);

			MapRef?.Refresh();
		}
	}
}