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

namespace QC.QMSCT;

public class DataGridViewManager
{
	private const string PANEL = "Panel";

	private const string PORT = "1st Port";

	private const string PORT_AFTER_POWER = "2nd Port";

	private const string PORT_PHY_ADDR = "Phy Addr";

	private const string PANEL_SPEC_GV = "Panel Specific GV";

	private const string NET_MODE = "Networ Adapter";

	private Dictionary<int, DataRow> keyRowMap = new Dictionary<int, DataRow>();

	private DataGridView panelPortMapView;

	private bool panelSpecificGVMode;

	private PortSetupData portDataManager = new PortSetupData();

	private PortSetupData portDataManagerOrg = new PortSetupData();

	public bool DualPortSolution
	{
		get
		{
			return portDataManager.DualPortSolution;
		}
		set
		{
			portDataManager.DualPortSolution = value;
			UpdateGridView();
		}
	}

	public DetectionInstance DetectionInstance
	{
		get
		{
			return portDataManager.DetectionInstance;
		}
		set
		{
			portDataManager.DetectionInstance = value;
			UpdateGridView();
		}
	}

	public bool PanelSpecificGVMode
	{
		get
		{
			return panelSpecificGVMode;
		}
		set
		{
			panelSpecificGVMode = value;
			UpdateGridView();
		}
	}

	public PortSetupData PortDataManager => portDataManager;

	public int TotalNumberOfPanels
	{
		get
		{
			return portDataManager.TotalNumberOfPanels;
		}
		set
		{
			portDataManager.TotalNumberOfPanels = value;
			UpdateGridView();
		}
	}

	public bool ManualSetup
	{
		get
		{
			return portDataManager.ManualSetup;
		}
		set
		{
			portDataManager.ManualSetup = value;
		}
	}

	public DataGridViewManager(DataGridView panelportmapview)
	{
		panelPortMapView = panelportmapview;
		UpdateGridView();
	}

	public void LoadSetupData(StationConfiguration sc)
	{
		portDataManager.LoadSetupData(sc);
		portDataManagerOrg.LoadSetupData(sc);
		UpdateGridView();
	}

	public void SetPanelSpecificGVMode(bool enabled)
	{
		panelSpecificGVMode = enabled;
	}

	private DataTable ConvertToDataTable(Dictionary<int, PortDataType> portvalues)
	{
		DataTable dataTable = new DataTable();
		keyRowMap.Clear();
		dataTable.Columns.Add(new DataColumn("Panel", typeof(int)));
		if (panelSpecificGVMode)
		{
			dataTable.Columns.Add(new DataColumn("Panel Specific GV", typeof(string)));
		}
		else if (DetectionInstance == DetectionInstance.DETECT_INST_NET_MODE)
		{
			dataTable.Columns.Add(new DataColumn("Networ Adapter", typeof(string)));
		}
		else if (DetectionInstance == DetectionInstance.DETECT_INST_PHY_ADDR)
		{
			dataTable.Columns.Add(new DataColumn("Phy Addr", typeof(string)));
		}
		else
		{
			dataTable.Columns.Add(new DataColumn("1st Port", typeof(int)));
			if (DualPortSolution)
			{
				dataTable.Columns.Add(new DataColumn("2nd Port", typeof(int)));
			}
		}
		if (portvalues != null)
		{
			foreach (int key in portvalues.Keys)
			{
				DataRow dataRow = dataTable.NewRow();
				dataRow["Panel"] = key;
				if (panelSpecificGVMode)
				{
					dataRow["Panel Specific GV"] = portvalues[key].PanelSpecificGV;
				}
				else if (DetectionInstance == DetectionInstance.DETECT_INST_NET_MODE)
				{
					dataRow["Networ Adapter"] = portvalues[key].USBPhysicalAddress;
				}
				else if (DetectionInstance == DetectionInstance.DETECT_INST_PHY_ADDR)
				{
					dataRow["Phy Addr"] = portvalues[key].USBPhysicalAddress;
				}
				else
				{
					dataRow["1st Port"] = portvalues[key].COMPortAtStart;
					if (DualPortSolution)
					{
						dataRow["2nd Port"] = portvalues[key].COMPortAfterPowerCycle;
					}
				}
				keyRowMap[key] = dataRow;
				dataTable.Rows.Add(dataRow);
			}
		}
		return dataTable;
	}

	public void UpdateGridView()
	{
		DataTable dataSource = ConvertToDataTable(portDataManager.PanelPortMap);
		panelPortMapView.DataSource = dataSource;
		for (int i = 0; i < panelPortMapView.Columns.Count; i++)
		{
			panelPortMapView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
		}
	}

	public void RemoveAllElements()
	{
		if (panelPortMapView != null)
		{
			try
			{
				DataTable dataSource = ConvertToDataTable(null);
				panelPortMapView.DataSource = dataSource;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}

	public bool HasDataBeenModified()
	{
		if (portDataManager.DetectionInstance != portDataManagerOrg.DetectionInstance || PortDataManager.ManualSetup != portDataManagerOrg.ManualSetup || PortDataManager.DualPortSolution != portDataManagerOrg.DualPortSolution || portDataManager.PanelPortMap.Count != portDataManagerOrg.PanelPortMap.Count)
		{
			return true;
		}
		foreach (int key in portDataManager.PanelPortMap.Keys)
		{
			if (!portDataManagerOrg.PanelPortMap.Keys.Contains(key))
			{
				return true;
			}
			if (portDataManager.PanelPortMap[key].COMPortAtStart != portDataManagerOrg.PanelPortMap[key].COMPortAtStart || portDataManager.PanelPortMap[key].COMPortAfterPowerCycle != portDataManagerOrg.PanelPortMap[key].COMPortAfterPowerCycle || portDataManager.PanelPortMap[key].USBPhysicalAddress != portDataManagerOrg.PanelPortMap[key].USBPhysicalAddress || portDataManager.PanelPortMap[key].PanelSpecificGV != portDataManagerOrg.PanelPortMap[key].PanelSpecificGV)
			{
				return true;
			}
		}
		return false;
	}

	public void RecallOriginalSetting()
	{
		portDataManager.CopySetupData(portDataManagerOrg);
		UpdateGridView();
	}

	public bool SaveData()
	{
		if (portDataManager.CheckPortData())
		{
			portDataManagerOrg.CopySetupData(portDataManager);
			return true;
		}
		return false;
	}

	public bool CheckPortDataDetailed(int panelIndex, ref string detailedMessage)
	{
		return portDataManager.CheckPortDataDetailed(panelIndex, ref detailedMessage);
	}
}
