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

namespace QC.QTMSecurity;

public class ModifySettingsDialog : Form
{
	private const string SWITCH_TO_WINDOWS_GROUP_MODE_TEXT = "&Switch to WindowsGroup based security";

	private const string SWITCH_TO_PASSWORD_MODE_TEXT = "&Switch to Password based security";

	private const string TURN_OFF_SECURITY_TEXT = "&Turn off security";

	private const string TURN_ON_SECURITY_TEXT = "&Turn on security";

	private PictureBox pictureBox1;

	private Label lblOldPass;

	private Label lblNewPass;

	private Button buttonChangePass;

	private Button buttonEnablePass;

	private StatusBar statusBar;

	private TextBox textBoxPassword;

	private TextBox textBoxConfirmPass;

	private Label labelStatus;

	private Button buttonClose;

	private Button buttonDisableSecurity;

	private IContainer components;

	public ModifySettingsDialog()
	{
		InitializeComponent();
		statusBar.Text = DateTime.Now.ToLongDateString() + "   (" + DateTime.Now.ToLongTimeString() + ")";
		UpdateControls();
	}

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

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QC.QTMSecurity.ModifySettingsDialog));
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		this.textBoxPassword = new System.Windows.Forms.TextBox();
		this.textBoxConfirmPass = new System.Windows.Forms.TextBox();
		this.lblOldPass = new System.Windows.Forms.Label();
		this.lblNewPass = new System.Windows.Forms.Label();
		this.buttonChangePass = new System.Windows.Forms.Button();
		this.buttonEnablePass = new System.Windows.Forms.Button();
		this.statusBar = new System.Windows.Forms.StatusBar();
		this.labelStatus = new System.Windows.Forms.Label();
		this.buttonClose = new System.Windows.Forms.Button();
		this.buttonDisableSecurity = new System.Windows.Forms.Button();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).BeginInit();
		base.SuspendLayout();
		this.pictureBox1.Image = (System.Drawing.Image)resources.GetObject("pictureBox1.Image");
		this.pictureBox1.Location = new System.Drawing.Point(0, 0);
		this.pictureBox1.Name = "pictureBox1";
		this.pictureBox1.Size = new System.Drawing.Size(366, 64);
		this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
		this.pictureBox1.TabIndex = 0;
		this.pictureBox1.TabStop = false;
		this.textBoxPassword.CausesValidation = false;
		this.textBoxPassword.Location = new System.Drawing.Point(74, 85);
		this.textBoxPassword.Name = "textBoxPassword";
		this.textBoxPassword.PasswordChar = '*';
		this.textBoxPassword.Size = new System.Drawing.Size(280, 20);
		this.textBoxPassword.TabIndex = 1;
		this.textBoxConfirmPass.ImeMode = System.Windows.Forms.ImeMode.On;
		this.textBoxConfirmPass.Location = new System.Drawing.Point(74, 118);
		this.textBoxConfirmPass.Name = "textBoxConfirmPass";
		this.textBoxConfirmPass.PasswordChar = '*';
		this.textBoxConfirmPass.Size = new System.Drawing.Size(280, 20);
		this.textBoxConfirmPass.TabIndex = 2;
		this.lblOldPass.AutoSize = true;
		this.lblOldPass.Location = new System.Drawing.Point(12, 88);
		this.lblOldPass.Name = "lblOldPass";
		this.lblOldPass.Size = new System.Drawing.Size(56, 13);
		this.lblOldPass.TabIndex = 0;
		this.lblOldPass.Text = "&Password:";
		this.lblNewPass.AutoSize = true;
		this.lblNewPass.Location = new System.Drawing.Point(12, 121);
		this.lblNewPass.Name = "lblNewPass";
		this.lblNewPass.Size = new System.Drawing.Size(45, 13);
		this.lblNewPass.TabIndex = 2;
		this.lblNewPass.Text = "&Confirm:";
		this.buttonChangePass.CausesValidation = false;
		this.buttonChangePass.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.buttonChangePass.Location = new System.Drawing.Point(7, 176);
		this.buttonChangePass.Name = "buttonChangePass";
		this.buttonChangePass.Size = new System.Drawing.Size(124, 25);
		this.buttonChangePass.TabIndex = 5;
		this.buttonChangePass.Text = "C&hange password";
		this.buttonChangePass.Click += new System.EventHandler(buttonChangePass_Click);
		this.buttonEnablePass.CausesValidation = false;
		this.buttonEnablePass.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.buttonEnablePass.Location = new System.Drawing.Point(137, 176);
		this.buttonEnablePass.Name = "buttonEnablePass";
		this.buttonEnablePass.Size = new System.Drawing.Size(217, 25);
		this.buttonEnablePass.TabIndex = 4;
		this.buttonEnablePass.Text = "&Switch to WindowsGroup based security";
		this.buttonEnablePass.Click += new System.EventHandler(buttonEnablePass_Click);
		this.statusBar.Location = new System.Drawing.Point(0, 271);
		this.statusBar.Name = "statusBar";
		this.statusBar.Size = new System.Drawing.Size(366, 22);
		this.statusBar.TabIndex = 9;
		this.labelStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.labelStatus.ForeColor = System.Drawing.Color.FromArgb(192, 0, 0);
		this.labelStatus.Location = new System.Drawing.Point(52, 146);
		this.labelStatus.Name = "labelStatus";
		this.labelStatus.Size = new System.Drawing.Size(232, 23);
		this.labelStatus.TabIndex = 8;
		this.labelStatus.Text = "LOGIN FAILED!";
		this.labelStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
		this.labelStatus.Visible = false;
		this.buttonClose.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.buttonClose.Location = new System.Drawing.Point(253, 242);
		this.buttonClose.Name = "buttonClose";
		this.buttonClose.Size = new System.Drawing.Size(75, 23);
		this.buttonClose.TabIndex = 10;
		this.buttonClose.Text = "Close";
		this.buttonClose.UseVisualStyleBackColor = true;
		this.buttonDisableSecurity.Location = new System.Drawing.Point(7, 207);
		this.buttonDisableSecurity.Name = "buttonDisableSecurity";
		this.buttonDisableSecurity.Size = new System.Drawing.Size(124, 23);
		this.buttonDisableSecurity.TabIndex = 11;
		this.buttonDisableSecurity.Text = "&Turn off security";
		this.buttonDisableSecurity.UseVisualStyleBackColor = true;
		this.buttonDisableSecurity.Click += new System.EventHandler(buttonDisableSecurity_Click);
		base.AcceptButton = this.buttonChangePass;
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		base.CancelButton = this.buttonEnablePass;
		base.CausesValidation = false;
		base.ClientSize = new System.Drawing.Size(366, 293);
		base.Controls.Add(this.buttonDisableSecurity);
		base.Controls.Add(this.buttonClose);
		base.Controls.Add(this.labelStatus);
		base.Controls.Add(this.statusBar);
		base.Controls.Add(this.buttonEnablePass);
		base.Controls.Add(this.buttonChangePass);
		base.Controls.Add(this.lblNewPass);
		base.Controls.Add(this.lblOldPass);
		base.Controls.Add(this.textBoxConfirmPass);
		base.Controls.Add(this.textBoxPassword);
		base.Controls.Add(this.pictureBox1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		base.HelpButton = true;
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "ModifySettingsDialog";
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "Modify Security Settings";
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}

	private void buttonEnablePass_Click(object sender, EventArgs e)
	{
		ChangeStatusLabel(show: false, error: false, "");
		if (!QSecurity.IsEngineer)
		{
			return;
		}
		if (QSecurity.IsPasswordMode)
		{
			if (QSecurity.SwitchToWindowsGroupMode())
			{
				UpdateControls();
				ChangeStatusLabel(show: true, error: false, "WindowsGroup based security enabled");
			}
			else
			{
				ChangeStatusLabel(show: true, error: true, "Unable to switch security mode");
				base.DialogResult = DialogResult.None;
			}
		}
		else if (textBoxPassword.Text.Trim().Length > 0)
		{
			if (textBoxPassword.Text == textBoxConfirmPass.Text)
			{
				if (QSecurity.SwitchToPasswordMode(textBoxPassword.Text, textBoxConfirmPass.Text))
				{
					UpdateControls();
					ChangeStatusLabel(show: true, error: false, "Password based security enabled");
				}
				else
				{
					ChangeStatusLabel(show: true, error: true, "Failed to set password");
					base.DialogResult = DialogResult.None;
				}
			}
			else
			{
				ChangeStatusLabel(show: true, error: true, "Password did not match");
				base.DialogResult = DialogResult.None;
			}
		}
		else
		{
			ChangeStatusLabel(show: true, error: true, "Password cannot be blank");
			base.DialogResult = DialogResult.None;
		}
	}

	private void ChangeStatusLabel(bool show, bool error, string message)
	{
		if (show)
		{
			labelStatus.Visible = true;
			if (error)
			{
				labelStatus.ForeColor = Color.Red;
			}
			else
			{
				labelStatus.ForeColor = Color.Navy;
			}
			labelStatus.Text = message;
		}
		else
		{
			labelStatus.Visible = false;
		}
	}

	private void buttonChangePass_Click(object sender, EventArgs e)
	{
		ChangeStatusLabel(show: false, error: false, "");
		if (QSecurity.IsEngineer && QSecurity.IsPasswordMode)
		{
			PasswordLoginScreen passwordLoginScreen = new PasswordLoginScreen(changingPassword: true);
			if (passwordLoginScreen.ShowDialog() == DialogResult.OK)
			{
				ChangeStatusLabel(show: true, error: false, "Password updated");
			}
			else
			{
				base.DialogResult = DialogResult.None;
			}
		}
		else
		{
			ChangeStatusLabel(show: true, error: true, "Unable to change password");
			base.DialogResult = DialogResult.None;
		}
	}

	private void buttonDisableSecurity_Click(object sender, EventArgs e)
	{
		if (QSecurity.UserAccessLevel != AccessLevel.Engineer)
		{
			return;
		}
		if (QSecurity.IsSecurityDisabled)
		{
			if (MessageBox.Show("Turning on security will close the dialog and revert to previous privilege level,\n do you wish to continue?", "Security settings", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				QSecurity.EnableSecurity(enable: true);
				Close();
			}
		}
		else
		{
			QSecurity.EnableSecurity(enable: false);
		}
		UpdateControls();
	}

	private void UpdateControls()
	{
		if (QSecurity.IsPasswordMode)
		{
			buttonEnablePass.Text = "&Switch to WindowsGroup based security";
		}
		else
		{
			buttonEnablePass.Text = "&Switch to Password based security";
		}
		if (QSecurity.IsSecurityDisabled)
		{
			buttonDisableSecurity.Text = "&Turn on security";
		}
		else
		{
			buttonDisableSecurity.Text = "&Turn off security";
		}
		if (QSecurity.IsSecurityDisabled)
		{
			buttonEnablePass.Enabled = false;
			textBoxPassword.Enabled = false;
			textBoxConfirmPass.Enabled = false;
			buttonChangePass.Enabled = false;
			buttonDisableSecurity.Enabled = true;
		}
		else if (!QSecurity.IsEngineer)
		{
			textBoxPassword.Enabled = false;
			textBoxConfirmPass.Enabled = false;
			buttonEnablePass.Enabled = false;
			buttonChangePass.Enabled = false;
			buttonDisableSecurity.Enabled = false;
		}
		else if (QSecurity.IsPasswordMode)
		{
			textBoxPassword.Enabled = false;
			textBoxConfirmPass.Enabled = false;
			buttonChangePass.Enabled = true;
			buttonEnablePass.Enabled = true;
			buttonDisableSecurity.Enabled = true;
		}
		else
		{
			textBoxPassword.Enabled = true;
			textBoxConfirmPass.Enabled = true;
			buttonChangePass.Enabled = false;
			buttonEnablePass.Enabled = true;
			buttonDisableSecurity.Enabled = true;
		}
	}
}
