using System;
using System.Collections.Generic;
using QC.VISA;

namespace QC.MCI;

public class MCI_QTI_SCPI : MultiplexControlInterface
{
	private enum EQUIPMENT_COMBO
	{
		UNKNOWN,
		CMW500_ONE_INSTR,
		CMW500_TWO_INSTR,
		CMW100,
		CMW500_CMWS_ONE_INSTR,
		CMW500_CMWS_TWO_INSTR,
		ANRITSU_MT8870A,
		LITEPOINT_IQXSTREAM
	}

	private string visaResource = "";

	private string cfg = "";

	private bool simulation_mode;

	private string[] channel_list_groups;

	private Visa32 visaHandle;

	private EQUIPMENT_COMBO m_TE;

	public override string BusConfiguration
	{
		get
		{
			return visaResource;
		}
		set
		{
			visaResource = value;
		}
	}

	public override string RoutingConfiguration
	{
		get
		{
			return cfg;
		}
		set
		{
			cfg = value.Replace(" ", string.Empty);
			cfg = cfg.Replace("\r", string.Empty);
			cfg = cfg.Replace("\n", string.Empty);
			cfg = cfg.Replace("\t", string.Empty);
			ChannelListGroups = cfg.Split(';');
		}
	}

	public override bool SimulationMode
	{
		get
		{
			return simulation_mode;
		}
		set
		{
			simulation_mode = value;
		}
	}

	private string[] ChannelListGroups
	{
		get
		{
			return channel_list_groups;
		}
		set
		{
			channel_list_groups = value;
		}
	}

	private Visa32 VisaHandle
	{
		get
		{
			if (visaHandle == null && !SimulationMode)
			{
				visaHandle = new Visa32();
				visaHandle.Resource = BusConfiguration;
			}
			return visaHandle;
		}
	}

	public override bool Configure()
	{
		SendCmd("*IDN?");
		string text = ReadResp();
		char[] separator = new char[1] { ',' };
		if (text.Contains("CMW"))
		{
			SendCmd("QCOM:ROUTe:TERMinals:CATalog:RF:LABel?");
			text = ReadResp();
			string[] array = text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
			if (array[1].Contains("RF1COM") || array[1].Contains("RF3COM"))
			{
				if (array[1].Contains("RF1COM") && array[1].Contains("RF3COM"))
				{
					m_TE = EQUIPMENT_COMBO.CMW500_ONE_INSTR;
				}
				else
				{
					m_TE = EQUIPMENT_COMBO.CMW500_TWO_INSTR;
				}
			}
			else if (array[1].Contains("RF1"))
			{
				m_TE = EQUIPMENT_COMBO.CMW100;
			}
			else if (array[1].Contains("1.1") || array[1].Contains("3.1"))
			{
				if (array[1].Contains("1.1") && array[1].Contains("3.1"))
				{
					m_TE = EQUIPMENT_COMBO.CMW500_CMWS_ONE_INSTR;
				}
				else
				{
					m_TE = EQUIPMENT_COMBO.CMW500_CMWS_TWO_INSTR;
				}
			}
		}
		else if (text.Contains("8870"))
		{
			m_TE = EQUIPMENT_COMBO.ANRITSU_MT8870A;
		}
		else if (text.Contains("LitePoint"))
		{
			m_TE = EQUIPMENT_COMBO.LITEPOINT_IQXSTREAM;
		}
		SendPreQTISCPI_NativeCommand();
		return true;
	}

	public override void EnablePath(int path, string Technlogy_Code)
	{
		int num = ChannelListGroups.Length;
		if (num < 1)
		{
			throw new Exception("Invalid RoutingConfig. The number of routes is : " + num);
		}
		if (path > num || path < 1)
		{
			throw new Exception("Invalid path number '" + path + "' requested. Valid path numbers are 1 - " + num);
		}
		if (ChannelListGroups[path - 1].Length == 0)
		{
			int num2 = path;
			throw new Exception("The routing configuration for DUT " + num2 + " is empty!");
		}
		char[] separator = new char[1] { ':' };
		string[] array = ChannelListGroups[path - 1].Split(separator, StringSplitOptions.RemoveEmptyEntries);
		if (array.Length != 2)
		{
			int num3 = path;
			throw new Exception("DUT: " + num3 + " Invalid routing configuration: " + ChannelListGroups[path - 1]);
		}
		string text = array[0].ToString();
		string text2 = array[1].ToString();
		SendPreQTISCPI_NativeCommand();
		Validate_Equipment_Port_Limitation(text, text2);
		string text3 = "";
		if (Technlogy_Code.Length != 0)
		{
			Technlogy_Code = Technlogy_Code.ToUpper();
			if (string.Compare(Technlogy_Code, "BT") == 0 || string.Compare(Technlogy_Code, "WLAN") == 0)
			{
				text3 = Technlogy_Code + ":";
			}
		}
		string globalVariable = MCI_EnvManager.GetInstance().GetGlobalVariable("MCI_EQUIPMENT_BYPASS_QTI_SCPI");
		if (globalVariable != "TRUE")
		{
			string cmd = "QCOM:" + text3 + "SENSe:ROUTe:PORT:CLOSE (@" + text + ")";
			SendCmd(cmd);
			string cmd2 = "QCOM:" + text3 + "SOURce:ROUTe:PORT:CLOSE (@" + text2 + ")";
			SendCmd(cmd2);
		}
		string text4 = path + ":" + text + ":" + text2;
		MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_QTI_SCPI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", text4);
		MCI_EnvManager.GetInstance().DebugMessageWrite("Debug", text4 + "\t QTI SCPI Driver");
		if (m_TE == EQUIPMENT_COMBO.CMW100 || m_TE == EQUIPMENT_COMBO.CMW500_ONE_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_TWO_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_CMWS_ONE_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_CMWS_TWO_INSTR)
		{
			Set_CMW_GlobalVariable(path, Technlogy_Code);
		}
		else if (m_TE == EQUIPMENT_COMBO.ANRITSU_MT8870A)
		{
			Set_8870_GlobalVariable(path, Technlogy_Code);
		}
		else if (m_TE == EQUIPMENT_COMBO.LITEPOINT_IQXSTREAM)
		{
			MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", text4);
			MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG_DUT" + path, text4);
		}
		else
		{
			MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", text4);
			MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG_DUT" + path, text4);
		}
		if (globalVariable != "TRUE")
		{
			CheckForErrors();
		}
	}

	private void SendPreQTISCPI_NativeCommand()
	{
		if (m_TE != EQUIPMENT_COMBO.ANRITSU_MT8870A && m_TE != EQUIPMENT_COMBO.CMW100 && m_TE != EQUIPMENT_COMBO.CMW500_ONE_INSTR && m_TE != EQUIPMENT_COMBO.CMW500_TWO_INSTR && m_TE != EQUIPMENT_COMBO.CMW500_CMWS_ONE_INSTR && m_TE != EQUIPMENT_COMBO.CMW500_CMWS_TWO_INSTR)
		{
			_ = m_TE;
			_ = 7;
		}
	}

	private void Validate_Equipment_Port_Limitation(string analyzerChannelList, string generatorChannelList)
	{
		char[] separator = new char[1] { ',' };
		string[] array = analyzerChannelList.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		generatorChannelList.Split(separator, StringSplitOptions.RemoveEmptyEntries);
		if (m_TE == EQUIPMENT_COMBO.ANRITSU_MT8870A)
		{
			if (array.Length > 1)
			{
				throw new Exception("MCI:MT8870A analyzer list size is greater than 1. MT8870A can only have one active analyzer port");
			}
		}
		else if ((m_TE == EQUIPMENT_COMBO.CMW100 || m_TE == EQUIPMENT_COMBO.CMW500_ONE_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_TWO_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_CMWS_ONE_INSTR || m_TE == EQUIPMENT_COMBO.CMW500_CMWS_TWO_INSTR) && array.Length > 1)
		{
			throw new Exception("MCI:R&S family analyzer list size is greater than 1. R&S family can only have one active analyzer port");
		}
	}

	private void Set_CMW_GlobalVariable(int path, string tech)
	{
		char[] separator = new char[1] { ':' };
		string[] array = ChannelListGroups[path - 1].Split(separator, StringSplitOptions.RemoveEmptyEntries);
		char[] separator2 = new char[1] { ',' };
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Clear();
		if (m_TE == EQUIPMENT_COMBO.CMW500_ONE_INSTR)
		{
			dictionary.Add("1", "RF1C");
			dictionary.Add("2", "RF1O");
			dictionary.Add("3", "RF2C");
			dictionary.Add("4", "RF3C");
			dictionary.Add("5", "RF3O");
			dictionary.Add("6", "RF4C");
		}
		else if (m_TE == EQUIPMENT_COMBO.CMW500_TWO_INSTR)
		{
			dictionary.Add("1", "RFAC");
			dictionary.Add("2", "RFAO");
			dictionary.Add("3", "RFBC");
			dictionary.Add("4", "RFAC");
			dictionary.Add("5", "RFAO");
			dictionary.Add("6", "RFBC");
		}
		else if (m_TE == EQUIPMENT_COMBO.CMW100)
		{
			dictionary.Add("1", "R11");
			dictionary.Add("2", "R12");
			dictionary.Add("3", "R13");
			dictionary.Add("4", "R14");
			dictionary.Add("5", "R15");
			dictionary.Add("6", "R16");
			dictionary.Add("7", "R17");
			dictionary.Add("8", "R18");
		}
		else if (m_TE == EQUIPMENT_COMBO.CMW500_CMWS_ONE_INSTR)
		{
			dictionary.Add("1", "R11");
			dictionary.Add("2", "R12");
			dictionary.Add("3", "R13");
			dictionary.Add("4", "R14");
			dictionary.Add("5", "R15");
			dictionary.Add("6", "R16");
			dictionary.Add("7", "R17");
			dictionary.Add("8", "R18");
			dictionary.Add("9", "R31");
			dictionary.Add("10", "R32");
			dictionary.Add("11", "R33");
			dictionary.Add("12", "R34");
			dictionary.Add("13", "R35");
			dictionary.Add("14", "R36");
			dictionary.Add("15", "R37");
			dictionary.Add("16", "R38");
			dictionary.Add("17", "R21");
			dictionary.Add("18", "R22");
			dictionary.Add("19", "R23");
			dictionary.Add("20", "R24");
			dictionary.Add("21", "R25");
			dictionary.Add("22", "R26");
			dictionary.Add("23", "R27");
			dictionary.Add("24", "R28");
		}
		else if (m_TE == EQUIPMENT_COMBO.CMW500_CMWS_TWO_INSTR)
		{
			dictionary.Add("1", "RA1");
			dictionary.Add("2", "RA2");
			dictionary.Add("3", "RA3");
			dictionary.Add("4", "RA4");
			dictionary.Add("5", "RA5");
			dictionary.Add("6", "RA6");
			dictionary.Add("7", "RA7");
			dictionary.Add("8", "RA8");
			dictionary.Add("9", "RA1");
			dictionary.Add("10", "RA2");
			dictionary.Add("11", "RA3");
			dictionary.Add("12", "RA4");
			dictionary.Add("13", "RA5");
			dictionary.Add("14", "RA6");
			dictionary.Add("15", "RA7");
			dictionary.Add("16", "RA8");
			dictionary.Add("17", "RB1");
			dictionary.Add("18", "RB2");
			dictionary.Add("19", "RB3");
			dictionary.Add("20", "RB4");
			dictionary.Add("21", "RB1");
			dictionary.Add("22", "RB2");
			dictionary.Add("23", "RB3");
			dictionary.Add("24", "RB4");
		}
		string[] array2 = array[1].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
		string key = array[0];
		string text = "";
		for (int i = 0; i < array2.Length; i++)
		{
			text += dictionary[array2[i]];
			if (i != array2.Length - 1)
			{
				text += ",";
			}
		}
		key = dictionary[key];
		MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", path + ":" + key + ":" + text);
		for (int j = 0; j < ChannelListGroups.Length; j++)
		{
			if (j != path - 1)
			{
				continue;
			}
			string[] array3 = ChannelListGroups[j].Split(separator, StringSplitOptions.RemoveEmptyEntries);
			string key2 = array3[0];
			key2 = dictionary[key2];
			string[] array4 = array3[1].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
			string text2 = "";
			for (int k = 0; k < array4.Length; k++)
			{
				text2 += dictionary[array4[k]];
				if (k != array4.Length - 1)
				{
					text2 += ",";
				}
			}
			MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG_DUT" + path, path + ":" + key2 + ":" + text2);
		}
	}

	private void Set_8870_GlobalVariable(int path, string tech)
	{
		char[] separator = new char[1] { ':' };
		string[] array = ChannelListGroups[path - 1].Split(separator, StringSplitOptions.RemoveEmptyEntries);
		char[] separator2 = new char[1] { ',' };
		string[] array2 = array[1].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
		string text = "";
		for (int i = 0; i < array2.Length; i++)
		{
			text = text + "PORT" + array2[i];
			if (i != array2.Length - 1)
			{
				text += ",";
			}
		}
		string text2 = array[0];
		text2 = "PORT" + text2;
		string value = path + ":" + text2 + ":" + text;
		MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", value);
		MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG_DUT" + path, value);
	}

	private void CheckForErrors()
	{
		string text = "";
		string text2 = "";
		do
		{
			SendCmd("QCOM:SYST:ERR?");
			text = (simulation_mode ? "No error" : ReadResp());
			if (text.Length > 0 && !text.Contains("No error"))
			{
				text2 += text;
				text2 += "\n";
			}
		}
		while (text.Length > 0 && !text.Contains("No error"));
		if (text2.Length != 0)
		{
			WriteErrorMessage("QTI SCPI Instrument reported the following errors: \n" + text2);
		}
	}

	private void SendCmd(string cmd)
	{
		try
		{
			if (!SimulationMode)
			{
				VisaHandle.WriteString(cmd);
			}
			WriteTPLMessage("IEEE488_WR", BusConfiguration + ";" + cmd);
		}
		catch (Exception ex)
		{
			if (!VisaHandle.IsOpen())
			{
				throw new Exception("Cannot communicate with QTI SCPI Instrument. " + ex.Message);
			}
			throw new Exception(ex.Message);
		}
	}

	private string ReadResp()
	{
		string text = "";
		try
		{
			if (!SimulationMode)
			{
				text = VisaHandle.ReadString();
			}
			WriteTPLMessage("IEEE488_RD", BusConfiguration + ";" + text);
			return text;
		}
		catch (Exception ex)
		{
			if (!VisaHandle.IsOpen())
			{
				throw new Exception("Cannot communicate with QTI SCPI Instrument");
			}
			throw new Exception(ex.Message);
		}
	}
}
