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

namespace SDL2Demo
{
	internal class FoundFrequenciesWindow2 : IRenderable
	{
		private readonly List<BlindscanSearchResult> _foundFrequencies;
		public bool doNotAutoZap;
		public bool allowZapNow;
		public bool zapNowRequested;
		private string tableUuid;
		public long statusPacketsInTotal;
		public long statusPacketsInQueue;

		public FoundFrequenciesWindow2(List<BlindscanSearchResult> foundFrequencies)
		{
			_foundFrequencies = foundFrequencies;
			tableUuid = Guid.NewGuid().ToString();
		}

		public void Render()
		{
			if (_foundFrequencies.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 < _foundFrequencies.Count; i++)
					{
						BlindscanSearchResult blindscanResult = _foundFrequencies[i];
						ImGui.TableNextRow();
						if (blindscanResult.IsSatellite())
						{
							ImGui.TableSetColumnIndex(0);
							ImGui.Text(String.Format("{0} {1} ", blindscanResult.SearchResult.Freq / 1000,
								blindscanResult.SearchResult.Pol == 0 ? "H" : "V"));

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

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

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

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

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

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