using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace QSC_Params_File_Helper;

public class QSC_Param_Query
{
	public enum FBRxInfoParameterType
	{
		PA_Gain_State,
		RGI_List,
		Expected_Power,
		Bias_List,
		ICQ_List
	}

	public enum RxCalParameterType
	{
		LNA_State,
		LNA_Power,
		Gain_Limit_Max,
		Gain_Limit_Min
	}

	public enum TxLineaizerCalParameterType
	{
		PA_State_List,
		RGI_List,
		Bias_List,
		ICQ_List,
		Max_Lin_Power_List,
		Min_Lin_Power_List
	}

	private const int SWEEP_TYPE_CONNECTED = 1000;

	private const int SWEEP_TYPE_UNCONNECTED = 1001;

	private const int SWEEP_TYPE_CONNECTED_V2 = 2000;

	private const int SWEEP_TYPE_UNCONNECTED_V2 = 2001;

	private bool isLoaded;

	private QSC_V1 QSC;

	public bool ViewOriginal(string QSC_Param_FilePath, ref string exceptionMessage, ref string sQSC_File_Text)
	{
		try
		{
			sQSC_File_Text = File.ReadAllText(QSC_Param_FilePath);
			return true;
		}
		catch (Exception ex)
		{
			exceptionMessage = ex.ToString();
			return false;
		}
	}

	public bool LoadQSCParamsFile(string QSC_Param_File_Path, ref string exceptionMessage)
	{
		try
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(QSC_V1));
			FileStream input = new FileStream(QSC_Param_File_Path, FileMode.Open, FileAccess.Read);
			XmlReader xmlReader = XmlReader.Create(input);
			QSC = (QSC_V1)xmlSerializer.Deserialize(xmlReader);
			exceptionMessage = "";
			return isLoaded = true;
		}
		catch (Exception ex)
		{
			exceptionMessage = ex.ToString();
			return isLoaded = false;
		}
	}

	public bool Get_Channel_List_From_UnConnected_Nodes(string tech, string band, string sub_band, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1001 || item.sweep_type == 2001) && item.tech == tech && item.band.ToString() == band && item.sub_band.ToString() == sub_band && Common.IsTrue(item.CalType.Enable_Full_Bias_Linearizers));
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].Common != null)
			{
				text += list[i].Common.Channel_List;
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_DLChan_List_From_Connected_Nodes(string tech, string band, string sub_band, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && Common.IsTrue(item.CalType.Enable_PRx_Gain));
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type = list[i].BandInfo.Items[j].GetType();
				if (type.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
					if (qSC_V1NodeBandInfoBand.band == Convert.ToByte(band) && qSC_V1NodeBandInfoBand.sub_band == Convert.ToByte(sub_band))
					{
						text += qSC_V1NodeBandInfoBand.Common.Channel_List;
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Channel_List(string tech, string band, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_FBRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type = list[i].BandInfo.Items[j].GetType();
				if (type.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
					if (qSC_V1NodeBandInfoBand.band == Convert.ToByte(band))
					{
						text += qSC_V1NodeBandInfoBand.Common.Channel_List;
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_FBRx_Cal_Info(string tech, string band, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_FBRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type = list[i].BandInfo.Items[j].GetType();
				if (!type.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					continue;
				}
				QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
				if (qSC_V1NodeBandInfoBand.band == Convert.ToByte(band))
				{
					if (qSC_V1NodeBandInfoBand.FBRx_Gain != null)
					{
						text = text + "PA Gain State: " + qSC_V1NodeBandInfoBand.FBRx_Gain.PA_State_List;
						text += "\r\n";
						text = text + "RGI List: " + qSC_V1NodeBandInfoBand.FBRx_Gain.RGI_List;
						text += "\r\n";
						text = text + "Expected Power: " + qSC_V1NodeBandInfoBand.FBRx_Gain.Tx_Power_dBm_List;
						text += "\r\n";
						text = text + "Bias List: " + qSC_V1NodeBandInfoBand.FBRx_Gain.Bias_List;
						text += "\r\n";
						text = text + "ICQ List: " + qSC_V1NodeBandInfoBand.FBRx_Gain.Icq_List;
						text += "\r\n";
					}
					else
					{
						text = text + "PA Gain State: " + list[i].Default.FBRx_Gain.PA_State_List;
						text += "\r\n";
						text = text + "RGI List: " + list[i].Default.FBRx_Gain.RGI_List;
						text += "\r\n";
						text = text + "Expected Power: " + list[i].Default.FBRx_Gain.Tx_Power_dBm_List;
						text += "\r\n";
						text = text + "Bias List: " + list[i].Default.FBRx_Gain.Bias_List;
						text += "\r\n";
						text = text + "ICQ List: " + list[i].Default.FBRx_Gain.Icq_List;
						text += "\r\n";
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Rx_Gain_Info(string tech, string band, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_PRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type = list[i].BandInfo.Items[j].GetType();
				if (!type.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					continue;
				}
				QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
				if (qSC_V1NodeBandInfoBand.band == Convert.ToByte(band))
				{
					if (qSC_V1NodeBandInfoBand.FBRx_Gain != null && qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States != null)
					{
						text = text + "LNA Gain State: " + qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States;
						text += "\r\n";
						text = text + "DL Power: " + qSC_V1NodeBandInfoBand.Rx_Gain.DL_Powers;
						text += "\r\n";
						text = text + "Gain Limit(Max): " + qSC_V1NodeBandInfoBand.Rx_Gain.Gain_Limit_Max;
						text += "\r\n";
						text = text + "Gain Limit(Min): " + qSC_V1NodeBandInfoBand.Rx_Gain.Gain_Limit_Min;
						text += "\r\n";
						text = text + "Power Mode: " + qSC_V1NodeBandInfoBand.Rx_Gain.Power_Mode;
						text += "\r\n";
					}
					else
					{
						text = text + "LNA Gain State: " + list[i].Default.Rx_Gain.Gain_States;
						text += "\r\n";
						text = text + "DL Power: " + list[i].Default.Rx_Gain.DL_Powers;
						text += "\r\n";
						text = text + "Gain Limit(Max): " + list[i].Default.Rx_Gain.Gain_Limit_Max;
						text += "\r\n";
						text = text + "Gain Limit(Min): " + list[i].Default.Rx_Gain.Gain_Limit_Min;
						text += "\r\n";
						text = text + "Power Mode: " + list[i].Default.Rx_Gain.Power_Mode;
						text += "\r\n";
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Bands_From_Unconnected_Nodes(string tech, ref List<Tuple<string, string>> bands, ref string bandStr)
	{
		try
		{
			if (!isLoaded)
			{
				return false;
			}
			IEnumerable<QSC_V1Node> enumerable = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1001 || item.sweep_type == 2001) && item.tech == tech && Common.IsTrue(item.CalType.Enable_Full_Bias_Linearizers));
			string text = "";
			List<QSC_V1Node> list = new List<QSC_V1Node>();
			if (enumerable != null)
			{
				list = enumerable.ToList();
			}
			bands = new List<Tuple<string, string>>();
			for (int i = 0; i < list.Count; i++)
			{
				Tuple<string, string> item2 = Tuple.Create(list[i].band.ToString(), list[i].sub_band.ToString());
				bands.Add(item2);
				text += list[i].band;
				text += "(Subband-";
				text += list[i].sub_band;
				text += "),";
			}
			bandStr = text;
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
			return false;
		}
	}

	public bool Get_Bands_From_Connected_Nodes(string tech, ref List<Tuple<string, string>> bands, ref string bandStr)
	{
		try
		{
			if (!isLoaded)
			{
				return false;
			}
			IEnumerable<QSC_V1Node> enumerable = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && (item.CalType.Enable_PRx_GainSpecified || item.CalType.Enable_Rx_GainSpecified));
			string text = "";
			List<QSC_V1Node> list = new List<QSC_V1Node>();
			if (enumerable != null)
			{
				list = enumerable.ToList();
			}
			bands = new List<Tuple<string, string>>();
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].BandInfo.Items == null)
				{
					continue;
				}
				for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
				{
					Type type = list[i].BandInfo.Items[j].GetType();
					if (type.Equals(typeof(QSC_V1NodeBandInfoBand)))
					{
						QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
						Tuple<string, string> item2 = Tuple.Create(qSC_V1NodeBandInfoBand.band.ToString(), qSC_V1NodeBandInfoBand.sub_band.ToString());
						bands.Add(item2);
						text += qSC_V1NodeBandInfoBand.band;
						text += "(Subband-";
						text += qSC_V1NodeBandInfoBand.sub_band;
						text += "),";
					}
				}
			}
			bandStr = text;
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
			return false;
		}
	}

	public bool Get_Bands(string tech, ref List<string> bands, ref string bandStr)
	{
		try
		{
			if (!isLoaded)
			{
				return false;
			}
			IEnumerable<QSC_V1Node> enumerable = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_PRx_GainSpecified);
			string text = "";
			List<QSC_V1Node> list = new List<QSC_V1Node>();
			if (enumerable != null)
			{
				list = enumerable.ToList();
			}
			bands = new List<string>();
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].BandInfo.Items == null)
				{
					continue;
				}
				for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
				{
					Type type = list[i].BandInfo.Items[j].GetType();
					if (type.Equals(typeof(QSC_V1NodeBandInfoBand)))
					{
						QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
						bands.Add(qSC_V1NodeBandInfoBand.band.ToString());
						text += qSC_V1NodeBandInfoBand.band;
						text += ",";
					}
				}
			}
			bandStr = text;
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
			return false;
		}
	}

	public bool Get_FBRx_Cal_Info_By_Type(string tech, string band, FBRxInfoParameterType type, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_FBRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type2 = list[i].BandInfo.Items[j].GetType();
				if (!type2.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					continue;
				}
				QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
				if (qSC_V1NodeBandInfoBand.band != Convert.ToByte(band))
				{
					continue;
				}
				if (qSC_V1NodeBandInfoBand.FBRx_Gain != null)
				{
					switch (type)
					{
					case FBRxInfoParameterType.PA_Gain_State:
						text += qSC_V1NodeBandInfoBand.FBRx_Gain.PA_State_List;
						break;
					case FBRxInfoParameterType.RGI_List:
						text += qSC_V1NodeBandInfoBand.FBRx_Gain.RGI_List;
						break;
					case FBRxInfoParameterType.Expected_Power:
						text += qSC_V1NodeBandInfoBand.FBRx_Gain.Tx_Power_dBm_List;
						break;
					case FBRxInfoParameterType.Bias_List:
						text += qSC_V1NodeBandInfoBand.FBRx_Gain.Bias_List;
						break;
					case FBRxInfoParameterType.ICQ_List:
						text += qSC_V1NodeBandInfoBand.FBRx_Gain.Icq_List;
						break;
					}
				}
				else
				{
					switch (type)
					{
					case FBRxInfoParameterType.PA_Gain_State:
						text += list[i].Default.FBRx_Gain.PA_State_List;
						break;
					case FBRxInfoParameterType.RGI_List:
						text += list[i].Default.FBRx_Gain.RGI_List;
						break;
					case FBRxInfoParameterType.Expected_Power:
						text += list[i].Default.FBRx_Gain.Tx_Power_dBm_List;
						break;
					case FBRxInfoParameterType.Bias_List:
						text += list[i].Default.FBRx_Gain.Bias_List;
						break;
					case FBRxInfoParameterType.ICQ_List:
						text += list[i].Default.FBRx_Gain.Icq_List;
						break;
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Rx_Gain_Info_By_Type_V2(string tech, string band, string sub_band, RxCalParameterType type, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_PRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type2 = list[i].BandInfo.Items[j].GetType();
				if (!type2.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					continue;
				}
				QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
				if (qSC_V1NodeBandInfoBand.band != Convert.ToByte(band) || qSC_V1NodeBandInfoBand.sub_band != Convert.ToByte(sub_band))
				{
					continue;
				}
				if (qSC_V1NodeBandInfoBand.FBRx_Gain != null && qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States != null)
				{
					switch (type)
					{
					case RxCalParameterType.LNA_State:
						text += qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States;
						break;
					case RxCalParameterType.LNA_Power:
						text += qSC_V1NodeBandInfoBand.Rx_Gain.DL_Powers;
						break;
					case RxCalParameterType.Gain_Limit_Max:
						text += qSC_V1NodeBandInfoBand.Rx_Gain.Gain_Limit_Max;
						break;
					case RxCalParameterType.Gain_Limit_Min:
						text += qSC_V1NodeBandInfoBand.Rx_Gain.Gain_Limit_Min;
						break;
					}
				}
				else
				{
					switch (type)
					{
					case RxCalParameterType.LNA_State:
						text += list[i].Default.Rx_Gain.Gain_States;
						break;
					case RxCalParameterType.LNA_Power:
						text += list[i].Default.Rx_Gain.DL_Powers;
						break;
					case RxCalParameterType.Gain_Limit_Max:
						text += list[i].Default.Rx_Gain.Gain_Limit_Max;
						break;
					case RxCalParameterType.Gain_Limit_Min:
						text += list[i].Default.Rx_Gain.Gain_Limit_Min;
						break;
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Rx_Gain_Info_By_Type(string tech, string band, RxCalParameterType type, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1000 || item.sweep_type == 2000) && item.tech == tech && item.CalType.Enable_PRx_GainSpecified);
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = 0; j < list[i].BandInfo.Items.Length; j++)
			{
				Type type2 = list[i].BandInfo.Items[j].GetType();
				if (!type2.Equals(typeof(QSC_V1NodeBandInfoBand)))
				{
					continue;
				}
				QSC_V1NodeBandInfoBand qSC_V1NodeBandInfoBand = (QSC_V1NodeBandInfoBand)list[i].BandInfo.Items[j];
				if (qSC_V1NodeBandInfoBand.band != Convert.ToByte(band))
				{
					continue;
				}
				if (qSC_V1NodeBandInfoBand.FBRx_Gain != null && qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States != null)
				{
					if (type == RxCalParameterType.LNA_State)
					{
						text += qSC_V1NodeBandInfoBand.Rx_Gain.Gain_States;
					}
					if (type == RxCalParameterType.LNA_Power)
					{
						text += qSC_V1NodeBandInfoBand.Rx_Gain.DL_Powers;
					}
				}
				else
				{
					if (type == RxCalParameterType.LNA_State)
					{
						text += list[i].Default.Rx_Gain.Gain_States;
					}
					if (type == RxCalParameterType.LNA_Power)
					{
						text += list[i].Default.Rx_Gain.DL_Powers;
					}
				}
			}
		}
		calInfo = text;
		return true;
	}

	public bool Get_Tx_Cal_Info_By_Type(string tech, string band, string sub_band, TxLineaizerCalParameterType type, ref string calInfo)
	{
		if (!isLoaded)
		{
			return false;
		}
		IEnumerable<QSC_V1Node> source = QSC.Node.Where((QSC_V1Node item) => (item.sweep_type == 1001 || item.sweep_type == 2001) && item.tech == tech && item.band.ToString() == band && item.sub_band.ToString() == sub_band && Common.IsTrue(item.CalType.Enable_Full_Bias_Linearizers));
		string text = "";
		List<QSC_V1Node> list = source.ToList();
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].Tx_Linearizer != null)
			{
				switch (type)
				{
				case TxLineaizerCalParameterType.PA_State_List:
					text += list[i].Tx_Linearizer.PA_State_List;
					break;
				case TxLineaizerCalParameterType.RGI_List:
					text += list[i].Tx_Linearizer.RGI_List;
					break;
				case TxLineaizerCalParameterType.Bias_List:
					text += list[i].Tx_Linearizer.Bias_List;
					break;
				case TxLineaizerCalParameterType.ICQ_List:
					text += list[i].Tx_Linearizer.Icq_List;
					break;
				case TxLineaizerCalParameterType.Max_Lin_Power_List:
					text += list[i].Tx_Linearizer.Max_Lin_Power_List;
					break;
				case TxLineaizerCalParameterType.Min_Lin_Power_List:
					text += list[i].Tx_Linearizer.Min_Lin_power_List;
					break;
				}
			}
			else
			{
				switch (type)
				{
				case TxLineaizerCalParameterType.PA_State_List:
					text += list[i].Default.Tx_Linearizer.PA_State_List;
					break;
				case TxLineaizerCalParameterType.RGI_List:
					text += list[i].Default.Tx_Linearizer.RGI_List;
					break;
				case TxLineaizerCalParameterType.Bias_List:
					text += list[i].Default.Tx_Linearizer.Bias_List;
					break;
				case TxLineaizerCalParameterType.ICQ_List:
					text += list[i].Default.Tx_Linearizer.Icq_List;
					break;
				case TxLineaizerCalParameterType.Max_Lin_Power_List:
					text += list[i].Default.Tx_Linearizer.Max_Lin_Power_List;
					break;
				case TxLineaizerCalParameterType.Min_Lin_Power_List:
					text += list[i].Default.Tx_Linearizer.Min_Lin_Power_List;
					break;
				}
			}
		}
		calInfo = text;
		return true;
	}
}
