using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MultiLanguageManger;

namespace QC.QMSCT;

public class PortSetupData
{
	private bool dualPortSolution;

	private bool manualSetup = true;

	private DetectionInstance detectionInstance = DetectionInstance.DETECT_INST_PORT_NO;

	private Dictionary<int, PortDataType> panelPortMap = new Dictionary<int, PortDataType>();

	public bool DualPortSolution
	{
		get
		{
			return dualPortSolution;
		}
		set
		{
			dualPortSolution = value;
		}
	}

	public int TotalNumberOfPanels
	{
		get
		{
			return panelPortMap.Count;
		}
		set
		{
			OnChangePanelNumber(value);
		}
	}

	public Dictionary<int, PortDataType> PanelPortMap => panelPortMap;

	public DetectionInstance DetectionInstance
	{
		get
		{
			return detectionInstance;
		}
		set
		{
			detectionInstance = value;
		}
	}

	public bool ManualSetup
	{
		get
		{
			return manualSetup;
		}
		set
		{
			manualSetup = value;
		}
	}

	public void LoadSetupData(StationConfiguration sc)
	{
		DetectionInstance = sc.DetectionInstanceValue;
		DualPortSolution = sc.DualPortSolution;
		ManualSetup = sc.ManualSetup;
		panelPortMap.Clear();
		if (sc.PortMapping == null)
		{
			return;
		}
		PortMapType[] portMapping = sc.PortMapping;
		foreach (PortMapType portMapType in portMapping)
		{
			int panelNumber = portMapType.PanelNumber;
			if (!panelPortMap.Keys.Contains(panelNumber))
			{
				panelPortMap.Add(panelNumber, new PortDataType());
				panelPortMap[panelNumber].COMPortAtStart = portMapType.PortData.COMPortAtStart;
				panelPortMap[panelNumber].COMPortAfterPowerCycle = portMapType.PortData.COMPortAfterPowerCycle;
				panelPortMap[panelNumber].USBPhysicalAddress = portMapType.PortData.USBPhysicalAddress;
				panelPortMap[panelNumber].PanelSpecificGV = portMapType.PortData.PanelSpecificGV;
			}
		}
	}

	public void CopySetupData(PortSetupData psd)
	{
		DetectionInstance = psd.DetectionInstance;
		DualPortSolution = psd.DualPortSolution;
		ManualSetup = psd.ManualSetup;
		panelPortMap.Clear();
		foreach (int key in psd.PanelPortMap.Keys)
		{
			panelPortMap.Add(key, new PortDataType());
			panelPortMap[key].COMPortAtStart = psd.PanelPortMap[key].COMPortAtStart;
			panelPortMap[key].COMPortAfterPowerCycle = psd.PanelPortMap[key].COMPortAfterPowerCycle;
			panelPortMap[key].USBPhysicalAddress = psd.PanelPortMap[key].USBPhysicalAddress;
			panelPortMap[key].PanelSpecificGV = psd.PanelPortMap[key].PanelSpecificGV;
		}
	}

	private void OnChangePanelNumber(int panelNumber)
	{
		int totalNumberOfPanels = TotalNumberOfPanels;
		for (int i = totalNumberOfPanels; i < panelNumber; i++)
		{
			AddEmptyPanel(i + 1);
		}
		for (int j = panelNumber; j < totalNumberOfPanels; j++)
		{
			panelPortMap.Remove(j + 1);
		}
	}

	public void AddEmptyPanel(int panel)
	{
		if (!panelPortMap.ContainsKey(panel))
		{
			panelPortMap.Add(panel, new PortDataType());
		}
		panelPortMap[panel].COMPortAtStart = 50 + panel;
		panelPortMap[panel].COMPortAfterPowerCycle = 100 + panel;
		panelPortMap[panel].USBPhysicalAddress = "PCIROOT(" + panel + ")";
		panelPortMap[panel].PanelSpecificGV = "";
	}

	public void AddPanelPort(int panel, int port)
	{
		if (!panelPortMap.ContainsKey(panel))
		{
			panelPortMap.Add(panel, new PortDataType());
			panelPortMap[panel].PanelSpecificGV = "";
		}
		panelPortMap[panel].COMPortAtStart = port;
		panelPortMap[panel].COMPortAfterPowerCycle = 100 + panel;
		panelPortMap[panel].USBPhysicalAddress = "PCIROOT(" + panel + ")";
	}

	public void AddPanelPort(int panel, int port, int portAfterPowerCycle)
	{
		if (!panelPortMap.ContainsKey(panel))
		{
			panelPortMap.Add(panel, new PortDataType());
			panelPortMap[panel].PanelSpecificGV = "";
		}
		panelPortMap[panel].COMPortAtStart = port;
		panelPortMap[panel].COMPortAfterPowerCycle = portAfterPowerCycle;
		panelPortMap[panel].USBPhysicalAddress = "";
	}

	public void AddPanelPort(int panel, int port, string physicalAddress)
	{
		if (!panelPortMap.ContainsKey(panel))
		{
			panelPortMap.Add(panel, new PortDataType());
			panelPortMap[panel].PanelSpecificGV = "";
		}
		panelPortMap[panel].COMPortAtStart = port;
		panelPortMap[panel].COMPortAfterPowerCycle = 100 + panel;
		panelPortMap[panel].USBPhysicalAddress = physicalAddress;
	}

	public void EditPanelSpecificGV(int panel, string panelSpecificGV)
	{
		if (panelPortMap.ContainsKey(panel))
		{
			panelPortMap[panel].PanelSpecificGV = panelSpecificGV;
		}
	}

	private bool CheckPhysicalAddress()
	{
		Dictionary<int, string> dictionary = new Dictionary<int, string>();
		foreach (KeyValuePair<int, PortDataType> item in panelPortMap)
		{
			if (item.Key <= 0 || item.Key > TotalNumberOfPanels || dictionary.Keys.Contains(item.Key))
			{
				return false;
			}
			if (!dictionary.Values.Contains(item.Value.USBPhysicalAddress))
			{
				dictionary.Add(item.Key, item.Value.USBPhysicalAddress);
				if (dictionary.Count > TotalNumberOfPanels)
				{
					return false;
				}
				continue;
			}
			return false;
		}
		return true;
	}

	private bool CheckPhysicalAddressDetailed(int panelIndex, ref string detailedMessage)
	{
		detailedMessage = "";
		if (!panelPortMap.Keys.Contains(panelIndex))
		{
			return true;
		}
		string text = "";
		string text2 = "";
		int num = 0;
		foreach (KeyValuePair<int, PortDataType> item in panelPortMap)
		{
			if (item.Key > 0 && item.Key <= TotalNumberOfPanels && panelIndex != item.Key)
			{
				if (item.Value.USBPhysicalAddress == panelPortMap[panelIndex].USBPhysicalAddress)
				{
					text = ((!(text == "")) ? (text + "," + item.Key) : (text + panelIndex + "," + item.Key));
				}
				string text3 = item.Value.USBPhysicalAddress;
				if (panelPortMap[panelIndex].USBPhysicalAddress.LastIndexOf("\\PORT") > 0)
				{
					text3 = panelPortMap[panelIndex].USBPhysicalAddress.Substring(0, panelPortMap[panelIndex].USBPhysicalAddress.LastIndexOf("\\PORT"));
				}
				string text4 = item.Value.USBPhysicalAddress;
				if (item.Value.USBPhysicalAddress.LastIndexOf("\\PORT") > 0)
				{
					text4 = item.Value.USBPhysicalAddress.Substring(0, item.Value.USBPhysicalAddress.LastIndexOf("\\PORT"));
				}
				if (text3 == text4)
				{
					text2 = ((!(text2 == "")) ? (text2 + "," + item.Key) : (text2 + panelIndex + "," + item.Key));
					num++;
				}
			}
		}
		if (text != "")
		{
			detailedMessage = CommonResource.strPanel + " " + text + " " + CommonResource.BoxMsgDuplicatePort;
		}
		if (num >= 2 && text2 != "")
		{
			string text5 = detailedMessage;
			detailedMessage = text5 + "\n" + CommonResource.strPanel + " " + text2 + " " + CommonResource.BoxMsgHeavyLoad;
		}
		if (detailedMessage != "")
		{
			return false;
		}
		return true;
	}

	private bool CheckPortsNumber()
	{
		Dictionary<string, int> dictionary = new Dictionary<string, int>();
		foreach (KeyValuePair<int, PortDataType> item in panelPortMap)
		{
			string text = item.Key + "_startport";
			if (item.Key <= 0 || item.Key > TotalNumberOfPanels || dictionary.Keys.Contains(text))
			{
				return false;
			}
			if (!dictionary.Values.Contains(item.Value.COMPortAtStart))
			{
				if (item.Value.COMPortAtStart != 0)
				{
					dictionary.Add(text, item.Value.COMPortAtStart);
					if (dualPortSolution)
					{
						text = item.Key + "_resetport";
						if (dictionary.Keys.Contains(text))
						{
							return false;
						}
						if (dictionary.Values.Contains(item.Value.COMPortAfterPowerCycle))
						{
							return false;
						}
						if (item.Value.COMPortAfterPowerCycle == 0)
						{
							return false;
						}
						dictionary.Add(text, item.Value.COMPortAfterPowerCycle);
						if (dictionary.Count > TotalNumberOfPanels * 2)
						{
							return false;
						}
					}
					else if (dictionary.Count > TotalNumberOfPanels)
					{
						return false;
					}
					continue;
				}
				return false;
			}
			return false;
		}
		return true;
	}

	private bool CheckPortsNumberDetailed(int panelIndex, ref string detailedMessage)
	{
		detailedMessage = "";
		if (!panelPortMap.Keys.Contains(panelIndex))
		{
			return true;
		}
		string text = "";
		foreach (KeyValuePair<int, PortDataType> item in panelPortMap)
		{
			if (item.Key > 0 && item.Key <= TotalNumberOfPanels && panelIndex != item.Key)
			{
				if (item.Value.COMPortAtStart == panelPortMap[panelIndex].COMPortAtStart)
				{
					text = ((!(text == "")) ? (text + "," + item.Key) : (text + panelIndex + "," + item.Key));
				}
				else if (dualPortSolution && (panelPortMap[panelIndex].COMPortAtStart == panelPortMap[panelIndex].COMPortAfterPowerCycle || item.Value.COMPortAtStart == panelPortMap[panelIndex].COMPortAfterPowerCycle || item.Value.COMPortAfterPowerCycle == panelPortMap[panelIndex].COMPortAtStart || item.Value.COMPortAfterPowerCycle == panelPortMap[panelIndex].COMPortAfterPowerCycle))
				{
					text = ((!(text == "")) ? (text + "," + item.Key) : (text + item.Key));
				}
			}
		}
		if (text != "")
		{
			detailedMessage = CommonResource.strPanel + " " + text + " " + CommonResource.BoxMsgDuplicatePort;
			return false;
		}
		return true;
	}

	public bool CheckPortData()
	{
		if (detectionInstance == DetectionInstance.DETECT_INST_PHY_ADDR)
		{
			return CheckPhysicalAddress();
		}
		return CheckPortsNumber();
	}

	public bool CheckPortDataDetailed(int panelIndex, ref string detailedMessage)
	{
		if (detectionInstance == DetectionInstance.DETECT_INST_PHY_ADDR)
		{
			return CheckPhysicalAddressDetailed(panelIndex + 1, ref detailedMessage);
		}
		return CheckPortsNumberDetailed(panelIndex + 1, ref detailedMessage);
	}

	private bool GetUserInputYesNo(string message, string caption)
	{
		DialogResult dialogResult = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);
		if (dialogResult == DialogResult.No)
		{
			return false;
		}
		return true;
	}
}
