using ImGuiNET;
using skyscraper5.Skyscraper.IO.CrazycatStreamReader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SDL2Demo.Jobs;
using skyscraper8.Skyscraper.FrequencyListGenerator;
using static SDL2Demo.Jobs.Blindscan;

namespace SDL2Demo.Forms
{

	class FoundFrequenciesWindow : IRenderable
	{
		private readonly List<BlindscanResult> _blindscanResults;
		private readonly STD_TYPE _standard;
		private string tableUuid;
		private JobContext jobContext;

		public bool zapNowRequested;
		public bool allowZapNow;
		public bool doNotAutoZap;
		internal ulong statusPacketsInTotal;
		internal int statusPacketsInqueue;

		public FoundFrequenciesWindow(List<BlindscanResult> blindscanResults, STD_TYPE standard,
			JobContext jobContext)
		{
			_blindscanResults = blindscanResults;
			_standard = standard;
			tableUuid = Guid.NewGuid().ToString();
			this.jobContext = jobContext;
		}

		public void Render()
		{
			if (this.jobContext.MemorySaverMode)
				return;

			if (_blindscanResults.Count == 0)
				return;

			if (ImGui.Begin("Blind-Scan Results"))
			{
				ImGui.Checkbox("Do not Auto-Zap", ref doNotAutoZap);
				ImGui.SameLine();
				ImGui.BeginDisabled(!allowZapNow);
				if (ImGui.Button("Zap now"))
				{
					zapNowRequested = true;
				}
				ImGui.EndDisabled();

				if (allowZapNow)
				{
					ImGui.Text(String.Format("{0} packets received in total, {1} queued.", statusPacketsInTotal, statusPacketsInqueue));
				}

				if (ImGui.BeginTable(tableUuid, 5,
						ImGuiTableFlags.NoSavedSettings | ImGuiTableFlags.SizingFixedFit))
				{
					for (int i = 0; i < _blindscanResults.Count; i++)
					{
						BlindscanResult blindscanResult = _blindscanResults[i];
						ImGui.TableNextRow();
						if (_standard == STD_TYPE.STD_DVBS)
						{
							ImGui.TableSetColumnIndex(0);
							ImGui.Text(String.Format("{0} {1} ", blindscanResult.sr1.Freq / 1000,
								blindscanResult.sr1.Pol == 0 ? "H" : "V"));

							ImGui.TableSetColumnIndex(1);
							ImGui.Text((blindscanResult.sr1.SR / 1000).ToString());

							ImGui.TableSetColumnIndex(2);
							ImGui.Text(((MOD_TYPE)blindscanResult.sr1.ModType).ToString());

							ImGui.TableSetColumnIndex(3);
							ImGui.Text((blindscanResult.sr1.MIS + 1).ToString());
						}
						else
						{
							ImGui.TableSetColumnIndex(0);
							ImGui.Text((blindscanResult.sr2.Freq / 1000).ToString());

							ImGui.TableSetColumnIndex(1);
							ImGui.Text((blindscanResult.sr2.SR / 10).ToString());

							ImGui.TableSetColumnIndex(2);
							ImGui.Text(((MOD_TYPE)blindscanResult.sr2.ModType).ToString());
						}

						ImGui.TableSetColumnIndex(4);
						ImGui.Text(blindscanResult.State.ToString());
					}
					ImGui.EndTable();
				}
				ImGui.End();
			}
		}

		public void Add(BlindscanSearchResult searchResult, int polarityIndex, int lnbTypeMinimumFrequency, int i)
		{
			if (searchResult.IsSatellite())
				_blindscanResults.Add(new Blindscan.BlindscanResult(searchResult.SearchResult, lnbTypeMinimumFrequency, i));
			else
				_blindscanResults.Add(new BlindscanResult(searchResult.SearchResult2, lnbTypeMinimumFrequency, i));
		}

		public void Clear()
		{
			_blindscanResults.Clear();
		}
	}

}
