using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.QDM.Libraries.DBAccessLibrary;
using QC.Tools.QTMDatabaseService;

namespace QC.Tools;

public class DatabaseOptions : Form
{
	private string serverName = "";

	private string databaseName = "";

	private string serverDescription = "";

	private string databaseDescription = "";

	private string userName = "";

	private string password = "";

	private QC.Tools.QTMDatabaseService.QTMDatabaseService dBService;

	private string sqlConnectionString = string.Empty;

	private bool qtm;

	private IContainer components;

	private TextBox txtServer;

	private TextBox txtDatabase;

	private Label serverLabel;

	private Label nameLabel;

	private Button btnTestConnection;

	private Button btnCancel;

	private Button btnOK;

	private Button btnServers;

	private Button btnDatabases;

	private Label DatabaseTimeoutLabel;

	private TextBox txtDatabaseConnectionTimeout;

	private TextBox txtCommandTimeout;

	private Label CommandTimeoutlabel;

	public string ServerName
	{
		get
		{
			return serverName;
		}
		set
		{
			serverName = value;
			txtServer.Text = value;
		}
	}

	public string ServerDescription
	{
		get
		{
			return serverDescription;
		}
		set
		{
			serverDescription = value;
		}
	}

	public string DatabaseName
	{
		get
		{
			return databaseName;
		}
		set
		{
			databaseName = value;
			txtDatabase.Text = value;
		}
	}

	public string DatabaseConnectionTimeout
	{
		get
		{
			return txtDatabaseConnectionTimeout.Text;
		}
		set
		{
			txtDatabaseConnectionTimeout.Text = value;
		}
	}

	public string CommandTimeout
	{
		get
		{
			return txtCommandTimeout.Text;
		}
		set
		{
			txtCommandTimeout.Text = value;
		}
	}

	public string DatabaseDescription
	{
		get
		{
			return databaseDescription;
		}
		set
		{
			databaseDescription = value;
		}
	}

	public string SqlConnectionString => sqlConnectionString;

	public bool QTMDB
	{
		get
		{
			return qtm;
		}
		set
		{
			qtm = value;
		}
	}

	public DatabaseOptions()
	{
		InitializeComponent();
	}

	public DatabaseOptions(string userName, string password)
	{
		InitializeComponent();
		this.userName = userName;
		this.password = password;
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		if (TestConnection(showDialog: false))
		{
			serverName = txtServer.Text.Trim();
			databaseName = txtDatabase.Text.Trim();
			base.DialogResult = DialogResult.OK;
			Close();
		}
	}

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

	private void btnServers_Click(object sender, EventArgs e)
	{
		ListDialog listDialog = new ListDialog();
		listDialog.Type = DialogType.ServerList;
		if (listDialog.ShowDialog() == DialogResult.OK && txtServer.Text.Trim().CompareTo(listDialog.SelectedName) != 0)
		{
			txtServer.Text = listDialog.SelectedName;
			serverDescription = listDialog.SelectedDescription;
			txtDatabase.Text = "";
			SetDefaultDatabaseName();
		}
	}

	private void DBService_GetDatabaseListCompleted(object sender, GetDatabaseListCompletedEventArgs e)
	{
		try
		{
			string[] result = e.Result;
			if (result.Length > 0)
			{
				string[] array = result[0].Split(new string[1] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
				txtDatabase.Text = array[0];
				databaseDescription = array[1];
			}
		}
		catch
		{
		}
	}

	private void btnDatabases_Click(object sender, EventArgs e)
	{
		ListDialog listDialog = new ListDialog();
		listDialog.ServerName = txtServer.Text;
		listDialog.Type = DialogType.DatabaseList;
		if (listDialog.ShowDialog() == DialogResult.OK)
		{
			txtDatabase.Text = listDialog.SelectedName;
			databaseDescription = listDialog.SelectedDescription;
		}
	}

	private void btnTestConnection_Click(object sender, EventArgs e)
	{
		btnOK.Enabled = false;
		if (TestConnection(showDialog: true))
		{
			btnOK.Enabled = true;
		}
	}

	private void SetDefaultDatabaseName()
	{
		dBService = new QC.Tools.QTMDatabaseService.QTMDatabaseService();
		dBService.GetDatabaseListCompleted += DBService_GetDatabaseListCompleted;
		LoadDatabases();
	}

	private void LoadDatabases()
	{
		try
		{
			dBService.GetDatabaseListAsync(txtServer.Text);
		}
		catch
		{
		}
	}

	private bool TestConnection(bool showDialog)
	{
		if (txtServer.Text.Trim().Length == 0)
		{
			MessageBox.Show("Enter Server Name");
			return false;
		}
		if (txtDatabase.Text.Trim().Length == 0)
		{
			MessageBox.Show("Enter Database Name");
			return false;
		}
		try
		{
			if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
			{
				DataAccessManager.TestConnection(txtServer.Text.Trim(), txtDatabase.Text.Trim(), userName, password);
			}
			else
			{
				DataAccessManager.TestConnection(txtServer.Text.Trim(), txtDatabase.Text.Trim());
			}
			if (showDialog)
			{
				MessageBox.Show(this, "Connection Test Passed", "Connection Test");
			}
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(this, ex.Message, "Connection Test - FAILED");
			return false;
		}
	}

	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.Tools.DatabaseOptions));
		this.txtServer = new System.Windows.Forms.TextBox();
		this.txtDatabase = new System.Windows.Forms.TextBox();
		this.serverLabel = new System.Windows.Forms.Label();
		this.nameLabel = new System.Windows.Forms.Label();
		this.btnTestConnection = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnServers = new System.Windows.Forms.Button();
		this.btnDatabases = new System.Windows.Forms.Button();
		this.DatabaseTimeoutLabel = new System.Windows.Forms.Label();
		this.txtDatabaseConnectionTimeout = new System.Windows.Forms.TextBox();
		this.txtCommandTimeout = new System.Windows.Forms.TextBox();
		this.CommandTimeoutlabel = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.txtServer.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
		this.txtServer.Location = new System.Drawing.Point(12, 40);
		this.txtServer.Name = "txtServer";
		this.txtServer.Size = new System.Drawing.Size(365, 20);
		this.txtServer.TabIndex = 0;
		this.txtDatabase.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
		this.txtDatabase.Location = new System.Drawing.Point(12, 86);
		this.txtDatabase.Name = "txtDatabase";
		this.txtDatabase.Size = new System.Drawing.Size(365, 20);
		this.txtDatabase.TabIndex = 2;
		this.serverLabel.AutoSize = true;
		this.serverLabel.Location = new System.Drawing.Point(12, 20);
		this.serverLabel.Name = "serverLabel";
		this.serverLabel.Size = new System.Drawing.Size(90, 13);
		this.serverLabel.TabIndex = 2;
		this.serverLabel.Text = "Database Server:";
		this.nameLabel.AutoSize = true;
		this.nameLabel.Location = new System.Drawing.Point(12, 67);
		this.nameLabel.Name = "nameLabel";
		this.nameLabel.Size = new System.Drawing.Size(87, 13);
		this.nameLabel.TabIndex = 3;
		this.nameLabel.Text = "Database Name:";
		this.btnTestConnection.Location = new System.Drawing.Point(12, 212);
		this.btnTestConnection.Name = "btnTestConnection";
		this.btnTestConnection.Size = new System.Drawing.Size(110, 23);
		this.btnTestConnection.TabIndex = 4;
		this.btnTestConnection.Text = "Test Connection";
		this.btnTestConnection.UseVisualStyleBackColor = true;
		this.btnTestConnection.Click += new System.EventHandler(btnTestConnection_Click);
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(142, 212);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(110, 23);
		this.btnCancel.TabIndex = 5;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnOK.Location = new System.Drawing.Point(272, 212);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(105, 23);
		this.btnOK.TabIndex = 6;
		this.btnOK.Text = "OK";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.btnServers.Location = new System.Drawing.Point(383, 40);
		this.btnServers.Name = "btnServers";
		this.btnServers.Size = new System.Drawing.Size(23, 20);
		this.btnServers.TabIndex = 1;
		this.btnServers.Text = "..";
		this.btnServers.UseVisualStyleBackColor = true;
		this.btnServers.Click += new System.EventHandler(btnServers_Click);
		this.btnDatabases.Location = new System.Drawing.Point(383, 86);
		this.btnDatabases.Name = "btnDatabases";
		this.btnDatabases.Size = new System.Drawing.Size(23, 20);
		this.btnDatabases.TabIndex = 3;
		this.btnDatabases.Text = "..";
		this.btnDatabases.UseVisualStyleBackColor = true;
		this.btnDatabases.Click += new System.EventHandler(btnDatabases_Click);
		this.DatabaseTimeoutLabel.AutoSize = true;
		this.DatabaseTimeoutLabel.Location = new System.Drawing.Point(12, 114);
		this.DatabaseTimeoutLabel.Name = "DatabaseTimeoutLabel";
		this.DatabaseTimeoutLabel.Size = new System.Drawing.Size(151, 13);
		this.DatabaseTimeoutLabel.TabIndex = 7;
		this.DatabaseTimeoutLabel.Text = "Database Connection Timeout";
		this.txtDatabaseConnectionTimeout.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
		this.txtDatabaseConnectionTimeout.Location = new System.Drawing.Point(12, 132);
		this.txtDatabaseConnectionTimeout.Name = "txtDatabaseConnectionTimeout";
		this.txtDatabaseConnectionTimeout.Size = new System.Drawing.Size(365, 20);
		this.txtDatabaseConnectionTimeout.TabIndex = 8;
		this.txtCommandTimeout.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
		this.txtCommandTimeout.Location = new System.Drawing.Point(12, 178);
		this.txtCommandTimeout.Name = "txtCommandTimeout";
		this.txtCommandTimeout.Size = new System.Drawing.Size(365, 20);
		this.txtCommandTimeout.TabIndex = 10;
		this.CommandTimeoutlabel.AutoSize = true;
		this.CommandTimeoutlabel.Location = new System.Drawing.Point(12, 158);
		this.CommandTimeoutlabel.Name = "CommandTimeoutlabel";
		this.CommandTimeoutlabel.Size = new System.Drawing.Size(95, 13);
		this.CommandTimeoutlabel.TabIndex = 9;
		this.CommandTimeoutlabel.Text = "Command Timeout";
		base.AcceptButton = this.btnOK;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.BackColor = System.Drawing.Color.White;
		base.CancelButton = this.btnCancel;
		base.ClientSize = new System.Drawing.Size(420, 243);
		base.Controls.Add(this.txtCommandTimeout);
		base.Controls.Add(this.CommandTimeoutlabel);
		base.Controls.Add(this.txtDatabaseConnectionTimeout);
		base.Controls.Add(this.DatabaseTimeoutLabel);
		base.Controls.Add(this.btnDatabases);
		base.Controls.Add(this.btnServers);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnTestConnection);
		base.Controls.Add(this.nameLabel);
		base.Controls.Add(this.serverLabel);
		base.Controls.Add(this.txtDatabase);
		base.Controls.Add(this.txtServer);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "DatabaseOptions";
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Database Options";
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
