using System;
using System.Collections.Generic;
using System.Timers;

namespace QC.QMSCT;

public class DebugUnitDetector : IUnitDetector
{
	private DetectionEventMode eventMode = DetectionEventMode.DETECT_MODE_TIMER;

	private DetectionInstance detectionInstance = DetectionInstance.DETECT_INST_PORT_NO;

	private UDEDetectionMode udeDetectionMode;

	private Dictionary<int, int[]> expectedPorts = new Dictionary<int, int[]>();

	private Dictionary<int, string> expectedPhysicalAddress = new Dictionary<int, string>();

	private string expectedHardwareId = "";

	private DebugPanel debugPanel = new DebugPanel();

	private Timer timer;

	private USBEnumerator usbEnumerator = new USBEnumerator();

	public bool PortKeyWordCaseSensitive
	{
		set
		{
			usbEnumerator.PortKeyWordCaseSensitive = value;
		}
	}

	public List<string> PortKeyWord => usbEnumerator.PortKeyWord;

	public DetectionEventMode SetEventMode
	{
		set
		{
			eventMode = value;
		}
	}

	public DetectionInstance SetDetectionInstance
	{
		set
		{
			detectionInstance = value;
		}
	}

	public UDEDetectionMode SetUDEDetectionMode
	{
		set
		{
			udeDetectionMode = value;
		}
	}

	public Dictionary<int, int[]> ExpectedListOfPorts
	{
		get
		{
			return expectedPorts;
		}
		set
		{
			expectedPorts = value;
		}
	}

	public Dictionary<int, string> ExpectedListOfPhysicalAddress
	{
		get
		{
			return expectedPhysicalAddress;
		}
		set
		{
			expectedPhysicalAddress = value;
		}
	}

	public string ExpectedHardwareId
	{
		get
		{
			return expectedHardwareId;
		}
		set
		{
			expectedHardwareId = value;
		}
	}

	public event ActivePositionsChangedGlobal OnChange;

	public event ActivePositionsChangedGlobalGV OnChangeGV;

	public void Stop()
	{
		if (eventMode == DetectionEventMode.DETECT_MODE_TIMER)
		{
			if (timer != null)
			{
				timer.Stop();
			}
		}
		else
		{
			debugPanel.Hide();
		}
	}

	public void Start()
	{
		int num = 0;
		num = ((detectionInstance != 0) ? expectedPorts.Count : expectedPhysicalAddress.Count);
		debugPanel.ClearAll(num);
		debugPanel.Show();
		DetectAndNotify();
		debugPanel.SetEventMode = eventMode;
		if (eventMode == DetectionEventMode.DETECT_MODE_TIMER)
		{
			if (timer == null)
			{
				timer = new Timer(2000.0);
				timer.Elapsed += timer_EventArrived;
				timer.AutoReset = true;
				timer.Enabled = true;
			}
			timer.Start();
		}
		else
		{
			debugPanel.OnPanelCheckBoxClicked += watcher_EventArrived;
		}
	}

	private void timer_EventArrived(object source, ElapsedEventArgs e)
	{
		DetectAndNotify();
	}

	private void watcher_EventArrived()
	{
		DetectAndNotify();
	}

	public void DetectAndNotify()
	{
		if (detectionInstance == DetectionInstance.DETECT_INST_PORT_NO)
		{
			IsReleventPortConnected();
		}
		else
		{
			IsReleventDeviceConnected();
		}
	}

	public void IsReleventPortConnected()
	{
		if (expectedPorts.Count == 0)
		{
			throw new Exception("Unit detector is not initialized correctly!");
		}
		int num = 0;
		Dictionary<int, int[]>.KeyCollection keys = expectedPorts.Keys;
		foreach (int item in keys)
		{
			if (debugPanel.GetPortStatus(num))
			{
				this.OnChange(num, connected: true, expectedPorts[item][0]);
			}
			else
			{
				this.OnChange(num, connected: false, 0);
			}
			num++;
		}
	}

	public void IsReleventDeviceConnected()
	{
		if (expectedPhysicalAddress.Count == 0)
		{
			throw new Exception("Unit detector is not initialized correctly!");
		}
		int num = 0;
		Dictionary<int, string>.KeyCollection keys = expectedPhysicalAddress.Keys;
		foreach (int item in keys)
		{
			if (debugPanel.GetPortStatus(num))
			{
				List<int> intPortNumberCollection = new List<int>();
				usbEnumerator.GetPortNumberByUSBDevicePhysicalAddress(expectedPhysicalAddress[item], ref intPortNumberCollection);
				if (intPortNumberCollection.Count > 0)
				{
					this.OnChange(num, connected: true, intPortNumberCollection[0]);
				}
			}
			else
			{
				this.OnChange(num, connected: false, 0);
			}
			num++;
		}
	}
}
