using System;
using System.Text.RegularExpressions;

namespace QC.MCI;

public class MCI_Anritsu_MT8870A : MultiplexControlInterface
{
	private enum MT8870_ROUTING_INFO
	{
		MODULE,
		INPUT_PORT,
		OUTPUT_PORT
	}

	private enum CONNECTION_TYPE
	{
		TCPIP = 0,
		GPIB = 1,
		INVALID = -1
	}

	private enum EQUIP_BUS_CONFIG
	{
		COMMUNICATION_TYPE,
		ADDRESS
	}

	private const string qseqDLLName = "QSEQ_CORE_DLL.dll";

	private string visaResource = "";

	private string cfg = "";

	private bool simulation_mode;

	private string[] channel_list_groups;

	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;
		}
	}

	public override bool Configure()
	{
		return true;
	}

	public override void EnablePath(int path, string tech)
	{
		int num = ChannelListGroups.Length;
		if (num < 1)
		{
			throw new Exception("ChannelListGroups.Length is an invalid number: " + num);
		}
		if (path > num || path < 1)
		{
			throw new Exception("Invalid path number '" + path + "' requested. Valid path numbers are 1 - " + num);
		}
		string[] array = ChannelListGroups[path - 1].Split(':');
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Split(',');
			if (array2.Length == Enum.GetValues(typeof(MT8870_ROUTING_INFO)).Length)
			{
				BusConfiguration = getBusConfiguration(array2[Convert.ToInt16(MT8870_ROUTING_INFO.MODULE)]);
				MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_BUS_CONFIG", BusConfiguration);
				string portInfo = getPortInfo(array2[Convert.ToInt16(MT8870_ROUTING_INFO.INPUT_PORT)], array2[Convert.ToInt16(MT8870_ROUTING_INFO.OUTPUT_PORT)]);
				MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG", portInfo);
				MCI_EnvManager.GetInstance().SetGlobalVariable("MCI_EQUIPMENT_DEVICE_RF_PORT_CONFIG_DUT" + path, portInfo);
				continue;
			}
			throw new Exception("Bad command: " + ChannelListGroups[path - 1]);
		}
	}

	private string getBusConfiguration(string module)
	{
		string busConfiguration = BusConfiguration;
		string[] array = Regex.Split(busConfiguration, "::");
		if (array.Length < 2)
		{
			throw new Exception("Bad command: " + busConfiguration);
		}
		string text = array[Convert.ToInt16(EQUIP_BUS_CONFIG.ADDRESS)];
		if (getCommType(array[Convert.ToInt16(EQUIP_BUS_CONFIG.COMMUNICATION_TYPE)]) == CONNECTION_TYPE.GPIB)
		{
			text = module;
		}
		else
		{
			if (getCommType(array[Convert.ToInt16(EQUIP_BUS_CONFIG.COMMUNICATION_TYPE)]) != 0)
			{
				throw new Exception("Bad command: " + text);
			}
			text = changeTCPIPInfo(array[Convert.ToInt16(EQUIP_BUS_CONFIG.ADDRESS)], module);
		}
		string text2 = "CONNECT=" + array[Convert.ToInt16(EQUIP_BUS_CONFIG.COMMUNICATION_TYPE)] + "::" + text;
		for (int i = 2; i < array.Length; i++)
		{
			text2 = text2 + "::" + array[i];
		}
		return text2;
	}

	private CONNECTION_TYPE getCommType(string commString)
	{
		if (Regex.Match(commString, "GPIB", RegexOptions.IgnoreCase).Success)
		{
			return CONNECTION_TYPE.GPIB;
		}
		if (Regex.Match(commString, "TCPIP", RegexOptions.IgnoreCase).Success)
		{
			return CONNECTION_TYPE.TCPIP;
		}
		return CONNECTION_TYPE.INVALID;
	}

	private string changeTCPIPInfo(string TCPIPInfo, string module)
	{
		string text = "";
		string[] array = TCPIPInfo.Split('.');
		if (array.Length != 4)
		{
			throw new Exception("Bad command: " + TCPIPInfo);
		}
		for (int i = 0; i < 3; i++)
		{
			text = text + array[i] + ".";
		}
		return text + module;
	}

	private string getPortInfo(string inputPort, string outputPort)
	{
		string text = "";
		string text2 = text;
		return text2 + "RF_PORT_IDS=PORT" + inputPort + ",PORT" + outputPort;
	}
}
