using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC;

public class NFCMainWorkerThread : WorkerThreadBase
{
	private ManualResetEvent initDeviceEvent = new ManualResetEvent(initialState: false);

	private ManualResetEvent patchFirmwareEvent = new ManualResetEvent(initialState: false);

	protected List<ManualResetEvent> eventList = new List<ManualResetEvent>();

	protected NFCAPI nfcAPI;

	protected CombinedScripter combinedScripter;

	private NFCConfigSettings nfcConfigSettings;

	private string patchFilename;

	public NFCMainWorkerThread(Logger logger, NFCAPI nfcAPI)
		: base(logger)
	{
		this.nfcAPI = nfcAPI;
		eventList.Add(shutdownThreadEvent);
		eventList.Add(initDeviceEvent);
		eventList.Add(patchFirmwareEvent);
	}

	public void SetCombinedScripter(CombinedScripter combinedScripter)
	{
		this.combinedScripter = combinedScripter;
	}

	public void InitDevice(NFCConfigSettings nfcConfigSettings)
	{
		this.nfcConfigSettings = nfcConfigSettings;
		initDeviceEvent.Set();
	}

	public void PatchFirmware(string filename)
	{
		patchFilename = filename;
		patchFirmwareEvent.Set();
	}

	protected override void Run()
	{
		logger.Enqueue(new LogMessage(LogMessageType.Info, "NFC main thread starting"));
		int index;
		do
		{
			index = WaitHandle.WaitAny(eventList.ToArray());
			HandleEvent(eventList[index]);
		}
		while (eventList[index] != shutdownThreadEvent);
		logger.Enqueue(new LogMessage(LogMessageType.Info, "NFC main thread ending"));
	}

	protected virtual void HandleEvent(ManualResetEvent manualResetEvent)
	{
		if (manualResetEvent == initDeviceEvent)
		{
			InitDeviceHandler(nfcConfigSettings);
			initDeviceEvent.Reset();
		}
		else if (manualResetEvent == patchFirmwareEvent)
		{
			PatchFirmwareHandler(patchFilename);
			patchFirmwareEvent.Reset();
		}
	}

	protected virtual void InitDeviceHandler(NFCConfigSettings configSettings)
	{
		if (!configSettings.RunNFCTopLevelScript && !configSettings.LoadNFCPatchFile)
		{
			return;
		}
		try
		{
			if (configSettings.RunNFCTopLevelScript)
			{
				logger.Enqueue(new LogMessage(LogMessageType.Info, "Automatically running NFC top level script"));
				string workingDirectory = Path.GetDirectoryName(configSettings.NFCTopLevelScript) + "\\";
				combinedScripter.RunScriptSync(configSettings.NFCTopLevelScript, workingDirectory);
			}
		}
		catch (Exception ex)
		{
			logger.Enqueue(new LogMessage(LogMessageType.Error, ex));
			MessageBoxes.ShowErrorBox("Error running NFC top level script");
			return;
		}
		try
		{
			if (configSettings.LoadNFCPatchFile)
			{
				logger.Enqueue(new LogMessage(LogMessageType.Info, "Automatically patching NFC firmware"));
				nfcAPI.PatchFirmware(configSettings.DefaultNFCPatchFile);
			}
		}
		catch (Exception ex2)
		{
			logger.Enqueue(new LogMessage(LogMessageType.Error, ex2));
			MessageBoxes.ShowErrorBox("Error patching NFC firmware");
			return;
		}
		logger.Enqueue(new LogMessage(LogMessageType.Info, "Device init finished"));
	}

	protected void PatchFirmwareHandler(string filename)
	{
		try
		{
			nfcAPI.PatchFirmware(filename);
		}
		catch (Exception ex)
		{
			logger.Enqueue(new LogMessage(LogMessageType.Error, ex));
			MessageBoxes.ShowErrorBox("Error patching NFC firmware");
		}
	}
}
