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

namespace QCRPG.TSL.NFC;

public class NFCConfigUserControl : UserControlBase, IConfigurable
{
	private NFCConfigSettings nfcConfigSettings;

	private bool topLevelScriptInitialDirectorySet;

	private bool nvmInitialDirectorySet;

	private bool patchInitialDirectorySet;

	private IContainer components;

	private Label labelDefaultNfcNvmFile;

	private TextBox textBoxDefaultNfcNvmFile;

	private Button buttonChooseDefaultNfcNvmFile;

	private Label labelDefaultNfcPatchFile;

	private TextBox textBoxDefaultNfcPatchFile;

	private Button buttonChooseDefaultNfcPatchFile;

	private CheckBox checkBoxOpenNFCConnectionOnStartup;

	private CheckBox checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand;

	private TextBox textBoxTopLevelScript;

	private Button buttonChooseTopLevelScript;

	private Label labelTopLevelScript;

	private CheckBox checkBoxRunTopLevelScriptOnPortOpen;

	private CheckBox checkBoxLoadPatchFileOnPortOpen;

	public NFCConfigUserControl(NFCConfigSettings nfcConfigSettings)
		: base("Config")
	{
		this.nfcConfigSettings = nfcConfigSettings;
		InitializeComponent();
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		configuration.SetString("NFCTopLevelScript", nfcConfigSettings.NFCTopLevelScript);
		configuration.SetString("DefaultNFCNVMFile", nfcConfigSettings.DefaultNFCNVMFile);
		configuration.SetString("DefaultNFCPatchFile", nfcConfigSettings.DefaultNFCPatchFile);
		configuration.SetBool("OpenNFCConnectionOnStartup", nfcConfigSettings.OpenNFCConnectionOnStartup);
		configuration.SetBool("RunNFCTopLevelScript", nfcConfigSettings.RunNFCTopLevelScript);
		configuration.SetBool("LoadNFCPatchFile", nfcConfigSettings.LoadNFCPatchFile);
		configuration.SetBool("LoadNFCNVMFile", nfcConfigSettings.LoadNFCNVMFile);
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		nfcConfigSettings.NFCTopLevelScript = configuration.GetString("NFCTopLevelScript", "");
		textBoxTopLevelScript.Text = nfcConfigSettings.NFCTopLevelScript;
		nfcConfigSettings.DefaultNFCNVMFile = configuration.GetString("DefaultNFCNVMFile", "");
		textBoxDefaultNfcNvmFile.Text = nfcConfigSettings.DefaultNFCNVMFile;
		nfcConfigSettings.DefaultNFCPatchFile = configuration.GetString("DefaultNFCPatchFile", "");
		textBoxDefaultNfcPatchFile.Text = nfcConfigSettings.DefaultNFCPatchFile;
		nfcConfigSettings.OpenNFCConnectionOnStartup = configuration.GetBool("OpenNFCConnectionOnStartup", defaultValue: false);
		checkBoxOpenNFCConnectionOnStartup.Checked = nfcConfigSettings.OpenNFCConnectionOnStartup;
		nfcConfigSettings.RunNFCTopLevelScript = configuration.GetBool("RunNFCTopLevelScript", defaultValue: false);
		checkBoxRunTopLevelScriptOnPortOpen.Checked = nfcConfigSettings.RunNFCTopLevelScript;
		nfcConfigSettings.LoadNFCPatchFile = configuration.GetBool("LoadNFCPatchFile", defaultValue: false);
		checkBoxLoadPatchFileOnPortOpen.Checked = nfcConfigSettings.LoadNFCPatchFile;
		nfcConfigSettings.LoadNFCNVMFile = configuration.GetBool("LoadNFCNVMFile", defaultValue: false);
		checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Checked = nfcConfigSettings.LoadNFCNVMFile;
	}

	public void SetInternalControlVisibility(bool visible)
	{
		labelDefaultNfcNvmFile.Visible = visible;
		textBoxDefaultNfcNvmFile.Visible = visible;
		buttonChooseDefaultNfcNvmFile.Visible = visible;
		checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Visible = visible;
	}

	private void buttonChooseTopLevelScript_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!topLevelScriptInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			topLevelScriptInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Tcl Files (*.tcl)|*.tcl|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			nfcConfigSettings.NFCTopLevelScript = openFileDialog.FileName;
			textBoxTopLevelScript.Text = nfcConfigSettings.NFCTopLevelScript;
		}
	}

	private void buttonChooseDefaultNfcNvmFile_Click(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)
		{
			nfcConfigSettings.DefaultNFCNVMFile = openFileDialog.FileName;
			textBoxDefaultNfcNvmFile.Text = nfcConfigSettings.DefaultNFCNVMFile;
		}
	}

	private void textBoxDefaultNfcNvmFile_TextChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.DefaultNFCNVMFile = textBoxDefaultNfcNvmFile.Text;
	}

	private void buttonChooseDefaultNfcPatchFile_Click(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)
		{
			nfcConfigSettings.DefaultNFCPatchFile = openFileDialog.FileName;
			textBoxDefaultNfcPatchFile.Text = nfcConfigSettings.DefaultNFCPatchFile;
		}
	}

	private void textBoxDefaultNfcPatchFile_TextChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.DefaultNFCPatchFile = textBoxDefaultNfcPatchFile.Text;
	}

	private void checkBoxOpenNFCConnectionOnStartup_CheckedChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.OpenNFCConnectionOnStartup = ((CheckBox)sender).Checked;
	}

	private void checkBoxRunTopLevelScriptOnPortOpen_CheckedChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.RunNFCTopLevelScript = ((CheckBox)sender).Checked;
	}

	private void checkBoxLoadPatchFileOnPortOpen_CheckedChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.LoadNFCPatchFile = ((CheckBox)sender).Checked;
	}

	private void checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand_CheckedChanged(object sender, EventArgs e)
	{
		nfcConfigSettings.LoadNFCNVMFile = ((CheckBox)sender).Checked;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.labelDefaultNfcNvmFile = new System.Windows.Forms.Label();
		this.textBoxDefaultNfcNvmFile = new System.Windows.Forms.TextBox();
		this.buttonChooseDefaultNfcNvmFile = new System.Windows.Forms.Button();
		this.labelDefaultNfcPatchFile = new System.Windows.Forms.Label();
		this.textBoxDefaultNfcPatchFile = new System.Windows.Forms.TextBox();
		this.buttonChooseDefaultNfcPatchFile = new System.Windows.Forms.Button();
		this.checkBoxOpenNFCConnectionOnStartup = new System.Windows.Forms.CheckBox();
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand = new System.Windows.Forms.CheckBox();
		this.textBoxTopLevelScript = new System.Windows.Forms.TextBox();
		this.buttonChooseTopLevelScript = new System.Windows.Forms.Button();
		this.labelTopLevelScript = new System.Windows.Forms.Label();
		this.checkBoxRunTopLevelScriptOnPortOpen = new System.Windows.Forms.CheckBox();
		this.checkBoxLoadPatchFileOnPortOpen = new System.Windows.Forms.CheckBox();
		base.SuspendLayout();
		this.labelDefaultNfcNvmFile.AutoSize = true;
		this.labelDefaultNfcNvmFile.Location = new System.Drawing.Point(15, 69);
		this.labelDefaultNfcNvmFile.Name = "labelDefaultNfcNvmFile";
		this.labelDefaultNfcNvmFile.Size = new System.Drawing.Size(111, 13);
		this.labelDefaultNfcNvmFile.TabIndex = 6;
		this.labelDefaultNfcNvmFile.Text = "Default NFC NVM File";
		this.textBoxDefaultNfcNvmFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxDefaultNfcNvmFile.Location = new System.Drawing.Point(132, 66);
		this.textBoxDefaultNfcNvmFile.Name = "textBoxDefaultNfcNvmFile";
		this.textBoxDefaultNfcNvmFile.Size = new System.Drawing.Size(222, 20);
		this.textBoxDefaultNfcNvmFile.TabIndex = 7;
		this.textBoxDefaultNfcNvmFile.TextChanged += new System.EventHandler(textBoxDefaultNfcNvmFile_TextChanged);
		this.buttonChooseDefaultNfcNvmFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseDefaultNfcNvmFile.AutoSize = true;
		this.buttonChooseDefaultNfcNvmFile.Location = new System.Drawing.Point(360, 64);
		this.buttonChooseDefaultNfcNvmFile.Name = "buttonChooseDefaultNfcNvmFile";
		this.buttonChooseDefaultNfcNvmFile.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseDefaultNfcNvmFile.TabIndex = 8;
		this.buttonChooseDefaultNfcNvmFile.Text = "...";
		this.buttonChooseDefaultNfcNvmFile.UseVisualStyleBackColor = true;
		this.buttonChooseDefaultNfcNvmFile.Click += new System.EventHandler(buttonChooseDefaultNfcNvmFile_Click);
		this.labelDefaultNfcPatchFile.AutoSize = true;
		this.labelDefaultNfcPatchFile.Location = new System.Drawing.Point(15, 43);
		this.labelDefaultNfcPatchFile.Name = "labelDefaultNfcPatchFile";
		this.labelDefaultNfcPatchFile.Size = new System.Drawing.Size(115, 13);
		this.labelDefaultNfcPatchFile.TabIndex = 3;
		this.labelDefaultNfcPatchFile.Text = "Default NFC Patch File";
		this.textBoxDefaultNfcPatchFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxDefaultNfcPatchFile.Location = new System.Drawing.Point(132, 40);
		this.textBoxDefaultNfcPatchFile.Name = "textBoxDefaultNfcPatchFile";
		this.textBoxDefaultNfcPatchFile.Size = new System.Drawing.Size(222, 20);
		this.textBoxDefaultNfcPatchFile.TabIndex = 4;
		this.textBoxDefaultNfcPatchFile.TextChanged += new System.EventHandler(textBoxDefaultNfcPatchFile_TextChanged);
		this.buttonChooseDefaultNfcPatchFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseDefaultNfcPatchFile.AutoSize = true;
		this.buttonChooseDefaultNfcPatchFile.Location = new System.Drawing.Point(360, 38);
		this.buttonChooseDefaultNfcPatchFile.Name = "buttonChooseDefaultNfcPatchFile";
		this.buttonChooseDefaultNfcPatchFile.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseDefaultNfcPatchFile.TabIndex = 5;
		this.buttonChooseDefaultNfcPatchFile.Text = "...";
		this.buttonChooseDefaultNfcPatchFile.UseVisualStyleBackColor = true;
		this.buttonChooseDefaultNfcPatchFile.Click += new System.EventHandler(buttonChooseDefaultNfcPatchFile_Click);
		this.checkBoxOpenNFCConnectionOnStartup.AutoSize = true;
		this.checkBoxOpenNFCConnectionOnStartup.Location = new System.Drawing.Point(18, 104);
		this.checkBoxOpenNFCConnectionOnStartup.Name = "checkBoxOpenNFCConnectionOnStartup";
		this.checkBoxOpenNFCConnectionOnStartup.Size = new System.Drawing.Size(163, 17);
		this.checkBoxOpenNFCConnectionOnStartup.TabIndex = 9;
		this.checkBoxOpenNFCConnectionOnStartup.Text = "Open Connection On Startup";
		this.checkBoxOpenNFCConnectionOnStartup.UseVisualStyleBackColor = true;
		this.checkBoxOpenNFCConnectionOnStartup.CheckedChanged += new System.EventHandler(checkBoxOpenNFCConnectionOnStartup_CheckedChanged);
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.AutoSize = true;
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Location = new System.Drawing.Point(18, 173);
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Name = "checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand";
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Size = new System.Drawing.Size(347, 17);
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.TabIndex = 12;
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.Text = "Load NVM File On Port Open Or WriteNFCNVMFile Script Command";
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.UseVisualStyleBackColor = true;
		this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand.CheckedChanged += new System.EventHandler(checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand_CheckedChanged);
		this.textBoxTopLevelScript.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxTopLevelScript.Location = new System.Drawing.Point(132, 14);
		this.textBoxTopLevelScript.Name = "textBoxTopLevelScript";
		this.textBoxTopLevelScript.Size = new System.Drawing.Size(222, 20);
		this.textBoxTopLevelScript.TabIndex = 1;
		this.buttonChooseTopLevelScript.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseTopLevelScript.AutoSize = true;
		this.buttonChooseTopLevelScript.Location = new System.Drawing.Point(360, 12);
		this.buttonChooseTopLevelScript.Name = "buttonChooseTopLevelScript";
		this.buttonChooseTopLevelScript.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseTopLevelScript.TabIndex = 2;
		this.buttonChooseTopLevelScript.Text = "...";
		this.buttonChooseTopLevelScript.UseVisualStyleBackColor = true;
		this.buttonChooseTopLevelScript.Click += new System.EventHandler(buttonChooseTopLevelScript_Click);
		this.labelTopLevelScript.AutoSize = true;
		this.labelTopLevelScript.Location = new System.Drawing.Point(15, 17);
		this.labelTopLevelScript.Name = "labelTopLevelScript";
		this.labelTopLevelScript.Size = new System.Drawing.Size(85, 13);
		this.labelTopLevelScript.TabIndex = 0;
		this.labelTopLevelScript.Text = "Top Level Script";
		this.checkBoxRunTopLevelScriptOnPortOpen.AutoSize = true;
		this.checkBoxRunTopLevelScriptOnPortOpen.Location = new System.Drawing.Point(18, 127);
		this.checkBoxRunTopLevelScriptOnPortOpen.Name = "checkBoxRunTopLevelScriptOnPortOpen";
		this.checkBoxRunTopLevelScriptOnPortOpen.Size = new System.Drawing.Size(195, 17);
		this.checkBoxRunTopLevelScriptOnPortOpen.TabIndex = 10;
		this.checkBoxRunTopLevelScriptOnPortOpen.Text = "Run Top Level Script On Port Open";
		this.checkBoxRunTopLevelScriptOnPortOpen.UseVisualStyleBackColor = true;
		this.checkBoxRunTopLevelScriptOnPortOpen.CheckedChanged += new System.EventHandler(checkBoxRunTopLevelScriptOnPortOpen_CheckedChanged);
		this.checkBoxLoadPatchFileOnPortOpen.AutoSize = true;
		this.checkBoxLoadPatchFileOnPortOpen.Location = new System.Drawing.Point(18, 150);
		this.checkBoxLoadPatchFileOnPortOpen.Name = "checkBoxLoadPatchFileOnPortOpen";
		this.checkBoxLoadPatchFileOnPortOpen.Size = new System.Drawing.Size(168, 17);
		this.checkBoxLoadPatchFileOnPortOpen.TabIndex = 11;
		this.checkBoxLoadPatchFileOnPortOpen.Text = "Load Patch File On Port Open";
		this.checkBoxLoadPatchFileOnPortOpen.UseVisualStyleBackColor = true;
		this.checkBoxLoadPatchFileOnPortOpen.CheckedChanged += new System.EventHandler(checkBoxLoadPatchFileOnPortOpen_CheckedChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.checkBoxLoadPatchFileOnPortOpen);
		base.Controls.Add(this.checkBoxRunTopLevelScriptOnPortOpen);
		base.Controls.Add(this.labelTopLevelScript);
		base.Controls.Add(this.buttonChooseTopLevelScript);
		base.Controls.Add(this.textBoxTopLevelScript);
		base.Controls.Add(this.checkBoxLoadNFCNVMFileOnPortOpenOrWriteNFCNVMFileScriptCommand);
		base.Controls.Add(this.checkBoxOpenNFCConnectionOnStartup);
		base.Controls.Add(this.buttonChooseDefaultNfcPatchFile);
		base.Controls.Add(this.labelDefaultNfcPatchFile);
		base.Controls.Add(this.buttonChooseDefaultNfcNvmFile);
		base.Controls.Add(this.textBoxDefaultNfcPatchFile);
		base.Controls.Add(this.textBoxDefaultNfcNvmFile);
		base.Controls.Add(this.labelDefaultNfcNvmFile);
		base.Name = "NFCConfigUserControl";
		base.Size = new System.Drawing.Size(400, 300);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
