using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.QsprShared.Config;
using QC.QsprShared.Dialogs;
using QC.QTMSecurity;

namespace QC.QTMApplication;

public class DllPaths : Form
{
	private const string UPDATE_DLL_PATH_TOOL_TIP = "If checked, the original DLL path(s) will be updated";

	private bool modifiedFlag;

	private WorkspaceConfig WConfig;

	private IContainer components;

	private CheckBox checkBoxUseDllPath;

	private ListBox listBoxSearchPaths;

	private Button buttonAddPath;

	private Button buttonPathDown;

	private Button buttonPathUp;

	private Button buttonRemovePath;

	private Button buttonOK;

	private Button buttonCancel;

	private Label label1;

	private FolderBrowserDialog folderBrowserSearchPath;

	private Button btnHelp;

	private GroupBox groupBox1;

	private Label label3;

	public bool TreeChanged
	{
		get
		{
			return modifiedFlag;
		}
		set
		{
			modifiedFlag = value;
		}
	}

	public DllPaths(WorkspaceConfig wConfig)
	{
		WConfig = wConfig;
		InitializeComponent();
		modifiedFlag = false;
		if (WConfig.DLLPaths.Count > 0)
		{
			listBoxSearchPaths.Items.AddRange(WConfig.DLLPaths.ToArray());
		}
		checkBoxUseDllPath.Checked = WConfig.UseDllSearch;
		if (!QSecurity.IsEngineer)
		{
			SetUserAccessLevel(isEnabled: false);
		}
		else
		{
			SetUserAccessLevel(isEnabled: true);
		}
	}

	private void SetUserAccessLevel(bool isEnabled)
	{
		checkBoxUseDllPath.Enabled = isEnabled;
		buttonAddPath.Enabled = isEnabled;
		buttonRemovePath.Enabled = isEnabled;
		buttonPathUp.Enabled = isEnabled;
		buttonPathDown.Enabled = isEnabled;
		buttonOK.Enabled = isEnabled;
	}

	private void InitializeToolTip(ToolTip toolTip)
	{
		toolTip.AutoPopDelay = 5000;
		toolTip.InitialDelay = 1000;
		toolTip.ReshowDelay = 500;
		toolTip.ShowAlways = true;
	}

	private void buttonAddPath_Click(object sender, EventArgs e)
	{
		modifiedFlag = true;
		if (folderBrowserSearchPath.ShowDialog() == DialogResult.OK)
		{
			if (listBoxSearchPaths.Items.Contains(folderBrowserSearchPath.SelectedPath))
			{
				AppMessageDialog.Show($"Path {folderBrowserSearchPath.SelectedPath} is already added.", DialogResult.OK, MessageDialogType.UIWarning);
			}
			else
			{
				listBoxSearchPaths.Items.Add(folderBrowserSearchPath.SelectedPath);
			}
		}
	}

	private void buttonRemovePath_Click(object sender, EventArgs e)
	{
		modifiedFlag = true;
		if (listBoxSearchPaths.SelectedItems.Count > 0)
		{
			while (listBoxSearchPaths.SelectedItems.Count > 0)
			{
				listBoxSearchPaths.Items.Remove(listBoxSearchPaths.SelectedItem);
			}
		}
	}

	private void buttonPathUp_Click(object sender, EventArgs e)
	{
		modifiedFlag = true;
		if (listBoxSearchPaths.SelectedItems.Count > 0)
		{
			int selectedIndex = listBoxSearchPaths.SelectedIndex;
			string item = listBoxSearchPaths.SelectedItem.ToString();
			int num = ((selectedIndex - 1 > 0) ? (selectedIndex - 1) : 0);
			listBoxSearchPaths.Items.RemoveAt(selectedIndex);
			listBoxSearchPaths.Items.Insert(num, item);
			listBoxSearchPaths.SelectedIndex = num;
		}
	}

	private void buttonPathDown_Click(object sender, EventArgs e)
	{
		modifiedFlag = true;
		if (listBoxSearchPaths.SelectedItems.Count > 0)
		{
			int selectedIndex = listBoxSearchPaths.SelectedIndex;
			string item = listBoxSearchPaths.SelectedItem.ToString();
			int num = listBoxSearchPaths.Items.Count - 1;
			int num2 = ((selectedIndex + 1 >= num) ? num : (selectedIndex + 1));
			listBoxSearchPaths.Items.RemoveAt(selectedIndex);
			listBoxSearchPaths.Items.Insert(num2, item);
			listBoxSearchPaths.SelectedIndex = num2;
		}
	}

	private void buttonCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	private void buttonOK_Click(object sender, EventArgs e)
	{
		try
		{
			if (modifiedFlag)
			{
				string[] array = new string[listBoxSearchPaths.Items.Count];
				listBoxSearchPaths.Items.CopyTo(array, 0);
				if (array != null && array.Length > 0)
				{
					WConfig.DLLPaths = new List<string>(array);
				}
				WConfig.UseDllSearch = checkBoxUseDllPath.Checked;
			}
			base.DialogResult = DialogResult.OK;
			Close();
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show("Failed to save the changes. Error: " + ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void checkBoxUseDllPath_CheckedChanged(object sender, EventArgs e)
	{
		modifiedFlag = true;
	}

	private void btnHelp_Click(object sender, EventArgs e)
	{
		DLLPathsHelp dLLPathsHelp = new DLLPathsHelp();
		dLLPathsHelp.ShowDialog();
	}

	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.QTMApplication.DllPaths));
		this.checkBoxUseDllPath = new System.Windows.Forms.CheckBox();
		this.listBoxSearchPaths = new System.Windows.Forms.ListBox();
		this.buttonAddPath = new System.Windows.Forms.Button();
		this.buttonPathDown = new System.Windows.Forms.Button();
		this.buttonPathUp = new System.Windows.Forms.Button();
		this.buttonRemovePath = new System.Windows.Forms.Button();
		this.buttonOK = new System.Windows.Forms.Button();
		this.buttonCancel = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.folderBrowserSearchPath = new System.Windows.Forms.FolderBrowserDialog();
		this.btnHelp = new System.Windows.Forms.Button();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.label3 = new System.Windows.Forms.Label();
		this.groupBox1.SuspendLayout();
		base.SuspendLayout();
		this.checkBoxUseDllPath.AutoSize = true;
		this.checkBoxUseDllPath.Location = new System.Drawing.Point(13, 12);
		this.checkBoxUseDllPath.Name = "checkBoxUseDllPath";
		this.checkBoxUseDllPath.Size = new System.Drawing.Size(141, 17);
		this.checkBoxUseDllPath.TabIndex = 0;
		this.checkBoxUseDllPath.Text = "Use DLL Search Path(s)";
		this.checkBoxUseDllPath.UseVisualStyleBackColor = true;
		this.checkBoxUseDllPath.CheckedChanged += new System.EventHandler(checkBoxUseDllPath_CheckedChanged);
		this.listBoxSearchPaths.FormattingEnabled = true;
		this.listBoxSearchPaths.Location = new System.Drawing.Point(12, 156);
		this.listBoxSearchPaths.Name = "listBoxSearchPaths";
		this.listBoxSearchPaths.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
		this.listBoxSearchPaths.Size = new System.Drawing.Size(461, 147);
		this.listBoxSearchPaths.TabIndex = 1;
		this.buttonAddPath.Location = new System.Drawing.Point(215, 124);
		this.buttonAddPath.Name = "buttonAddPath";
		this.buttonAddPath.Size = new System.Drawing.Size(60, 23);
		this.buttonAddPath.TabIndex = 2;
		this.buttonAddPath.Text = "&Add";
		this.buttonAddPath.UseVisualStyleBackColor = true;
		this.buttonAddPath.Click += new System.EventHandler(buttonAddPath_Click);
		this.buttonPathDown.Location = new System.Drawing.Point(413, 124);
		this.buttonPathDown.Name = "buttonPathDown";
		this.buttonPathDown.Size = new System.Drawing.Size(60, 23);
		this.buttonPathDown.TabIndex = 3;
		this.buttonPathDown.Text = "&Down";
		this.buttonPathDown.UseVisualStyleBackColor = true;
		this.buttonPathDown.Click += new System.EventHandler(buttonPathDown_Click);
		this.buttonPathUp.Location = new System.Drawing.Point(347, 124);
		this.buttonPathUp.Name = "buttonPathUp";
		this.buttonPathUp.Size = new System.Drawing.Size(60, 23);
		this.buttonPathUp.TabIndex = 4;
		this.buttonPathUp.Text = "&Up";
		this.buttonPathUp.UseVisualStyleBackColor = true;
		this.buttonPathUp.Click += new System.EventHandler(buttonPathUp_Click);
		this.buttonRemovePath.Location = new System.Drawing.Point(281, 124);
		this.buttonRemovePath.Name = "buttonRemovePath";
		this.buttonRemovePath.Size = new System.Drawing.Size(60, 23);
		this.buttonRemovePath.TabIndex = 5;
		this.buttonRemovePath.Text = "&Remove";
		this.buttonRemovePath.UseVisualStyleBackColor = true;
		this.buttonRemovePath.Click += new System.EventHandler(buttonRemovePath_Click);
		this.buttonOK.Location = new System.Drawing.Point(330, 309);
		this.buttonOK.Name = "buttonOK";
		this.buttonOK.Size = new System.Drawing.Size(60, 23);
		this.buttonOK.TabIndex = 7;
		this.buttonOK.Text = "OK";
		this.buttonOK.UseVisualStyleBackColor = true;
		this.buttonOK.Click += new System.EventHandler(buttonOK_Click);
		this.buttonCancel.Location = new System.Drawing.Point(396, 309);
		this.buttonCancel.Name = "buttonCancel";
		this.buttonCancel.Size = new System.Drawing.Size(60, 23);
		this.buttonCancel.TabIndex = 6;
		this.buttonCancel.Text = "Cancel";
		this.buttonCancel.UseVisualStyleBackColor = true;
		this.buttonCancel.Click += new System.EventHandler(buttonCancel_Click);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(10, 137);
		this.label1.Margin = new System.Windows.Forms.Padding(0, 0, 3, 0);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(40, 13);
		this.label1.TabIndex = 8;
		this.label1.Text = "Path(s)";
		this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.folderBrowserSearchPath.RootFolder = System.Environment.SpecialFolder.MyComputer;
		this.btnHelp.Location = new System.Drawing.Point(12, 309);
		this.btnHelp.Name = "btnHelp";
		this.btnHelp.Size = new System.Drawing.Size(60, 23);
		this.btnHelp.TabIndex = 9;
		this.btnHelp.Text = "Help";
		this.btnHelp.UseVisualStyleBackColor = true;
		this.btnHelp.Click += new System.EventHandler(btnHelp_Click);
		this.groupBox1.Controls.Add(this.label3);
		this.groupBox1.Location = new System.Drawing.Point(12, 35);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(461, 72);
		this.groupBox1.TabIndex = 11;
		this.groupBox1.TabStop = false;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(6, 16);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(323, 52);
		this.label3.TabIndex = 11;
		this.label3.Text = resources.GetString("label3.Text");
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(492, 343);
		base.Controls.Add(this.groupBox1);
		base.Controls.Add(this.btnHelp);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.buttonOK);
		base.Controls.Add(this.buttonCancel);
		base.Controls.Add(this.buttonRemovePath);
		base.Controls.Add(this.buttonPathUp);
		base.Controls.Add(this.buttonPathDown);
		base.Controls.Add(this.buttonAddPath);
		base.Controls.Add(this.listBoxSearchPaths);
		base.Controls.Add(this.checkBoxUseDllPath);
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "DllPaths";
		base.ShowIcon = false;
		this.Text = "DLL Search Path(s)";
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
