using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QCRPG.Framework;

public class OpenConnectionPortRateForm : Form, IConfigurable
{
	private string selectedPortName = "";

	private bool setDisplayedPortNamesExternally;

	private List<string> displayedPortNameList = new List<string>();

	private int selectedDataRate;

	private List<int> displayedDataRateList = new List<int>();

	private List<string> flowControlList = new List<string>();

	private bool sharedConnectionEnabled;

	private string serialSelectedFlowControl = "None";

	private Handshake serialSelectedHandShake;

	private IContainer components;

	private ComboBox comboBoxDataRate;

	private ToolStripStatusLabel toolStripStatusLabel;

	private StatusStrip statusStrip;

	private Button buttonCancel;

	private Button buttonOK;

	private ComboBox comboBoxPortNames;

	private Label labelPortName;

	private Label labelDataRate;

	private ComboBox comboBoxFlowControl;

	private Label labelFlowControl;

	private CheckBox checkBoxSharedPort;

	public string Title
	{
		get
		{
			return Text;
		}
		set
		{
			Text = value;
		}
	}

	public string SelectedPortName
	{
		get
		{
			return selectedPortName;
		}
		set
		{
			selectedPortName = value;
		}
	}

	public List<string> DisplayedPortNameList
	{
		set
		{
			displayedPortNameList = value;
			setDisplayedPortNamesExternally = true;
		}
	}

	public int SelectedDataRate
	{
		get
		{
			return selectedDataRate;
		}
		set
		{
			selectedDataRate = value;
		}
	}

	public List<int> DisplayedDataRateList
	{
		set
		{
			displayedDataRateList = value;
		}
	}

	public List<string> SerialFlowControlList
	{
		get
		{
			return flowControlList;
		}
		set
		{
			flowControlList = value;
		}
	}

	public bool SharedConnectionEnabled
	{
		get
		{
			return sharedConnectionEnabled;
		}
		set
		{
			sharedConnectionEnabled = value;
		}
	}

	public Handshake SerialSelectedHandShake
	{
		get
		{
			return serialSelectedHandShake;
		}
		set
		{
			serialSelectedHandShake = value;
		}
	}

	[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
	public static extern int StrCmpLogicalW(string psz1, string psz2);

	public OpenConnectionPortRateForm()
	{
		InitializeComponent();
		Title = "Open Connection";
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		configuration.SetString("SelectedPortName", selectedPortName);
		configuration.SetInt("DataRate", selectedDataRate);
		configuration.SetString("SerialSelectedHandShake", serialSelectedFlowControl);
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		selectedPortName = configuration.GetString("SelectedPortName", "COM1");
		selectedDataRate = configuration.GetInt("DataRate", 115200);
		serialSelectedFlowControl = configuration.GetString("SerialSelectedHandShake", "None");
	}

	private void OpenConnectionPortRateForm_Load(object sender, EventArgs e)
	{
		toolStripStatusLabel.Text = "";
		if (displayedPortNameList == null || displayedPortNameList.Count == 0 || !setDisplayedPortNamesExternally)
		{
			displayedPortNameList = new List<string>(SerialPort.GetPortNames());
			displayedPortNameList.Sort(StrCmpLogicalW);
		}
		comboBoxPortNames.Items.Clear();
		foreach (string displayedPortName in displayedPortNameList)
		{
			comboBoxPortNames.Items.Add(displayedPortName);
		}
		comboBoxPortNames.SelectedItem = selectedPortName;
		if (displayedDataRateList == null || displayedDataRateList.Count == 0)
		{
			displayedDataRateList = new List<int> { 300, 1200, 2400, 4800, 9600, 19200, 34800, 57600, 115200 };
		}
		comboBoxDataRate.Items.Clear();
		foreach (int displayedDataRate in displayedDataRateList)
		{
			comboBoxDataRate.Items.Add(displayedDataRate);
		}
		comboBoxDataRate.SelectedText = selectedDataRate.ToString();
		toolStripStatusLabel.ForeColor = Color.Red;
		comboBoxFlowControl.Items.Clear();
		if (SerialFlowControlList == null || SerialFlowControlList.Count == 0)
		{
			SerialFlowControlList = new List<string> { "None", "Hardware", "XOnXOff", "HardwareXOnXOff" };
		}
		foreach (string serialFlowControl in SerialFlowControlList)
		{
			comboBoxFlowControl.Items.Add(serialFlowControl);
		}
		comboBoxFlowControl.SelectedText = serialSelectedFlowControl;
	}

	private void buttonOK_Click(object sender, EventArgs e)
	{
		try
		{
			selectedPortName = comboBoxPortNames.Text;
		}
		catch (Exception)
		{
			toolStripStatusLabel.Text = "Select a port name from the list";
			return;
		}
		if (selectedPortName == null)
		{
			toolStripStatusLabel.Text = "Select a port name from the list";
			return;
		}
		try
		{
			selectedDataRate = Convert.ToInt32(comboBoxDataRate.Text);
		}
		catch (Exception)
		{
			toolStripStatusLabel.Text = "Invalid data rate";
			return;
		}
		try
		{
			serialSelectedFlowControl = comboBoxFlowControl.Text;
			serialSelectedHandShake = GetHandShakeFromString(comboBoxFlowControl.Text);
		}
		catch (Exception)
		{
			toolStripStatusLabel.Text = "Invalid handshake selection";
			return;
		}
		if (checkBoxSharedPort.CheckState == CheckState.Checked)
		{
			sharedConnectionEnabled = true;
		}
		else
		{
			sharedConnectionEnabled = false;
		}
		base.DialogResult = DialogResult.OK;
		Close();
	}

	private Handshake GetHandShakeFromString(string value)
	{
		string value2 = value.Replace("Hardware", "RequestToSend");
		return (Handshake)Enum.Parse(typeof(Handshake), value2);
	}

	private void buttonCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
		Close();
	}

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

	private void InitializeComponent()
	{
		this.comboBoxDataRate = new System.Windows.Forms.ComboBox();
		this.toolStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
		this.statusStrip = new System.Windows.Forms.StatusStrip();
		this.buttonCancel = new System.Windows.Forms.Button();
		this.buttonOK = new System.Windows.Forms.Button();
		this.comboBoxPortNames = new System.Windows.Forms.ComboBox();
		this.labelPortName = new System.Windows.Forms.Label();
		this.labelDataRate = new System.Windows.Forms.Label();
		this.comboBoxFlowControl = new System.Windows.Forms.ComboBox();
		this.labelFlowControl = new System.Windows.Forms.Label();
		this.checkBoxSharedPort = new System.Windows.Forms.CheckBox();
		this.statusStrip.SuspendLayout();
		base.SuspendLayout();
		this.comboBoxDataRate.FormattingEnabled = true;
		this.comboBoxDataRate.Location = new System.Drawing.Point(107, 50);
		this.comboBoxDataRate.Name = "comboBoxDataRate";
		this.comboBoxDataRate.Size = new System.Drawing.Size(121, 21);
		this.comboBoxDataRate.TabIndex = 17;
		this.toolStripStatusLabel.Name = "toolStripStatusLabel";
		this.toolStripStatusLabel.Size = new System.Drawing.Size(0, 17);
		this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[1] { this.toolStripStatusLabel });
		this.statusStrip.Location = new System.Drawing.Point(0, 174);
		this.statusStrip.Name = "statusStrip";
		this.statusStrip.Size = new System.Drawing.Size(238, 22);
		this.statusStrip.TabIndex = 11;
		this.statusStrip.Text = "statusStrip1";
		this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.buttonCancel.Location = new System.Drawing.Point(107, 140);
		this.buttonCancel.Name = "buttonCancel";
		this.buttonCancel.Size = new System.Drawing.Size(75, 23);
		this.buttonCancel.TabIndex = 10;
		this.buttonCancel.Text = "Cancel";
		this.buttonCancel.UseVisualStyleBackColor = true;
		this.buttonCancel.Click += new System.EventHandler(buttonCancel_Click);
		this.buttonOK.Location = new System.Drawing.Point(12, 140);
		this.buttonOK.Name = "buttonOK";
		this.buttonOK.Size = new System.Drawing.Size(75, 23);
		this.buttonOK.TabIndex = 9;
		this.buttonOK.Text = "OK";
		this.buttonOK.UseVisualStyleBackColor = true;
		this.buttonOK.Click += new System.EventHandler(buttonOK_Click);
		this.comboBoxPortNames.FormattingEnabled = true;
		this.comboBoxPortNames.Location = new System.Drawing.Point(107, 20);
		this.comboBoxPortNames.Name = "comboBoxPortNames";
		this.comboBoxPortNames.Size = new System.Drawing.Size(121, 21);
		this.comboBoxPortNames.TabIndex = 15;
		this.labelPortName.AutoSize = true;
		this.labelPortName.Location = new System.Drawing.Point(12, 23);
		this.labelPortName.Name = "labelPortName";
		this.labelPortName.Size = new System.Drawing.Size(57, 13);
		this.labelPortName.TabIndex = 14;
		this.labelPortName.Text = "&Port Name";
		this.labelDataRate.AutoSize = true;
		this.labelDataRate.Location = new System.Drawing.Point(12, 53);
		this.labelDataRate.Name = "labelDataRate";
		this.labelDataRate.Size = new System.Drawing.Size(56, 13);
		this.labelDataRate.TabIndex = 16;
		this.labelDataRate.Text = "Data &Rate";
		this.comboBoxFlowControl.FormattingEnabled = true;
		this.comboBoxFlowControl.Location = new System.Drawing.Point(107, 78);
		this.comboBoxFlowControl.Name = "comboBoxFlowControl";
		this.comboBoxFlowControl.Size = new System.Drawing.Size(121, 21);
		this.comboBoxFlowControl.TabIndex = 19;
		this.labelFlowControl.AutoSize = true;
		this.labelFlowControl.Location = new System.Drawing.Point(12, 81);
		this.labelFlowControl.Name = "labelFlowControl";
		this.labelFlowControl.Size = new System.Drawing.Size(65, 13);
		this.labelFlowControl.TabIndex = 18;
		this.labelFlowControl.Text = "&Flow Control";
		this.checkBoxSharedPort.AutoSize = true;
		this.checkBoxSharedPort.Location = new System.Drawing.Point(15, 117);
		this.checkBoxSharedPort.Name = "checkBoxSharedPort";
		this.checkBoxSharedPort.Size = new System.Drawing.Size(117, 17);
		this.checkBoxSharedPort.TabIndex = 20;
		this.checkBoxSharedPort.Text = "Shared Connection";
		this.checkBoxSharedPort.UseVisualStyleBackColor = true;
		base.AcceptButton = this.buttonOK;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.CancelButton = this.buttonCancel;
		base.ClientSize = new System.Drawing.Size(238, 196);
		base.Controls.Add(this.checkBoxSharedPort);
		base.Controls.Add(this.comboBoxFlowControl);
		base.Controls.Add(this.labelFlowControl);
		base.Controls.Add(this.comboBoxDataRate);
		base.Controls.Add(this.statusStrip);
		base.Controls.Add(this.buttonCancel);
		base.Controls.Add(this.buttonOK);
		base.Controls.Add(this.comboBoxPortNames);
		base.Controls.Add(this.labelPortName);
		base.Controls.Add(this.labelDataRate);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "OpenConnectionPortRateForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Open Connection";
		base.Load += new System.EventHandler(OpenConnectionPortRateForm_Load);
		this.statusStrip.ResumeLayout(false);
		this.statusStrip.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
