using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using QC.Tools.QTMDatabaseService;

namespace QC.Tools;

public class ListDialog : Form
{
	private delegate void SetDataSourceDelegate(string[] list);

	private IContainer components;

	private ListBox listbox;

	private Button btnCancel;

	private Button btnSelect;

	private Label lblInstruction;

	private StatusStrip statusStrip1;

	private ToolStripStatusLabel lblItemName;

	private ToolStripStatusLabel lblStatus;

	private string serverName = "";

	private string selectedName = "";

	private string selectedDescription = "";

	private string[] names = new string[1] { "" };

	private string[] descriptions = new string[1] { "" };

	private DialogType dialogType;

	private QC.Tools.QTMDatabaseService.QTMDatabaseService DBService;

	private bool IsWebserviceAvailable = true;

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

	public string SelectedName
	{
		get
		{
			return selectedName;
		}
		set
		{
			selectedName = value;
		}
	}

	public string SelectedDescription
	{
		get
		{
			return selectedDescription;
		}
		set
		{
			selectedDescription = value;
		}
	}

	public DialogType Type
	{
		get
		{
			return dialogType;
		}
		set
		{
			dialogType = value;
			if (value == DialogType.ServerList)
			{
				Text = "Server List";
			}
			else
			{
				Text = "Database List";
			}
		}
	}

	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.ListDialog));
		this.listbox = new System.Windows.Forms.ListBox();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnSelect = new System.Windows.Forms.Button();
		this.lblInstruction = new System.Windows.Forms.Label();
		this.statusStrip1 = new System.Windows.Forms.StatusStrip();
		this.lblItemName = new System.Windows.Forms.ToolStripStatusLabel();
		this.lblStatus = new System.Windows.Forms.ToolStripStatusLabel();
		this.statusStrip1.SuspendLayout();
		base.SuspendLayout();
		this.listbox.FormattingEnabled = true;
		this.listbox.Location = new System.Drawing.Point(12, 12);
		this.listbox.Name = "listbox";
		this.listbox.Size = new System.Drawing.Size(342, 186);
		this.listbox.TabIndex = 0;
		this.listbox.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(listbox_MouseDoubleClick);
		this.listbox.SelectedIndexChanged += new System.EventHandler(listbox_SelectedIndexChanged);
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(198, 206);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 1;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnSelect.Enabled = false;
		this.btnSelect.Location = new System.Drawing.Point(279, 206);
		this.btnSelect.Name = "btnSelect";
		this.btnSelect.Size = new System.Drawing.Size(75, 23);
		this.btnSelect.TabIndex = 2;
		this.btnSelect.Text = "Select";
		this.btnSelect.UseVisualStyleBackColor = true;
		this.btnSelect.Click += new System.EventHandler(btnSelect_Click);
		this.lblInstruction.AutoSize = true;
		this.lblInstruction.Location = new System.Drawing.Point(12, 211);
		this.lblInstruction.Name = "lblInstruction";
		this.lblInstruction.Size = new System.Drawing.Size(180, 13);
		this.lblInstruction.TabIndex = 3;
		this.lblInstruction.Text = "Double click in the list or click Select";
		this.statusStrip1.BackColor = System.Drawing.Color.White;
		this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[2] { this.lblItemName, this.lblStatus });
		this.statusStrip1.Location = new System.Drawing.Point(0, 241);
		this.statusStrip1.Name = "statusStrip1";
		this.statusStrip1.Size = new System.Drawing.Size(366, 22);
		this.statusStrip1.TabIndex = 4;
		this.statusStrip1.Text = "statusStrip1";
		this.lblItemName.BorderSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.Right;
		this.lblItemName.Name = "lblItemName";
		this.lblItemName.Size = new System.Drawing.Size(351, 17);
		this.lblItemName.Spring = true;
		this.lblItemName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.lblStatus.Name = "lblStatus";
		this.lblStatus.Size = new System.Drawing.Size(0, 17);
		base.AcceptButton = this.btnSelect;
		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(366, 263);
		base.Controls.Add(this.statusStrip1);
		base.Controls.Add(this.lblInstruction);
		base.Controls.Add(this.btnSelect);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.listbox);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "ListDialog";
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "List Dialog";
		base.Load += new System.EventHandler(ListDialog_Load);
		this.statusStrip1.ResumeLayout(false);
		this.statusStrip1.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}

	public ListDialog()
	{
		InitializeComponent();
		try
		{
			DBService = new QC.Tools.QTMDatabaseService.QTMDatabaseService();
			DBService.GetServerListCompleted += DBService_GetServerListCompleted;
			DBService.GetDatabaseListCompleted += DBService_GetDatabaseListCompleted;
		}
		catch
		{
		}
	}

	private void ListDialog_Load(object sender, EventArgs e)
	{
		if (Type == DialogType.ServerList)
		{
			Thread thread = new Thread(LoadServers);
			thread.Start();
		}
		else
		{
			Thread thread2 = new Thread(LoadDatabases);
			thread2.Start();
		}
	}

	private void LoadServers()
	{
		try
		{
			DBService.GetServerListAsync();
		}
		catch
		{
		}
	}

	private void LoadDatabases()
	{
		try
		{
			DBService.GetDatabaseListAsync(serverName);
		}
		catch
		{
		}
	}

	private void DBService_GetServerListCompleted(object sender, GetServerListCompletedEventArgs e)
	{
		try
		{
			string[] result = e.Result;
			SetDataSourceDelegate method = SetListBoxDataSource;
			Invoke(method, new object[1] { result });
		}
		catch
		{
			IsWebserviceAvailable = false;
			SetDataSourceDelegate method2 = SetListBoxDataSource;
			Invoke(method2, new object[1] { GetServerList() });
		}
	}

	private string[] GetServerList()
	{
		try
		{
			string text = "";
			return text.Split(new string[1] { "##" }, StringSplitOptions.RemoveEmptyEntries);
		}
		catch
		{
		}
		return null;
	}

	private void DBService_GetDatabaseListCompleted(object sender, GetDatabaseListCompletedEventArgs e)
	{
		try
		{
			string[] result = e.Result;
			SetDataSourceDelegate method = SetListBoxDataSource;
			Invoke(method, new object[1] { result });
		}
		catch
		{
			IsWebserviceAvailable = false;
			SetDataSourceDelegate method2 = SetListBoxDataSource;
			Invoke(method2, new object[1] { GetDatabaseList(serverName) });
		}
	}

	private string[] GetDatabaseList(string serverName)
	{
		try
		{
			string text = "";
			switch (serverName.ToUpper())
			{
			case "CORPTESTSQL2":
				text = "QTMDB_CORP@@Database for collecting CORP data##QTMDB_IPS@@Database for collecting IPS data##QTMDB_SHIVTA@@Database for collecting SHIVTA data";
				break;
			case "QWBS-TE":
				text = "QTMDB_OMNI@@Database for collecting OMNI data";
				break;
			case "QMT-SQL":
				text = "QTMDB_PVI@@Database for collecting QMT-PVI data";
				break;
			case "QMT-ENG-SQL":
				text = "QTMDB_SDE@@Database for collecting QMT San Diego data##QTMDB_SJE@@Database for collecting QMT San Jose data";
				break;
			case "172.16.96.68\\SQLEXPRESS":
				text = "QTMDB_PVI@@Database for collecting QMT-PVI data";
				break;
			case "WUXDC01":
				text = "QTMDB_OMNI@@Database for collecting OMNI data in WUXI";
				break;
			}
			return text.Split(new string[1] { "##" }, StringSplitOptions.RemoveEmptyEntries);
		}
		catch
		{
		}
		return null;
	}

	private void SetListBoxDataSource(string[] list)
	{
		if (list != null)
		{
			names = new string[list.Length];
			descriptions = new string[list.Length];
			for (int i = 0; i < list.Length; i++)
			{
				string[] array = list[i].Split(new string[1] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
				listbox.Items.Add(array[1]);
				names[i] = array[0];
				descriptions[i] = array[1];
			}
			if (IsWebserviceAvailable)
			{
				lblStatus.Text = list.Length + " available.";
				lblStatus.ForeColor = Color.Black;
			}
			else
			{
				MessageBox.Show("Failed to get a list of database servers, please type a server name and database name manually.");
				Close();
			}
		}
		else
		{
			MessageBox.Show("Failed to get a list of databases, please type a server name and database name manually.");
			Close();
		}
	}

	private void listbox_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (listbox.SelectedIndex > -1)
		{
			btnSelect.Enabled = true;
			lblItemName.Text = names[listbox.SelectedIndex];
		}
	}

	private void btnSelect_Click(object sender, EventArgs e)
	{
		selectedName = names[listbox.SelectedIndex];
		selectedDescription = descriptions[listbox.SelectedIndex];
		base.DialogResult = DialogResult.OK;
		Close();
	}

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

	private void listbox_MouseDoubleClick(object sender, MouseEventArgs e)
	{
		if (e.Button == MouseButtons.Left && listbox.IndexFromPoint(e.Location) > -1)
		{
			btnSelect_Click(sender, e);
		}
	}
}
