using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC;

public class NFCGooierInternal : NFCGooier
{
	private const string menuTextLoadDefaultNVMFile = "Load Default NVM File";

	private const string menuTextLoadNVMFile = "Load NVM File";

	private const string menuTextLoadDefaultPatchFile = "Load Default Patch File";

	private const string menuTextLoadPatchFile = "Load Patch File";

	private ToolStripButton toolStripButtonLoadDefaultNvm;

	private ToolStripButton toolStripButtonLoadNvm;

	private ToolStripButton toolStripButtonLoadDefaultPatch;

	private ToolStripButton toolStripButtonLoadPatch;

	private ToolStripProgressBar toolStripProgressBarLoadPatch;

	private bool nvmInitialDirectorySet;

	private bool patchInitialDirectorySet;

	private NFCPeekPokeUserControl nfcPeekPokeUserControl;

	private NFCATETranslationUserControl nfcATETranslationUserControl;

	private NFCMainWorkerThreadInternal nfcMainWorkerThreadInternal;

	public NFCGooierInternal(string technologyName, Logger logger, Configurer configurer, NFCAPI nfcAPI, PacketListBase packetList, NFCConfigSettings nfcConfigSettings, NFCMainWorkerThreadInternal nfcMainWorkerThreadInternal)
		: base(technologyName, logger, configurer, nfcAPI, packetList, nfcConfigSettings, nfcMainWorkerThreadInternal)
	{
		this.nfcMainWorkerThreadInternal = nfcMainWorkerThreadInternal;
		nfcConfigUserControl.SetInternalControlVisibility(visible: true);
		nfcPeekPokeUserControl = new NFCPeekPokeUserControl(nfcMainWorkerThreadInternal);
		nfcATETranslationUserControl = new NFCATETranslationUserControl();
	}

	protected override void AddUserControls()
	{
		userControls.Add(packetUserControl);
		userControls.Add(nfcConfigUserControl);
		userControls.Add(nfcPeekPokeUserControl);
		userControls.Add(nfcATETranslationUserControl);
		configurer.RegisterConfigurableObject(packetUserControl, "PacketUserControlSettings");
		configurer.RegisterConfigurableObject(nfcConfigUserControl, "NFCConfigUserControlSettings");
		configurer.RegisterConfigurableObject(nfcPeekPokeUserControl, "NFCPeekPokeUserControl");
	}

	protected override void BuildDropDownMenu()
	{
		dropDownMenu.Text = technologyName;
		AddOpenCloseDropDownMenuItems();
		AddNvmDropDownMenuItems();
		SetDropDownMenuItemNames();
	}

	private void AddNvmDropDownMenuItems()
	{
		dropDownMenu.DropDownItems.Add("Load Default NVM File", null, LoadDefaultNvmHandler);
		dropDownMenu.DropDownItems.Add("Load NVM File", null, LoadNvmHandler);
		dropDownMenu.DropDownItems.Add("Load Default Patch File", null, LoadDefaultPatchHandler);
		dropDownMenu.DropDownItems.Add("Load Patch File", null, LoadPatchHandler);
	}

	protected override void BuildCustomToolStrip()
	{
		AddOpenCloseToolStripItems();
		AddNvmCustomToolStripItems();
	}

	private void AddNvmCustomToolStripItems()
	{
		toolStripButtonLoadDefaultNvm = new ToolStripButton(Resources.GetImage("LoadDefaultNvm.png"));
		toolStripButtonLoadDefaultNvm.DisplayStyle = ToolStripItemDisplayStyle.Image;
		toolStripButtonLoadDefaultNvm.AutoToolTip = true;
		toolStripButtonLoadDefaultNvm.ToolTipText = "Load the default NFC NVM file specified in the config";
		toolStripButtonLoadDefaultNvm.Click += LoadDefaultNvmHandler;
		customToolStripItems.Add(toolStripButtonLoadDefaultNvm);
		toolStripButtonLoadNvm = new ToolStripButton(Resources.GetImage("LoadNvm.png"));
		toolStripButtonLoadNvm.DisplayStyle = ToolStripItemDisplayStyle.Image;
		toolStripButtonLoadNvm.AutoToolTip = true;
		toolStripButtonLoadNvm.ToolTipText = "Choose and load an NFC NVM file";
		toolStripButtonLoadNvm.Click += LoadNvmHandler;
		customToolStripItems.Add(toolStripButtonLoadNvm);
		toolStripButtonLoadDefaultPatch = new ToolStripButton(Resources.GetImage("LoadDefaultPatch.png"));
		toolStripButtonLoadDefaultPatch.DisplayStyle = ToolStripItemDisplayStyle.Image;
		toolStripButtonLoadDefaultPatch.AutoToolTip = true;
		toolStripButtonLoadDefaultPatch.ToolTipText = "Load the default NFC firmware patch file specified in the config";
		toolStripButtonLoadDefaultPatch.Click += LoadDefaultPatchHandler;
		customToolStripItems.Add(toolStripButtonLoadDefaultPatch);
		toolStripButtonLoadPatch = new ToolStripButton(Resources.GetImage("LoadPatch.png"));
		toolStripButtonLoadPatch.DisplayStyle = ToolStripItemDisplayStyle.Image;
		toolStripButtonLoadPatch.AutoToolTip = true;
		toolStripButtonLoadPatch.ToolTipText = "Choose and load an NFC firmware patch file";
		toolStripButtonLoadPatch.Click += LoadPatchHandler;
		customToolStripItems.Add(toolStripButtonLoadPatch);
		toolStripProgressBarLoadPatch = new ToolStripProgressBar();
		toolStripProgressBarLoadPatch.AutoSize = false;
		toolStripProgressBarLoadPatch.Width /= 2;
		customToolStripItems.Add(toolStripProgressBarLoadPatch);
		nfcAPI.RegisterPatchFirmwarePercentCompleteObserver(new NFCFirmwarePatchProgressHandler(toolStripProgressBarLoadPatch));
	}

	private void LoadDefaultNvmHandler(object sender, EventArgs e)
	{
		nfcMainWorkerThreadInternal.WriteNVM(nfcConfigSettings.DefaultNFCNVMFile);
	}

	private void LoadNvmHandler(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!nvmInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			nvmInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Xml Files (*.xml)|*.xml|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			nfcMainWorkerThreadInternal.WriteNVM(openFileDialog.FileName);
		}
	}

	private void LoadDefaultPatchHandler(object sender, EventArgs e)
	{
		nfcMainWorkerThreadInternal.PatchFirmware(nfcConfigSettings.DefaultNFCPatchFile);
	}

	private void LoadPatchHandler(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!patchInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			patchInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Out Files (*.out)|*.out|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			nfcMainWorkerThreadInternal.PatchFirmware(openFileDialog.FileName);
		}
	}

	protected override void SetControlEnables(ControlEnableMode controlEnableMode)
	{
		base.SetControlEnables(controlEnableMode);
		switch (controlEnableMode)
		{
		case ControlEnableMode.DEFAULT:
			SetControlEnables(ControlEnableMode.CONNECTION_CLOSED);
			break;
		case ControlEnableMode.CONNECTION_OPENED:
			dropDownMenu.DropDownItems["Load Default NVM File"].Enabled = true;
			dropDownMenu.DropDownItems["Load NVM File"].Enabled = true;
			toolStripButtonLoadDefaultNvm.Enabled = true;
			toolStripButtonLoadNvm.Enabled = true;
			dropDownMenu.DropDownItems["Load Default Patch File"].Enabled = true;
			dropDownMenu.DropDownItems["Load Patch File"].Enabled = true;
			toolStripButtonLoadDefaultPatch.Enabled = true;
			toolStripButtonLoadPatch.Enabled = true;
			break;
		case ControlEnableMode.CONNECTION_CLOSED:
			dropDownMenu.DropDownItems["Load Default NVM File"].Enabled = false;
			dropDownMenu.DropDownItems["Load NVM File"].Enabled = false;
			toolStripButtonLoadDefaultNvm.Enabled = false;
			toolStripButtonLoadNvm.Enabled = false;
			dropDownMenu.DropDownItems["Load Default Patch File"].Enabled = false;
			dropDownMenu.DropDownItems["Load Patch File"].Enabled = false;
			toolStripButtonLoadDefaultPatch.Enabled = false;
			toolStripButtonLoadPatch.Enabled = false;
			break;
		default:
			throw new Exception("Unhandled control enable mode: " + controlEnableMode);
		}
	}
}
