using ImGuiNET;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Xml.Linq;
using skyscraper5.Skyscraper.Equipment;
using skyscraper8.Skyscraper.Scraper.Storage;

namespace SDL2Demo.Forms
{
	internal class ConfigureLnbTypes : IRenderable
	{
		private readonly DataStorage _scraperStroage;
		private List<LnbEntity> lnbTypes;
		private string tableUuid;
		private string lnbPopupUuid;

		private string manufacturer;
		private string name;
		private int lof1, lof2, lofSw, minFreq, maxFreq;

		public ConfigureLnbTypes(DataStorage scraperStroage, List<LnbEntity> lnbTypes)
		{
			_scraperStroage = scraperStroage;
			this.lnbTypes = lnbTypes;
			this.tableUuid = Guid.NewGuid().ToString();
			this.name = "";
			this.manufacturer = "";
			this.lnbPopupUuid = Guid.NewGuid().ToString();
		}

		public void Render()
		{
			bool pOpen = true;
			ImGuiWindowFlags windowFlags = ImGuiWindowFlags.AlwaysAutoResize;
			ImGui.Begin("Configure LNB Types", ref pOpen, windowFlags);
			if (ImGui.TreeNode("Add LNB Type"))
			{
				ImGui.InputText("Manufacturer", ref this.manufacturer, 64);
				ImGui.SameLine();
				ImGui.InputText("Name", ref name, 255);

				ImGui.InputInt("LOF1", ref lof1, 50, 100);
				ImGui.InputInt("LOF2", ref lof2, 50, 100);
				ImGui.InputInt("LOF-SW", ref lofSw, 50, 100);
				ImGui.InputInt("Min. Freq", ref minFreq, 50, 100);
				ImGui.InputInt("Max. Freq", ref maxFreq, 50, 100);

				if (ImGui.Button("Pick Template"))
				{
					ImGui.OpenPopup(lnbPopupUuid);
				}
				ImGui.SameLine();
				ImGui.BeginDisabled(!SaveButtonEnabled());
				if (ImGui.Button("Add"))
				{
					LnbEntity newLnb = new LnbEntity(manufacturer, name, lof1, lof2, lofSw, minFreq, maxFreq);
					lnbTypes.Add(newLnb);
					_scraperStroage.UiLnbTypesAdd(newLnb);
				}
				ImGui.EndDisabled();

				if (ImGui.BeginPopup(lnbPopupUuid))
				{
					if (ImGui.Selectable("Ku-Band"))
					{
						lof1 = 9750;
						lof2 = 10600;
						lofSw = 11700;
						minFreq = 10700;
						maxFreq = 12750;
					}

					if (ImGui.Selectable("C-Band"))
					{
						lof1 = 5150;
						lof2 = 0;
						lofSw = 0;
						minFreq = 3000;
						maxFreq = 4200;
					}

					ImGui.EndPopup();
				}
			}

			ImGui.Separator();


			ImGui.BeginTable(tableUuid, 3);
			ImGui.TableNextRow();
			ImGui.TableSetColumnIndex(0);
			ImGui.Text("Name");

			ImGui.TableSetColumnIndex(1);
			ImGui.Text("LOF1/LOF2/LOFSW");

			ImGui.TableSetColumnIndex(2);
			ImGui.Text("Frequency Range");
			foreach (LnbEntity lnbType in lnbTypes)
			{
				ImGui.TableNextRow();
				ImGui.TableSetColumnIndex(0);
				ImGui.Text(lnbType.Name);

				ImGui.TableSetColumnIndex(1);
				ImGui.Text(String.Format("{0}/{1}/{2}", lnbType.Lof1, lnbType.Lof2, lnbType.LofSw));

				ImGui.TableSetColumnIndex(2);
				ImGui.Text(String.Format("{0} -> {1} MHz", lnbType.MinimumFrequency, lnbType.MinimumFrequency));
			}
			ImGui.EndTable();


			ImGui.End();

			if (!pOpen)
				Closed = true;
		}

		private bool SaveButtonEnabled()
		{
			if (string.IsNullOrEmpty(name))
				return false;

			if (minFreq == 0)
				return false;

			if (maxFreq == 0)
				return false;

			if (minFreq > maxFreq)
				return false;

			if (lof1 == 0)
				return false;

			foreach (LnbEntity lnbType in lnbTypes)
			{
				if (lnbType.MinimumFrequency == minFreq &&
				    lnbType.MaximumFrequency == maxFreq &&
				    lnbType.Lof1 == lof1 &&
				    lnbType.Lof2 == lof2 &&
				    lnbType.LofSw == lofSw)
					return false;

				if (lnbType.Name.Equals(name))
					return false;
			}

			return true;
		}

		public bool Closed { get; set; }
	}
}
