using ImGuiNET;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.Equipment;
using skyscraper5.Skyscraper.Gps;
using skyscraper5.Skyscraper.IO;
using skyscraper8.Skyscraper.FrequencyListGenerator;
using skyscraper8.Skyscraper.Scraper.Storage;
using skyscraper8.UI.SDL2.Jobs;

namespace SDL2Demo.Jobs
{
	internal class InheritedBlindscanConfigWindow : IRenderable
	{
		private bool windowOpen;
		private List<TunerMetadata> tuners;
		private List<SatellitePositionEntity> satellites;
		private List<LnbEntity> lnbs;
		private List<DishEntity> dishes;
		private readonly TaskQueue _taskQueue;

		public int settingsWindowBLScanTunerSelection;
		public int multitunerMode;
		public int settingsWindowSetFilterTunerSelection;
		public int settingsWindowDiseqc;
		public bool settingsWindowCollectIqGraphs;
		public bool settingsWindowCollectRfSpectrum;
		public bool settingsWindowCaptureFile;
		public int settingsWindowSatellite;
		public int settingsWindowLNB;
		public int settingsWindowDish;

		public bool settingsWindowScanHorizontalLow;
		public bool settingsWindowScanHorizontalHigh;
		public bool settingsWindowScanVerticalLow;
		public bool settingsWindowScanVerticalHigh;

		

		private DataStorage dataStorage;
		private ObjectStorage objectStorage;
		private IGpsReceiver gpsReceiver;
		private Ini ini;
		private IStreamReader streamReader;

		private void StoreSettings()
		{
			dataStorage.InsertUiBlindscanSettingHistory(settingsWindowBLScanTunerSelection,
				multitunerMode, settingsWindowSetFilterTunerSelection,
				settingsWindowDiseqc, settingsWindowCollectIqGraphs,
				settingsWindowCollectRfSpectrum, settingsWindowCaptureFile, settingsWindowSatellite, settingsWindowLNB,
				settingsWindowDish, settingsWindowScanHorizontalLow, settingsWindowScanHorizontalHigh,
				settingsWindowScanVerticalLow, settingsWindowScanVerticalHigh);
		}
		public InheritedBlindscanConfigWindow(List<TunerMetadata> tunerMetadatas,
			List<SatellitePositionEntity> satellitePositions, List<LnbEntity> lnbTypes, List<DishEntity> dishTypes,
			DataStorage dataStorage, ObjectStorage objectStorage, IGpsReceiver gps, Ini ini, IStreamReader streamReader,
			TaskQueue taskQueue)
		{
			tuners = tunerMetadatas;
			satellites = satellitePositions;
			lnbs = lnbTypes;
			dishes = dishTypes;
			windowOpen = true;
			settingsWindowDiseqc = 1;
			this.dataStorage = dataStorage;
			this.objectStorage = objectStorage;
			this.gpsReceiver = gps;
			this.ini = ini;
			this.streamReader = streamReader;
			_taskQueue = taskQueue;

			object[] previousSettings = dataStorage.GetLastUiBlindscanSettings();
			if (previousSettings != null)
			{
				settingsWindowBLScanTunerSelection = Convert.ToInt32(previousSettings[1]);
				multitunerMode = Convert.ToInt32(previousSettings[2]);
				settingsWindowSetFilterTunerSelection = Convert.ToInt32(previousSettings[3]);
				settingsWindowDiseqc = Convert.ToInt32(previousSettings[4]);
				settingsWindowCollectIqGraphs = Convert.ToBoolean(previousSettings[5]);
				settingsWindowCollectRfSpectrum = Convert.ToBoolean(previousSettings[6]);
				settingsWindowCaptureFile = Convert.ToBoolean(previousSettings[7]);
				settingsWindowSatellite = Convert.ToInt32(previousSettings[8]);
				settingsWindowLNB = Convert.ToInt32(previousSettings[9]);
				settingsWindowDish = Convert.ToInt32(previousSettings[10]);

				settingsWindowScanHorizontalLow = Convert.ToBoolean(previousSettings[11]);
				settingsWindowScanHorizontalHigh = Convert.ToBoolean(previousSettings[12]);
				settingsWindowScanVerticalLow = Convert.ToBoolean(previousSettings[13]);
				settingsWindowScanVerticalHigh = Convert.ToBoolean(previousSettings[14]);
			}
		}
		public bool IsWindowOpen()
		{
			return windowOpen;
		}
		public BlindscanJobConfiguration GetConfiguration()
		{
			if (!okButtonClicked)
				return null;

			BlindscanJobConfiguration bjc = new BlindscanJobConfiguration();
			bjc.DataStorage = dataStorage;
			bjc.DiseqcIndex = settingsWindowDiseqc;
			bjc.DoHorizontalLow = settingsWindowScanHorizontalLow;
			bjc.DoHorizontalHigh = settingsWindowScanHorizontalHigh;
			bjc.DoVerticalLow = settingsWindowScanVerticalLow;
			bjc.DoVerticalHigh = settingsWindowScanVerticalHigh;
			bjc.DoCollectIqGraphs = settingsWindowCollectIqGraphs;
			bjc.DoCollectRfSpectrum = settingsWindowCollectRfSpectrum;
			bjc.DoRecordTs = settingsWindowCaptureFile;
			bjc.Gps = gpsReceiver;
			bjc.Ini = ini;
			bjc.LnbType = lnbs[settingsWindowLNB];
			bjc.ObjectStorage = objectStorage;
			bjc.SatellitePosition = satellites[settingsWindowSatellite];

			switch (multitunerMode)
			{
				case 1:
					bjc.StreamReader = streamReader;
					break;
				case 2:
					bjc.StreamReader = new CoopBlindscanStreamReaderProxy(streamReader, settingsWindowBLScanTunerSelection, settingsWindowSetFilterTunerSelection);
					break;
				case 4:
					bjc.StreamReader = new SatIpWithStreamReaderProxy(streamReader, objectStorage);
					break;
				default:
					throw new NotImplementedException(String.Format("Multituner Mode {0}", multitunerMode));
			}
			
			bjc.TunerMetadata = tuners[settingsWindowBLScanTunerSelection];
			bjc.Ui = new InheritedBlindscanUiJunction();
			//bjc.Ui.Tasks = _taskQueue;
			return bjc;
		}

		private bool okButtonClicked;
		public void Render()
		{
			if (ImGui.Begin("Blindscan", ref windowOpen, ImGuiWindowFlags.AlwaysAutoResize))
			{
				if (satellites.Count == 0)
				{
					ImGui.Text("You didn't configure the sattelite positions yet.");
					return;
				}
				ImGui.PushID("tunerA");
				if (ImGui.BeginCombo("Tuner for BLScanEx and RFScan", tuners[settingsWindowBLScanTunerSelection].Name))
				{
					for (int i = 0; i < tuners.Count; i++)
					{
						bool isSelected = settingsWindowBLScanTunerSelection == i;
						if (ImGui.Selectable(tuners[i].Name, isSelected))
						{
							settingsWindowBLScanTunerSelection = i;
						}
					}
					ImGui.EndCombo();
				}
				ImGui.PopID();

				if (tuners[settingsWindowBLScanTunerSelection].IsSatellite())
				{
					ImGui.RadioButton("Use the same Tuner for IQScan and SetFilter", ref multitunerMode, 1);
					ImGui.RadioButton("Use different Tuner for IQScan and SetFilter", ref multitunerMode, 2);
					
					if (multitunerMode == 2)
					{
						ImGui.Text("This assumes that both tuners are connected to the same antenna set-up, using e.g. a splitter.");

						ImGui.PushID("tunerB");

						if (ImGui.BeginCombo("Tuner for BLScanEx and RFScan", tuners[settingsWindowSetFilterTunerSelection].Name))
						{
							for (int i = 0; i < tuners.Count; i++)
							{
								bool isSelected = settingsWindowSetFilterTunerSelection == i;
								if (ImGui.Selectable(tuners[i].Name, isSelected))
								{
									settingsWindowSetFilterTunerSelection = i;
								}
							}
							ImGui.EndCombo();
						}
						ImGui.PopID();
					}

					ImGui.RadioButton("Use same Tuner for IQScan, but SAT>IP Server for SetFilter", ref multitunerMode, 4);
					
					ImGui.Text("DiSEqC");
					ImGui.SameLine();
					ImGui.RadioButton("A", ref settingsWindowDiseqc, 1);
					ImGui.SameLine();
					ImGui.RadioButton("B", ref settingsWindowDiseqc, 2);
					ImGui.SameLine();
					ImGui.RadioButton("C", ref settingsWindowDiseqc, 3);
					ImGui.SameLine();
					ImGui.RadioButton("D", ref settingsWindowDiseqc, 4);


					if (ImGui.BeginCombo("Target Satellite", satellites[settingsWindowSatellite].name))
					{
						for (int i = 0; i < satellites.Count; i++)
						{
							bool isSelected = settingsWindowSatellite == i;
							if (ImGui.Selectable(satellites[i].name, isSelected))
							{
								settingsWindowSatellite = i;
							}
						}
						ImGui.EndCombo();
					}

					if (ImGui.BeginCombo("LNB", lnbs[settingsWindowLNB].Name))
					{
						for (int i = 0; i < lnbs.Count; i++)
						{
							bool isSelected = settingsWindowLNB == i;
							if (ImGui.Selectable(lnbs[i].Name, isSelected))
							{
								settingsWindowLNB = i;
							}
						}
						ImGui.EndCombo();
					}

					if (ImGui.BeginCombo("Dish", dishes[settingsWindowDish].Name))
					{
						for (int i = 0; i < dishes.Count; i++)
						{
							bool isSelected = settingsWindowDish == i;
							if (ImGui.Selectable(dishes[i].Name, isSelected))
							{
								settingsWindowDish = i;
							}
						}
						ImGui.EndCombo();
					}

					if (lnbs[settingsWindowLNB].LofSw != 0)
					{
						ImGui.Checkbox("Scan Horizontal Low Band", ref settingsWindowScanHorizontalLow);
						ImGui.Checkbox("Scan Horizontal High Band", ref settingsWindowScanHorizontalHigh);
						ImGui.Checkbox("Scan Vertical Low Band", ref settingsWindowScanVerticalLow);
						ImGui.Checkbox("Scan Vertical High Band", ref settingsWindowScanVerticalHigh);
					}
					else
					{
						ImGui.Checkbox("Scan Left circular Band", ref settingsWindowScanHorizontalLow);
						ImGui.Checkbox("Scan Right circular Band", ref settingsWindowScanVerticalLow);
					}
				}

				ImGui.Checkbox("Capture Packets to files", ref settingsWindowCaptureFile);
				ImGui.Checkbox("Collect IQ Graphs", ref settingsWindowCollectIqGraphs);
				ImGui.Checkbox("Collect RF Spectrum", ref settingsWindowCollectRfSpectrum);

				if (ImGui.Button("OK"))
				{
					windowOpen = false;
					okButtonClicked = true;
					StoreSettings();
				}
			}
		}
	}
}
