using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace QCRPG.Framework;

public class DeviceEmulationModeUserControl : UserControlBase, IConfigurable
{
	private DeviceEmulationModeConfigSettings deviceEmulationModeConfigSettings;

	private PacketAutoResponder packetAutoResponder;

	private Color normalTextBoxBackColor;

	private IContainer components;

	private CheckBox checkBoxAutomaticallyRespondToCommands;

	private TextBox textBoxAutomaticResponseDelayMs;

	private Label labelAutomaticResponseDelayMs;

	public DeviceEmulationModeUserControl(DeviceEmulationModeConfigSettings deviceEmulationModeConfigSettings, PacketAutoResponder packetAutoResponder)
		: base("Device Emulation Mode")
	{
		this.deviceEmulationModeConfigSettings = deviceEmulationModeConfigSettings;
		this.packetAutoResponder = packetAutoResponder;
		InitializeComponent();
		normalTextBoxBackColor = textBoxAutomaticResponseDelayMs.BackColor;
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		configuration.SetBool("AutomaticallyRespondToCommands", deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands);
		configuration.SetInt("AutomaticResponseDelayMs", deviceEmulationModeConfigSettings.AutomaticResponseDelayMs);
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands = configuration.GetBool("AutomaticallyRespondToCommands", defaultValue: true);
		checkBoxAutomaticallyRespondToCommands.Checked = deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands;
		packetAutoResponder.SetState(deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands);
		deviceEmulationModeConfigSettings.AutomaticResponseDelayMs = configuration.GetInt("AutomaticResponseDelayMs", 10);
		if (deviceEmulationModeConfigSettings.AutomaticResponseDelayMs < 0)
		{
			deviceEmulationModeConfigSettings.AutomaticResponseDelayMs = 0;
		}
		textBoxAutomaticResponseDelayMs.Text = deviceEmulationModeConfigSettings.AutomaticResponseDelayMs.ToString();
		packetAutoResponder.SetDelayMs(deviceEmulationModeConfigSettings.AutomaticResponseDelayMs);
	}

	private void checkBoxAutomaticallyRespondToCommands_CheckedChanged(object sender, EventArgs e)
	{
		deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands = checkBoxAutomaticallyRespondToCommands.Checked;
		packetAutoResponder.SetState(deviceEmulationModeConfigSettings.AutomaticallyRespondToCommands);
	}

	private void textBoxAutomaticResponseDelayMs_TextChanged(object sender, EventArgs e)
	{
		try
		{
			int num = Convert.ToInt32(textBoxAutomaticResponseDelayMs.Text);
			if (num < 0)
			{
				throw new Exception("Delay must be > 0");
			}
			deviceEmulationModeConfigSettings.AutomaticResponseDelayMs = num;
			textBoxAutomaticResponseDelayMs.BackColor = normalTextBoxBackColor;
			packetAutoResponder.SetDelayMs(deviceEmulationModeConfigSettings.AutomaticResponseDelayMs);
		}
		catch (Exception)
		{
			textBoxAutomaticResponseDelayMs.BackColor = Color.Red;
		}
	}

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

	private void InitializeComponent()
	{
		this.checkBoxAutomaticallyRespondToCommands = new System.Windows.Forms.CheckBox();
		this.textBoxAutomaticResponseDelayMs = new System.Windows.Forms.TextBox();
		this.labelAutomaticResponseDelayMs = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.checkBoxAutomaticallyRespondToCommands.AutoSize = true;
		this.checkBoxAutomaticallyRespondToCommands.Location = new System.Drawing.Point(18, 13);
		this.checkBoxAutomaticallyRespondToCommands.Name = "checkBoxAutomaticallyRespondToCommands";
		this.checkBoxAutomaticallyRespondToCommands.Size = new System.Drawing.Size(205, 17);
		this.checkBoxAutomaticallyRespondToCommands.TabIndex = 0;
		this.checkBoxAutomaticallyRespondToCommands.Text = "Automatically Respond To Commands";
		this.checkBoxAutomaticallyRespondToCommands.UseVisualStyleBackColor = true;
		this.checkBoxAutomaticallyRespondToCommands.CheckedChanged += new System.EventHandler(checkBoxAutomaticallyRespondToCommands_CheckedChanged);
		this.textBoxAutomaticResponseDelayMs.Location = new System.Drawing.Point(178, 36);
		this.textBoxAutomaticResponseDelayMs.Name = "textBoxAutomaticResponseDelayMs";
		this.textBoxAutomaticResponseDelayMs.Size = new System.Drawing.Size(52, 20);
		this.textBoxAutomaticResponseDelayMs.TabIndex = 2;
		this.textBoxAutomaticResponseDelayMs.TextChanged += new System.EventHandler(textBoxAutomaticResponseDelayMs_TextChanged);
		this.labelAutomaticResponseDelayMs.AutoSize = true;
		this.labelAutomaticResponseDelayMs.Location = new System.Drawing.Point(15, 39);
		this.labelAutomaticResponseDelayMs.Name = "labelAutomaticResponseDelayMs";
		this.labelAutomaticResponseDelayMs.Size = new System.Drawing.Size(157, 13);
		this.labelAutomaticResponseDelayMs.TabIndex = 1;
		this.labelAutomaticResponseDelayMs.Text = "Automatic Response Delay (ms)";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.labelAutomaticResponseDelayMs);
		base.Controls.Add(this.textBoxAutomaticResponseDelayMs);
		base.Controls.Add(this.checkBoxAutomaticallyRespondToCommands);
		base.Name = "DeviceEmulationModeUserControl";
		base.Size = new System.Drawing.Size(400, 300);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
