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

namespace QC.MCI;

public class MCI_Anritsu_MN8116A : MultiplexControlInterface
{
	private enum MN8116A_BUS_CONFIG
	{
		MT8870A_VISARESOURCE,
		MPS_IP,
		MPS_TARGET_ID
	}

	private enum MN8116A_ROUTING
	{
		MPS_PORT_IN,
		MPS_PORT_OUT,
		MPS_ULPORT,
		MPS_DLPORT,
		MPS_TRIGGER
	}

	private string busConfiguration = "";

	private string cfg = "";

	private bool simulation_mode;

	private string[] config_list_groups;

	private string[] addr_list_groups;

	private Visa32 visaHandle;

	private string lang_setting = "";

	private Dictionary<string, List<string>> cmdDic = new Dictionary<string, List<string>>();

	public override string BusConfiguration
	{
		get
		{
			return busConfiguration;
		}
		set
		{
			busConfiguration = value;
			AddrListGroups = busConfiguration.Split(';');
		}
	}

	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);
			ConfigListGroups = cfg.Split(';');
		}
	}

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

	private string[] ConfigListGroups
	{
		get
		{
			return config_list_groups;
		}
		set
		{
			config_list_groups = value;
		}
	}

	private string[] AddrListGroups
	{
		get
		{
			return addr_list_groups;
		}
		set
		{
			addr_list_groups = value;
		}
	}

	private Visa32 VisaHandle
	{
		get
		{
			if (visaHandle == null && !SimulationMode)
			{
				visaHandle = new Visa32();
				visaHandle.Resource = AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MT8870A_VISARESOURCE)];
			}
			return visaHandle;
		}
	}

	private string LangSetting
	{
		get
		{
			return lang_setting;
		}
		set
		{
			lang_setting = value;
		}
	}

	public override bool Configure()
	{
		return true;
	}

	public MCI_Anritsu_MN8116A()
	{
		cmdDic.Clear();
		cmdDic.Add("MPS", new List<string> { "MPS", "SYST:MPSW" });
		cmdDic.Add("MPS_TARGET_IP", new List<string> { "MPS_TARGET_IP", "SYST:MPSW:COMM:NET:TARG:IPVF:STAT:IPAD" });
		cmdDic.Add("MPS_TARGET", new List<string> { "MPS_TARGET", "SYST:MPSW:TARG" });
		cmdDic.Add("MPS_PORT", new List<string> { "MPS_PORT", "ROUT:MPSW:PORT:CONN:DIR" });
		cmdDic.Add("MPS_FUNCTION", new List<string> { "MPS_FUNCTION", "SYST:MPSW:FUNC" });
		cmdDic.Add("MPS_DLPORT", new List<string> { "MPS_DLPORT", "ROUT:MPSW:PORT:CONN:DIR:OUTP" });
		cmdDic.Add("MPS_ULPORT", new List<string> { "MPS_ULPORT", "ROUT:MPSW:PORT:CONN:DIR:INP" });
		cmdDic.Add("MPS_DLROUTE", new List<string> { "MPS_DLROUTE", "ROUT:MPSW:PATH:OUTP" });
		cmdDic.Add("MPS_ULROUTE", new List<string> { "MPS_ULROUTE", "ROUT:MPSW:PATH:INP" });
		cmdDic.Add("MPS_TRIGSEL", new List<string> { "MPS_TRIGSEL", "ROUT:MPSW:TRIG:SEL" });
		cmdDic.Add("MPS_MODE", new List<string> { "MPS_MODE", "SYST:MPSW:MODE" });
		cmdDic.Add("SYSERR?", new List<string> { "SYSERR?", "SYST:ERR?" });
		cmdDic.Add("SYSERRALL?", new List<string> { "SYSERRALL?", "SYST:ERR:ALL?" });
		cmdDic.Add("MPS?", new List<string> { "MPS?", "SYST:MPSW?" });
		cmdDic.Add("MPS_TARGET_IP?", new List<string> { "MPS_TARGET_IP?", "SYST:MPSW:COMM:NET:TARG:IPVF:STAT:IPAD?" });
		cmdDic.Add("MPS_PORT?", new List<string> { "MPS_PORT?", "ROUT:MPSW:PORT:CONN:DIR?" });
	}

	private string GetCmd(string key)
	{
		int index = ((!LangSetting.Contains("NAT")) ? 1 : 0);
		return cmdDic[key][index];
	}

	public override void EnablePath(int path, string TechInfo)
	{
		int num = ConfigListGroups.Length;
		if (num < 1)
		{
			throw new Exception("ConfigListGroups.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);
		}
		if (AddrListGroups.Length != Enum.GetValues(typeof(MN8116A_BUS_CONFIG)).Length)
		{
			throw new Exception("Bad BusConfiguration: " + BusConfiguration);
		}
		string[] array = ConfigListGroups[path - 1].Split(',');
		if (array.Length != Enum.GetValues(typeof(MN8116A_ROUTING)).Length)
		{
			throw new Exception("Bad RoutingConfiguration: " + ConfigListGroups[path - 1]);
		}
		try
		{
			SendCmd("SYST:LANG?");
			LangSetting = ReadResp();
			SendCmd(GetCmd("SYSERRALL?"));
			ReadResp();
			bool flag = true;
			bool flag2 = true;
			SendCmd(GetCmd("MPS?") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)]);
			string text = ReadResp();
			CheckForError();
			if (text.Contains("OFF"))
			{
				flag = false;
				flag2 = true;
			}
			else
			{
				SendCmd(GetCmd("MPS_TARGET_IP?") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)]);
				string resIp = ReadResp();
				CheckForError();
				if (CheckMpsIpRes(resIp, GetMpsBankIPAdder()))
				{
					SendCmd(GetCmd("MPS_PORT?") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)]);
					string resPort = ReadResp();
					CheckForError();
					if (CheckMpsPortRes(resPort, array[Convert.ToInt16(MN8116A_ROUTING.MPS_PORT_OUT)], array[Convert.ToInt16(MN8116A_ROUTING.MPS_PORT_IN)]))
					{
						flag = false;
						flag2 = false;
					}
				}
			}
			if (flag)
			{
				SendCmd(GetCmd("MPS") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)] + ",OFF");
				CheckForError();
			}
			if (flag2)
			{
				SendCmd(GetCmd("MPS_TARGET_IP") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)] + "," + GetMpsBankIPAdder());
				CheckForError();
				SendCmd(GetCmd("MPS_PORT") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)] + ",PORT" + array[Convert.ToInt16(MN8116A_ROUTING.MPS_PORT_OUT)] + ",PORT" + array[Convert.ToInt16(MN8116A_ROUTING.MPS_PORT_IN)]);
				CheckForError();
				SendCmd(GetCmd("MPS") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)] + ",ON");
				CheckForError();
			}
			SendCmd(GetCmd("MPS_MODE") + " QMULTI");
			CheckForError();
			SendCmd(GetCmd("MPS_TARGET") + " " + AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_TARGET_ID)]);
			CheckForError();
			SendCmd(GetCmd("MPS_FUNCTION") + " ON");
			CheckForError();
			SendCmd(GetCmd("MPS_TRIGSEL") + " TRG" + array[Convert.ToInt16(MN8116A_ROUTING.MPS_TRIGGER)]);
			CheckForError();
			SendCmd(GetCmd("MPS_DLPORT") + " " + array[Convert.ToInt16(MN8116A_ROUTING.MPS_DLPORT)]);
			CheckForError();
			SendCmd(GetCmd("MPS_ULPORT") + " " + array[Convert.ToInt16(MN8116A_ROUTING.MPS_ULPORT)]);
			CheckForError();
			SendCmd(GetCmd("MPS_DLROUTE") + " AUTO");
			CheckForError();
			SendCmd(GetCmd("MPS_ULROUTE") + " AUTO");
			CheckForError();
		}
		finally
		{
			Close();
		}
	}

	private string ConvertMpsIPAdderParam(string ip)
	{
		return ip.Replace(".", ",");
	}

	private string GetMpsBankIPAdder()
	{
		string text = "";
		if (!AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_IP)].Equals(string.Empty))
		{
			return ConvertMpsIPAdderParam(AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MPS_IP)]);
		}
		throw new Exception("Bad BusConfiguration: " + BusConfiguration);
	}

	private void CheckForError()
	{
		SendCmd(GetCmd("SYSERRALL?"));
		string text = ReadResp();
		if (!text.Contains("No error"))
		{
			throw new Exception("MN8116A reported the following errors: \n" + text);
		}
	}

	private void SendCmd(string cmd)
	{
		try
		{
			if (!SimulationMode)
			{
				VisaHandle.WriteString(cmd + "\n");
			}
			WriteTPLMessage("IEEE488_WR", AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MT8870A_VISARESOURCE)] + ";" + cmd);
		}
		catch (Exception ex)
		{
			if (!VisaHandle.IsOpen())
			{
				throw new Exception("Cannot communicate with MN8116A. " + ex.Message);
			}
			throw new Exception(ex.Message);
		}
	}

	private string ReadResp()
	{
		string text = "";
		try
		{
			if (!SimulationMode)
			{
				text = VisaHandle.ReadString();
			}
			WriteTPLMessage("IEEE488_RD", AddrListGroups[Convert.ToInt16(MN8116A_BUS_CONFIG.MT8870A_VISARESOURCE)] + ";" + text);
			return text;
		}
		catch (Exception ex)
		{
			if (!VisaHandle.IsOpen())
			{
				throw new Exception("Cannot communicate with MN8116A");
			}
			throw new Exception(ex.Message);
		}
	}

	private void Close()
	{
		try
		{
			if (!SimulationMode && VisaHandle.IsOpen())
			{
				VisaHandle.Close();
			}
		}
		catch (Exception ex)
		{
			if (!VisaHandle.IsOpen())
			{
				throw new Exception("Cannot communicate with MN8116A. " + ex.Message);
			}
			throw new Exception(ex.Message);
		}
	}

	private bool CheckMpsIpRes(string resIp, string configIp)
	{
		bool result = false;
		if (resIp.Contains(configIp))
		{
			result = true;
		}
		return result;
	}

	private bool CheckMpsPortRes(string resPort, string configPortout, string configPortin)
	{
		bool result = false;
		string[] array = resPort.Split(',');
		if (array[0].Contains("PORT" + configPortout) && array[1].Contains("PORT" + configPortin))
		{
			result = true;
		}
		return result;
	}
}
