using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

namespace QC.QMSCT;

public abstract class QSPRUnitProgrammer : IUnitProgrammerInterface, IDisposable
{
	private UnitProgrammerGenericSetup genericSetup = new UnitProgrammerGenericSetup();

	private UnitProgrammerTechnicalSetup technicalSetup = new UnitProgrammerTechnicalSetup();

	private Dictionary<string, string> globalVbles = new Dictionary<string, string>();

	private int comPortAtStart;

	private int comPortAfterPowerCycle;

	private string serialNumber = "";

	private int index;

	private Color bgColor = Color.Blue;

	private string engine;

	public UnitProgrammerGenericSetup GenericSetup
	{
		get
		{
			return genericSetup;
		}
		set
		{
			genericSetup = value;
		}
	}

	public UnitProgrammerTechnicalSetup TechnicalSetup
	{
		get
		{
			return technicalSetup;
		}
		set
		{
			technicalSetup = value;
		}
	}

	protected Dictionary<string, string> GlobalVbles => globalVbles;

	public int COMPortAtStart
	{
		get
		{
			return comPortAtStart;
		}
		set
		{
			comPortAtStart = value;
			globalVbles["COMPORT_AT_START"] = comPortAtStart.ToString();
		}
	}

	public int COMPortAfterPowerCycle
	{
		get
		{
			return comPortAfterPowerCycle;
		}
		set
		{
			comPortAfterPowerCycle = value;
			globalVbles["COMPORT_AFTER_POWERCYCLE"] = comPortAfterPowerCycle.ToString();
		}
	}

	public string SerialNumber
	{
		get
		{
			return serialNumber;
		}
		set
		{
			serialNumber = value;
			globalVbles["SN"] = value.ToString();
		}
	}

	public string TestPlanFilePath
	{
		get
		{
			return TechnicalSetup.TestPlanFilePath;
		}
		set
		{
			TechnicalSetup.TestPlanFilePath = value;
		}
	}

	public int TestTimeoutSeconds
	{
		get
		{
			return TechnicalSetup.TestTimeoutSeconds;
		}
		set
		{
			TechnicalSetup.TestTimeoutSeconds = value;
			globalVbles["XTT_TIMEOUT"] = value.ToString();
		}
	}

	public int TestTimeoutSecondsOffset
	{
		get
		{
			return TechnicalSetup.TestTimeoutSecondsOffset;
		}
		set
		{
			TechnicalSetup.TestTimeoutSecondsOffset = value;
			globalVbles["XTT_TIMEOUT_OFFSET"] = value.ToString();
		}
	}

	public string PanelSpecificGV
	{
		get
		{
			return TechnicalSetup.PanelSpecificGV;
		}
		set
		{
			TechnicalSetup.PanelSpecificGV = value;
			globalVbles["PANEL_SPECIFIC"] = value;
		}
	}

	public string ErrorCode
	{
		get
		{
			globalVbles["ERRORCODE"] = GetErrorCode();
			return globalVbles["ERRORCODE"];
		}
	}

	public string SubErrorCode
	{
		get
		{
			globalVbles["SUBERRORCODE"] = GetSubErrorCode();
			return globalVbles["SUBERRORCODE"];
		}
	}

	public Dictionary<string, string> TechnologySpecificGV
	{
		get
		{
			return TechnicalSetup.TechnologySpecificGV;
		}
		set
		{
			ClearTechnologySpecificGVFromGV();
			TechnicalSetup.TechnologySpecificGV = value;
			foreach (string key in TechnicalSetup.TechnologySpecificGV.Keys)
			{
				globalVbles[key] = TechnicalSetup.TechnologySpecificGV[key];
			}
		}
	}

	public int Index
	{
		get
		{
			return index;
		}
		set
		{
			index = value;
			globalVbles["DUT_INDEX"] = index.ToString();
			globalVbles["PANEL"] = index.ToString();
		}
	}

	public Color BackGroundColor
	{
		get
		{
			return bgColor;
		}
		set
		{
			bgColor = value;
		}
	}

	public string EngineName
	{
		get
		{
			return engine;
		}
		set
		{
			engine = value;
		}
	}

	public abstract bool IsProcessAlive { get; }

	public abstract bool IsTestTreeValid { get; }

	public event EventHandler<BoolEventArgs> ShowEvent;

	public event EventHandler<BoolEventArgs> HideEvent;

	public event EventHandler<BoolEventArgs> AbortEvent;

	public event EventHandler<TextEventArgs> OnReceiveSN;

	public event EventHandler<TextEventArgs> OnModifyTestTimeout;

	public event EventHandler OnStarted;

	public event EventHandler OnShown;

	public event EventHandler OnHidden;

	public event EventHandler OnExited;

	public event EventHandler<TestResultEventArgs> OnDone;

	public event EventHandler<LogEventArgs> OnSetStatus;

	public event EventHandler<TextEventArgs> OnDisplayStatus;

	public event EventHandler<TextEventArgs> OnTestItemStarted;

	public event EventHandler<TestResultEventArgs> OnTestItemDone;

	protected abstract string GetErrorCode();

	protected abstract string GetSubErrorCode();

	private void ClearTechnologySpecificGVFromGV()
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		List<string> fields = CONSTGV.GetFields();
		foreach (string key in globalVbles.Keys)
		{
			if (fields.Contains(key))
			{
				dictionary[key] = globalVbles[key];
			}
		}
		globalVbles.Clear();
		globalVbles = dictionary;
	}

	public bool ValidateEngineEnvironment()
	{
		if (EngineName == "QSPRGUI" && File.Exists("QSPRAutomationInterface.dll"))
		{
			return true;
		}
		if (EngineName == "QIA" && File.Exists("QSPRSchedulerWrapper.dll"))
		{
			return true;
		}
		return false;
	}

	public abstract void Start();

	public abstract void RunTest();

	public abstract void Close();

	public virtual bool EnableShow()
	{
		return this.ShowEvent != null;
	}

	public virtual bool EnableHide()
	{
		return this.HideEvent != null;
	}

	public virtual bool EnableAbort()
	{
		return this.AbortEvent != null;
	}

	public virtual bool EnableStop()
	{
		return this.ShowEvent != null;
	}

	public virtual bool Show()
	{
		BoolEventArgs boolEventArgs = new BoolEventArgs(data: false);
		this.ShowEvent(this, boolEventArgs);
		return boolEventArgs.Data;
	}

	public virtual bool Hide()
	{
		BoolEventArgs boolEventArgs = new BoolEventArgs(data: false);
		this.HideEvent(this, boolEventArgs);
		return boolEventArgs.Data;
	}

	public virtual bool Abort()
	{
		BoolEventArgs boolEventArgs = new BoolEventArgs(data: false);
		this.AbortEvent(this, boolEventArgs);
		return boolEventArgs.Data;
	}

	public virtual bool Stop()
	{
		BoolEventArgs boolEventArgs = new BoolEventArgs(data: false);
		this.AbortEvent(this, boolEventArgs);
		return boolEventArgs.Data;
	}

	public virtual void Dispose()
	{
		Close();
	}

	protected void AppendToLog(string logMessage)
	{
		SetStatus("Engine", logMessage, 0);
	}

	protected void SetStatus(string window, string text, int tracelv)
	{
		if (this.OnSetStatus != null)
		{
			this.OnSetStatus(this, new LogEventArgs(window, text, tracelv));
		}
	}

	protected void DisplayStatus(string text)
	{
		if (this.OnDisplayStatus != null)
		{
			this.OnDisplayStatus(this, new TextEventArgs(text));
		}
	}

	protected void TestItemStarted(string testName)
	{
		if (this.OnTestItemStarted != null)
		{
			this.OnTestItemStarted(this, new TextEventArgs(testName));
		}
	}

	protected void TestItemDone(string testName, TestResult testResu)
	{
		if (this.OnTestItemDone != null)
		{
			this.OnTestItemDone(this, new TestResultEventArgs(testName, testResu));
		}
	}

	protected void ProcessDebugMessage(string windowName, string text, int traceLevel)
	{
		SetStatus(windowName, text, traceLevel);
	}

	protected void ProcessStarted()
	{
		if (this.OnStarted != null)
		{
			this.OnStarted(this, null);
		}
	}

	protected void ProcessExited(int exitCode)
	{
		if (this.OnExited != null)
		{
			this.OnExited(this, null);
		}
	}

	protected void ProcessShown(bool shownOkay)
	{
		if (shownOkay)
		{
			if (this.OnShown != null)
			{
				this.OnShown(this, null);
			}
		}
		else if (this.OnHidden != null)
		{
			this.OnHidden(this, null);
		}
	}

	protected void ProcessHidden(bool hiddenOkay)
	{
		if (hiddenOkay)
		{
			if (this.OnHidden != null)
			{
				this.OnHidden(this, null);
			}
		}
		else if (this.OnShown != null)
		{
			this.OnShown(this, null);
		}
	}

	protected void ProcessDone(TestResult testResu)
	{
		if (this.OnDone != null)
		{
			this.OnDone(this, new TestResultEventArgs("", testResu));
		}
	}

	protected void ProcessGlobalVariableModfied(string gvName, string gvValue)
	{
		try
		{
			if (gvName == null)
			{
				return;
			}
			if (gvName.CompareTo("SN") == 0 && gvValue != GlobalVbles["SN"] && this.OnReceiveSN != null)
			{
				this.OnReceiveSN(this, new TextEventArgs(gvValue));
			}
			if (gvName.CompareTo("XTT_TIMEOUT") == 0 && gvValue != GlobalVbles["XTT_TIMEOUT"])
			{
				TestTimeoutSeconds = int.Parse(gvValue);
				if (this.OnModifyTestTimeout != null)
				{
					this.OnModifyTestTimeout(this, new TextEventArgs(gvValue));
				}
			}
			if (gvName.CompareTo("XTT_TIMEOUT_OFFSET") == 0 && gvValue != GlobalVbles["XTT_TIMEOUT_OFFSET"] && gvValue.CompareTo("0") != 0)
			{
				TestTimeoutSecondsOffset = int.Parse(gvValue);
			}
		}
		catch
		{
		}
	}
}
