using ImGuiNET;
using skyscraper5.Skyscraper.Equipment;
using skyscraper5.Skyscraper.Scraper.Storage;
using skyscraper5.src.Skyscraper.FrequencyListGenerator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Dvb.Descriptors;
using skyscraper8.Skyscraper.Scraper.Storage;

namespace SDL2Demo.Forms
{
	class BlindscanJobDeleter : IRenderable
	{
		private readonly DataStorage dataStorage;
		private readonly ObjectStorage objectStorage;
		private List<DbBlindscanJob> blindscanJobs;
		private Guid selectedGuid;
		private string tableGuid;
		public bool Closed { get; private set; }

		public BlindscanJobDeleter(DataStorage dataStorage,ObjectStorage objectStorage)
		{
			this.dataStorage = dataStorage;
			this.objectStorage = objectStorage;
			this.LoadPastBlindscans();
			this.tableGuid = Guid.NewGuid().ToString();
		}

		private void LoadPastBlindscans()
		{
			blindscanJobs = dataStorage.GetDbBlindscanJobs().ToList();
			selectedGuid = Guid.Empty;
		}

		private void DeleteSelectedJob()
		{
			IEnumerable<Tuple<int,SatelliteDeliverySystemDescriptor.PolarizationEnum>> frequencies = dataStorage.GetBlindscanResultFrequencies(selectedGuid);
			foreach (Tuple<int, SatelliteDeliverySystemDescriptor.PolarizationEnum> frequency in frequencies)
			{
				objectStorage.DeleteIqGraph(selectedGuid, frequency.Item1, frequency.Item2);
			}

			objectStorage.DeleteRfSpectrum(selectedGuid);

			dataStorage.DeleteBlindscanJob(selectedGuid);
			LoadPastBlindscans();
		}


		public void Render()
		{
			bool pOpen = true;
			ImGuiWindowFlags windowFlags = ImGuiWindowFlags.None;
			if (ImGui.Begin("Configure Dish Types", ref pOpen, windowFlags))
			{
				ImGui.Text(String.Format("{0} past jobs in DB.", blindscanJobs.Count));
				ImGui.Text(String.Format("Selected for deletion: {0}", selectedGuid.ToString()));
				ImGui.SameLine();

				bool nothingSelected = selectedGuid == Guid.Empty;

				ImGui.BeginDisabled(nothingSelected);
				if (ImGui.Button("Perform Deletion"))
					DeleteSelectedJob();
				ImGui.EndDisabled();

				ImGui.Separator();

				ImGui.BeginTable(tableGuid, 4);
				ImGui.TableNextRow();
				ImGui.TableSetColumnIndex(0);
				ImGui.Text("Date");

				ImGui.TableSetColumnIndex(1);
				ImGui.Text("Job");

				ImGui.TableSetColumnIndex(2);
				ImGui.Text("State");

				foreach(DbBlindscanJob blindscanJob in blindscanJobs)
				{
					ImGui.TableNextRow();
					ImGui.TableSetColumnIndex(0);
					ImGui.Text(blindscanJob.DateAdded.ToString("dd.MM.yyyy hh:mm"));

					ImGui.TableSetColumnIndex(1);
					ImGui.Text(blindscanJob.ToString(1));

					ImGui.TableSetColumnIndex(2);
					ImGui.Text(blindscanJob.ToString(2));

					ImGui.TableSetColumnIndex(3);
					ImGui.PushID(blindscanJob.JobGuid.ToString());
					if (ImGui.Button("Delete"))
					{
						this.selectedGuid = blindscanJob.JobGuid;
					}
					ImGui.PopID();
				}

				ImGui.EndTable();
				
			}
			ImGui.End();

			if (!pOpen)
				Closed = true;
		}
	}
}
