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

namespace QCRPG.TSL.FMHCI;

public class FMHCIConfigUserControl : UserControlBase, IConfigurable
{
	private FMHCIConfigSettings fmhciConfigSettings;

	private bool nvmInitialDirectorySet;

	private bool patchInitialDirectorySet;

	private bool spurTableInitialDirectorySet;

	private IContainer components;

	private Button buttonChooseDefaultFmHciNvmFile;

	private TextBox textBoxDefaultFmHciNvmFile;

	private Label labelDefaultFmHciNvmFile;

	private CheckBox checkBoxOpenFMHCIConnectionOnStartup;

	private Label labelDefaultFmHciSpurTableFile;

	private TextBox textBoxDefaultFmHciPatchFile;

	private Button buttonChooseDefaultFmHciPatchFile;

	private TextBox textBoxDefaultFmHciSpurTableFile;

	private Button buttonChooseDefaultFmHciSpurTableFile;

	private Label labelDefaultFmHciPatchFile;

	public FMHCIConfigUserControl(FMHCIConfigSettings fmhciConfigSettings)
		: base("Config")
	{
		this.fmhciConfigSettings = fmhciConfigSettings;
		InitializeComponent();
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		configuration.SetString("DefaultFMHCINVMFile", fmhciConfigSettings.DefaultFMHCINVMFile);
		configuration.SetString("DefaultFMHCIPatchFile", fmhciConfigSettings.DefaultFMHCIPatchFile);
		configuration.SetString("DefaultFMHCISpurTableFile", fmhciConfigSettings.DefaultFMHCISpurTableFile);
		configuration.SetBool("OpenFMHCIConnectionOnStartup", fmhciConfigSettings.OpenFMHCIConnectionOnStartup);
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		fmhciConfigSettings.DefaultFMHCINVMFile = configuration.GetString("DefaultFMHCINVMFile", "");
		textBoxDefaultFmHciNvmFile.Text = fmhciConfigSettings.DefaultFMHCINVMFile;
		fmhciConfigSettings.DefaultFMHCIPatchFile = configuration.GetString("DefaultFMHCIPatchFile", "");
		textBoxDefaultFmHciPatchFile.Text = fmhciConfigSettings.DefaultFMHCIPatchFile;
		fmhciConfigSettings.DefaultFMHCISpurTableFile = configuration.GetString("DefaultFMHCISpurTableFile", "");
		textBoxDefaultFmHciSpurTableFile.Text = fmhciConfigSettings.DefaultFMHCISpurTableFile;
		fmhciConfigSettings.OpenFMHCIConnectionOnStartup = configuration.GetBool("OpenFMHCIConnectionOnStartup", defaultValue: false);
		checkBoxOpenFMHCIConnectionOnStartup.Checked = fmhciConfigSettings.OpenFMHCIConnectionOnStartup;
	}

	private void buttonChooseDefaultFmHciNvmFile_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!nvmInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			nvmInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Nvm Files (*.nvm)|*.nvm|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			fmhciConfigSettings.DefaultFMHCINVMFile = openFileDialog.FileName;
			textBoxDefaultFmHciNvmFile.Text = fmhciConfigSettings.DefaultFMHCINVMFile;
		}
	}

	private void buttonChooseDefaultFmHciPatchFile_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!patchInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			patchInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Patch Image Files (*.img)|*.img|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			fmhciConfigSettings.DefaultFMHCIPatchFile = openFileDialog.FileName;
			textBoxDefaultFmHciPatchFile.Text = fmhciConfigSettings.DefaultFMHCIPatchFile;
		}
	}

	private void buttonChooseDefaultFmHciSpurTableFile_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (!spurTableInitialDirectorySet)
		{
			openFileDialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			spurTableInitialDirectorySet = true;
		}
		openFileDialog.Filter = "Spur Table Files (*.txt)|*.txt|All Files (*.*)|*.*";
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			fmhciConfigSettings.DefaultFMHCISpurTableFile = openFileDialog.FileName;
			textBoxDefaultFmHciSpurTableFile.Text = fmhciConfigSettings.DefaultFMHCISpurTableFile;
		}
	}

	private void textBoxDefaultFmHciNvmFile_TextChanged(object sender, EventArgs e)
	{
		fmhciConfigSettings.DefaultFMHCINVMFile = textBoxDefaultFmHciNvmFile.Text;
	}

	private void textBoxDefaultFmHciPatchFile_TextChanged(object sender, EventArgs e)
	{
		fmhciConfigSettings.DefaultFMHCIPatchFile = textBoxDefaultFmHciPatchFile.Text;
	}

	private void textBoxDefaultFmHciSpurTableFile_TextChanged(object sender, EventArgs e)
	{
		fmhciConfigSettings.DefaultFMHCISpurTableFile = textBoxDefaultFmHciSpurTableFile.Text;
	}

	private void checkBoxOpenFMHCIConnectionOnStartup_CheckedChanged(object sender, EventArgs e)
	{
		fmhciConfigSettings.OpenFMHCIConnectionOnStartup = ((CheckBox)sender).Checked;
	}

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

	private void InitializeComponent()
	{
		this.buttonChooseDefaultFmHciNvmFile = new System.Windows.Forms.Button();
		this.textBoxDefaultFmHciNvmFile = new System.Windows.Forms.TextBox();
		this.labelDefaultFmHciNvmFile = new System.Windows.Forms.Label();
		this.checkBoxOpenFMHCIConnectionOnStartup = new System.Windows.Forms.CheckBox();
		this.labelDefaultFmHciSpurTableFile = new System.Windows.Forms.Label();
		this.textBoxDefaultFmHciPatchFile = new System.Windows.Forms.TextBox();
		this.buttonChooseDefaultFmHciPatchFile = new System.Windows.Forms.Button();
		this.textBoxDefaultFmHciSpurTableFile = new System.Windows.Forms.TextBox();
		this.buttonChooseDefaultFmHciSpurTableFile = new System.Windows.Forms.Button();
		this.labelDefaultFmHciPatchFile = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.buttonChooseDefaultFmHciNvmFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseDefaultFmHciNvmFile.AutoSize = true;
		this.buttonChooseDefaultFmHciNvmFile.Location = new System.Drawing.Point(360, 11);
		this.buttonChooseDefaultFmHciNvmFile.Name = "buttonChooseDefaultFmHciNvmFile";
		this.buttonChooseDefaultFmHciNvmFile.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseDefaultFmHciNvmFile.TabIndex = 8;
		this.buttonChooseDefaultFmHciNvmFile.Text = "...";
		this.buttonChooseDefaultFmHciNvmFile.UseVisualStyleBackColor = true;
		this.buttonChooseDefaultFmHciNvmFile.Click += new System.EventHandler(buttonChooseDefaultFmHciNvmFile_Click);
		this.textBoxDefaultFmHciNvmFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxDefaultFmHciNvmFile.Location = new System.Drawing.Point(175, 13);
		this.textBoxDefaultFmHciNvmFile.Name = "textBoxDefaultFmHciNvmFile";
		this.textBoxDefaultFmHciNvmFile.Size = new System.Drawing.Size(179, 20);
		this.textBoxDefaultFmHciNvmFile.TabIndex = 7;
		this.textBoxDefaultFmHciNvmFile.TextChanged += new System.EventHandler(textBoxDefaultFmHciNvmFile_TextChanged);
		this.labelDefaultFmHciNvmFile.AutoSize = true;
		this.labelDefaultFmHciNvmFile.Location = new System.Drawing.Point(15, 16);
		this.labelDefaultFmHciNvmFile.Name = "labelDefaultFmHciNvmFile";
		this.labelDefaultFmHciNvmFile.Size = new System.Drawing.Size(126, 13);
		this.labelDefaultFmHciNvmFile.TabIndex = 5;
		this.labelDefaultFmHciNvmFile.Text = "Default FM HCI NVM File";
		this.checkBoxOpenFMHCIConnectionOnStartup.AutoSize = true;
		this.checkBoxOpenFMHCIConnectionOnStartup.Location = new System.Drawing.Point(18, 114);
		this.checkBoxOpenFMHCIConnectionOnStartup.Name = "checkBoxOpenFMHCIConnectionOnStartup";
		this.checkBoxOpenFMHCIConnectionOnStartup.Size = new System.Drawing.Size(163, 17);
		this.checkBoxOpenFMHCIConnectionOnStartup.TabIndex = 9;
		this.checkBoxOpenFMHCIConnectionOnStartup.Text = "Open Connection On Startup";
		this.checkBoxOpenFMHCIConnectionOnStartup.UseVisualStyleBackColor = true;
		this.checkBoxOpenFMHCIConnectionOnStartup.CheckedChanged += new System.EventHandler(checkBoxOpenFMHCIConnectionOnStartup_CheckedChanged);
		this.labelDefaultFmHciSpurTableFile.AutoSize = true;
		this.labelDefaultFmHciSpurTableFile.Location = new System.Drawing.Point(15, 68);
		this.labelDefaultFmHciSpurTableFile.Name = "labelDefaultFmHciSpurTableFile";
		this.labelDefaultFmHciSpurTableFile.Size = new System.Drawing.Size(154, 13);
		this.labelDefaultFmHciSpurTableFile.TabIndex = 10;
		this.labelDefaultFmHciSpurTableFile.Text = "Default FM HCI Spur Table File";
		this.textBoxDefaultFmHciPatchFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxDefaultFmHciPatchFile.Location = new System.Drawing.Point(175, 39);
		this.textBoxDefaultFmHciPatchFile.Name = "textBoxDefaultFmHciPatchFile";
		this.textBoxDefaultFmHciPatchFile.Size = new System.Drawing.Size(179, 20);
		this.textBoxDefaultFmHciPatchFile.TabIndex = 11;
		this.textBoxDefaultFmHciPatchFile.TextChanged += new System.EventHandler(textBoxDefaultFmHciPatchFile_TextChanged);
		this.buttonChooseDefaultFmHciPatchFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseDefaultFmHciPatchFile.AutoSize = true;
		this.buttonChooseDefaultFmHciPatchFile.Location = new System.Drawing.Point(360, 37);
		this.buttonChooseDefaultFmHciPatchFile.Name = "buttonChooseDefaultFmHciPatchFile";
		this.buttonChooseDefaultFmHciPatchFile.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseDefaultFmHciPatchFile.TabIndex = 12;
		this.buttonChooseDefaultFmHciPatchFile.Text = "...";
		this.buttonChooseDefaultFmHciPatchFile.UseVisualStyleBackColor = true;
		this.buttonChooseDefaultFmHciPatchFile.Click += new System.EventHandler(buttonChooseDefaultFmHciPatchFile_Click);
		this.textBoxDefaultFmHciSpurTableFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBoxDefaultFmHciSpurTableFile.Location = new System.Drawing.Point(175, 65);
		this.textBoxDefaultFmHciSpurTableFile.Name = "textBoxDefaultFmHciSpurTableFile";
		this.textBoxDefaultFmHciSpurTableFile.Size = new System.Drawing.Size(179, 20);
		this.textBoxDefaultFmHciSpurTableFile.TabIndex = 11;
		this.textBoxDefaultFmHciSpurTableFile.TextChanged += new System.EventHandler(textBoxDefaultFmHciSpurTableFile_TextChanged);
		this.buttonChooseDefaultFmHciSpurTableFile.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.buttonChooseDefaultFmHciSpurTableFile.AutoSize = true;
		this.buttonChooseDefaultFmHciSpurTableFile.Location = new System.Drawing.Point(360, 63);
		this.buttonChooseDefaultFmHciSpurTableFile.Name = "buttonChooseDefaultFmHciSpurTableFile";
		this.buttonChooseDefaultFmHciSpurTableFile.Size = new System.Drawing.Size(26, 23);
		this.buttonChooseDefaultFmHciSpurTableFile.TabIndex = 12;
		this.buttonChooseDefaultFmHciSpurTableFile.Text = "...";
		this.buttonChooseDefaultFmHciSpurTableFile.UseVisualStyleBackColor = true;
		this.buttonChooseDefaultFmHciSpurTableFile.Click += new System.EventHandler(buttonChooseDefaultFmHciSpurTableFile_Click);
		this.labelDefaultFmHciPatchFile.AutoSize = true;
		this.labelDefaultFmHciPatchFile.Location = new System.Drawing.Point(15, 42);
		this.labelDefaultFmHciPatchFile.Name = "labelDefaultFmHciPatchFile";
		this.labelDefaultFmHciPatchFile.Size = new System.Drawing.Size(130, 13);
		this.labelDefaultFmHciPatchFile.TabIndex = 10;
		this.labelDefaultFmHciPatchFile.Text = "Default FM HCI Patch File";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.buttonChooseDefaultFmHciSpurTableFile);
		base.Controls.Add(this.buttonChooseDefaultFmHciPatchFile);
		base.Controls.Add(this.textBoxDefaultFmHciSpurTableFile);
		base.Controls.Add(this.textBoxDefaultFmHciPatchFile);
		base.Controls.Add(this.labelDefaultFmHciPatchFile);
		base.Controls.Add(this.labelDefaultFmHciSpurTableFile);
		base.Controls.Add(this.checkBoxOpenFMHCIConnectionOnStartup);
		base.Controls.Add(this.buttonChooseDefaultFmHciNvmFile);
		base.Controls.Add(this.textBoxDefaultFmHciNvmFile);
		base.Controls.Add(this.labelDefaultFmHciNvmFile);
		base.Name = "FMHCIConfigUserControl";
		base.Size = new System.Drawing.Size(400, 300);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
