using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace QC.SmartMatchApp;

public class PopupForm : Form
{
	private struct TreeList
	{
		public string Tech;

		public List<string> TreeNames;
	}

	private const int Separation = 27;

	private const int InitialX = 43;

	private const int InitialY = 12;

	private List<TreeList> TreesSortedByTech = new List<TreeList>();

	private int CurrentY = 12;

	private int CurrentTabIndex = 100;

	private string tempPath = "C:\\Program Files (x86)\\QUALCOMM\\QDART\\XTT\\SubSysRFCalibration\\SmartQQMatch\\MasterTree\\";

	private string xttPath = "";

	private string selectTech = "";

	private string selectTreeName;

	private IContainer components;

	private Button btnOK;

	private Button btnCancel;

	private GroupBox groupBox1;

	private Panel panel1;

	private Button btnBrowse;

	public string XttPath => xttPath;

	public string Tech
	{
		get
		{
			return selectTech;
		}
		set
		{
			selectTech = value;
		}
	}

	public string TreeName
	{
		get
		{
			return selectTreeName;
		}
		set
		{
			selectTreeName = value;
		}
	}

	public PopupForm(string xttDirectory, MruFiles mruFiles, string currentTech, string currentTree)
	{
		TreesSortedByTech.Clear();
		List<FileInfo> list = new List<FileInfo>();
		string[] array = mruFiles.Files.ToArray();
		foreach (string text in array)
		{
			if (File.Exists(text))
			{
				FileInfo item = new FileInfo(text);
				if (!list.Contains(item))
				{
					list.Add(item);
				}
			}
		}
		string text2 = "SmartQMatch";
		DirectoryInfo directoryInfo = new DirectoryInfo(xttDirectory);
		FileInfo[] files = directoryInfo.GetFiles("*" + text2 + "*.xtt");
		FileInfo[] array2 = files;
		foreach (FileInfo fileInfo in array2)
		{
			bool flag = false;
			if (!fileInfo.Name.Contains("MasterTree"))
			{
				continue;
			}
			for (int k = 0; k < list.Count; k++)
			{
				if (list[k].Name.Contains(fileInfo.Name))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				list.Add(new FileInfo(fileInfo.FullName));
			}
			if (!mruFiles.Files.Contains(fileInfo.FullName))
			{
				mruFiles.AddFileName(fileInfo.FullName);
			}
		}
		string text3 = "";
		string[] separator = new string[4] { "__", "-", "_", "." };
		if (list.Count == 0)
		{
			BrowseForXTTs(list);
			currentTree = list[0].ToString();
			string[] array3 = list[0].Name.Split(separator, StringSplitOptions.None);
			if (array3.Count() > 1)
			{
				string[] array4 = array3;
				foreach (string text4 in array4)
				{
					if (text4.ToUpper() == "LTE" || text4.ToUpper() == "WCDMA" || text4.ToUpper() == "GSM")
					{
						text3 = text4.ToUpper();
						break;
					}
				}
			}
		}
		for (int m = 0; m < list.Count; m++)
		{
			string[] array3 = list[m].Name.Split(separator, StringSplitOptions.None);
			if (array3.Count() > 1)
			{
				string[] array5 = array3;
				foreach (string text5 in array5)
				{
					if (text5.ToUpper() == "LTE" || text5.ToUpper() == "WCDMA" || text5.ToUpper() == "GSM")
					{
						text3 = text5.ToUpper();
						break;
					}
				}
			}
			int num = -1;
			for (int num2 = 0; num2 < TreesSortedByTech.Count; num2++)
			{
				if (TreesSortedByTech[num2].Tech == text3)
				{
					num = num2;
					break;
				}
			}
			if (num != -1)
			{
				TreesSortedByTech[num].TreeNames.Add(list[m].ToString());
				continue;
			}
			TreeList item2 = default(TreeList);
			item2.Tech = text3;
			item2.TreeNames = new List<string>();
			item2.TreeNames.Add(list[m].ToString());
			TreesSortedByTech.Add(item2);
		}
		InitializeComponent();
		for (int num3 = 0; num3 < TreesSortedByTech.Count; num3++)
		{
			Label value = new Label
			{
				AutoSize = true,
				Location = new Point(43, CurrentY),
				Name = "label" + TreesSortedByTech[num3].Tech,
				TabIndex = CurrentTabIndex,
				Text = TreesSortedByTech[num3].Tech
			};
			panel1.Controls.Add(value);
			CurrentY += 27;
			CurrentTabIndex++;
			for (int num4 = 0; num4 < TreesSortedByTech[num3].TreeNames.Count; num4++)
			{
				RadioButton radioButton = new RadioButton
				{
					AutoSize = true,
					Location = new Point(70, CurrentY),
					Name = TreesSortedByTech[num3].Tech + "_" + num4,
					Text = TreesSortedByTech[num3].TreeNames[num4],
					UseVisualStyleBackColor = true,
					TabIndex = CurrentTabIndex
				};
				if (currentTree == TreesSortedByTech[num3].TreeNames[num4])
				{
					radioButton.Checked = true;
				}
				else
				{
					radioButton.Checked = false;
				}
				radioButton.CheckedChanged += radioButton_CheckedChanged;
				panel1.Controls.Add(radioButton);
				CurrentY += 27;
				CurrentTabIndex++;
			}
		}
		Tech = currentTech;
		TreeName = currentTree;
	}

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

	private void btnCancel_Click(object sender, EventArgs e)
	{
	}

	private void radioButton_CheckedChanged(object sender, EventArgs e)
	{
		RadioButton radioButton = (RadioButton)sender;
		if (radioButton.Checked)
		{
			string[] array = radioButton.Name.Split('_');
			Tech = array[0];
			TreeName = Path.GetFileName(radioButton.Text);
			xttPath = radioButton.Text;
			btnOK.Enabled = true;
		}
	}

	private void btnBrowse_Click(object sender, EventArgs e)
	{
		List<FileInfo> xttFileList = new List<FileInfo>();
		try
		{
			BrowseForXTTs(xttFileList);
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void BrowseForXTTs(List<FileInfo> xttFileList)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.InitialDirectory = tempPath;
		openFileDialog.Filter = "XTT files (*.xtt)|*.xtt|All files (*.*)|*.*";
		openFileDialog.FilterIndex = 1;
		openFileDialog.Title = "Select a test Tree File";
		if (openFileDialog.ShowDialog() != DialogResult.OK)
		{
			return;
		}
		xttPath = openFileDialog.FileName;
		string[] array = xttPath.Split('_');
		bool flag = false;
		string[] array2 = array;
		foreach (string text in array2)
		{
			switch (text)
			{
			case "CDMA":
				Tech = text;
				flag = true;
				break;
			case "WCDMA":
				Tech = text;
				flag = true;
				break;
			case "LTE":
				Tech = text;
				flag = true;
				break;
			default:
				continue;
			}
			break;
		}
		if (flag)
		{
			TreeName = Path.GetFileName(xttPath);
			xttFileList.Add(new FileInfo(TreeName));
			return;
		}
		throw new Exception("Filename does not contain technology!");
	}

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

	private void InitializeComponent()
	{
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.panel1 = new System.Windows.Forms.Panel();
		this.btnBrowse = new System.Windows.Forms.Button();
		this.groupBox1.SuspendLayout();
		base.SuspendLayout();
		this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.btnOK.Enabled = false;
		this.btnOK.Location = new System.Drawing.Point(734, 423);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(75, 23);
		this.btnOK.TabIndex = 3;
		this.btnOK.Text = "OK";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(818, 423);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.groupBox1.Controls.Add(this.panel1);
		this.groupBox1.Location = new System.Drawing.Point(30, 12);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(863, 390);
		this.groupBox1.TabIndex = 5;
		this.groupBox1.TabStop = false;
		this.panel1.AutoScroll = true;
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(3, 16);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(857, 371);
		this.panel1.TabIndex = 0;
		this.btnBrowse.Location = new System.Drawing.Point(33, 434);
		this.btnBrowse.Name = "btnBrowse";
		this.btnBrowse.Size = new System.Drawing.Size(75, 23);
		this.btnBrowse.TabIndex = 0;
		this.btnBrowse.Text = "...";
		this.btnBrowse.UseVisualStyleBackColor = true;
		this.btnBrowse.Click += new System.EventHandler(btnBrowse_Click);
		base.AcceptButton = this.btnOK;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.CancelButton = this.btnCancel;
		base.ClientSize = new System.Drawing.Size(918, 469);
		base.Controls.Add(this.btnBrowse);
		base.Controls.Add(this.groupBox1);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Name = "PopupForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Select Tree";
		this.groupBox1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
