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

namespace QDART_MFG_CfgEditor;

public class Namer : Form
{
	private IContainer components;

	private Label label1;

	private Label label2;

	private Button btnIN;

	private Button btnOUT;

	private Label lbPreview;

	private TextBox tbView;

	private Button btnOK;

	private Button btnCancel;

	private ContextMenuStrip cmsSelect;

	private ToolStripMenuItem newToolStripMenuItem;

	private ToolStripMenuItem renameToolStripMenuItem;

	private ToolStripMenuItem deleteToolStripMenuItem;

	private ListView lvOptions;

	private ListView lvSelected;

	private ContextMenuStrip cmsOptions;

	private ToolStripMenuItem newToolStripMenuItem1;

	private ToolStripMenuItem renameToolStripMenuItem1;

	private ToolStripMenuItem deleteToolStripMenuItem1;

	private ToolTip toolTip1;

	public Namer()
	{
		InitializeComponent();
	}

	public Namer(string inStr)
	{
		InitializeComponent();
		string[] array = inStr.Split(';');
		if (array.Length > 2)
		{
			string[] array2 = array[0].Split(new char[1] { '-' }, StringSplitOptions.RemoveEmptyEntries);
			string[] array3 = array[1].Split(new char[1] { '-' }, StringSplitOptions.RemoveEmptyEntries);
			string text = "";
			for (int i = 0; i < array2.Length; i++)
			{
				text = array2[i];
				lvSelected.Items.Add(text, text, null);
				if (Match(text))
				{
					lvSelected.Items[i].ForeColor = Color.Tomato;
				}
			}
			for (int j = 0; j < array3.Length; j++)
			{
				text = array3[j];
				if (!array2.Contains(text))
				{
					lvOptions.Items.Add(text, text, null);
					if (Match(text))
					{
						int count = lvOptions.Items.Count;
						lvOptions.Items[count - 1].ForeColor = Color.Tomato;
					}
				}
			}
			Text = Text + " (" + array[2] + ")";
			Label label = lbPreview;
			label.Text = label.Text + " (" + array[2] + ")";
		}
		UpdatePreView();
	}

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

	private void btnIN_Click(object sender, EventArgs e)
	{
		Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
		for (int i = 0; i < lvOptions.Items.Count; i++)
		{
			dictionary.Add(lvOptions.Items[i].Text, lvOptions.Items[i].Checked);
		}
		foreach (KeyValuePair<string, bool> item in dictionary)
		{
			if (item.Value)
			{
				lvSelected.Items.Add(item.Key, item.Key, null);
				if (Match(item.Key))
				{
					lvSelected.FindItemWithText(item.Key).ForeColor = Color.Tomato;
				}
				lvOptions.Items.RemoveByKey(item.Key);
			}
		}
		UpdatePreView();
	}

	private bool Match(string str)
	{
		if (string.IsNullOrEmpty(str))
		{
			return false;
		}
		if (str.StartsWith("%") && str.EndsWith("%"))
		{
			return true;
		}
		return false;
	}

	private string GetRemoveString(string strIn)
	{
		return "ListViewItem:{" + strIn + "}";
	}

	private void btnOUT_Click(object sender, EventArgs e)
	{
		Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
		for (int i = 0; i < lvSelected.Items.Count; i++)
		{
			dictionary.Add(lvSelected.Items[i].Text, lvSelected.Items[i].Checked);
		}
		foreach (KeyValuePair<string, bool> item in dictionary)
		{
			if (item.Value)
			{
				lvOptions.Items.Add(item.Key, item.Key, null);
				if (Match(item.Key))
				{
					lvOptions.FindItemWithText(item.Key).ForeColor = Color.Tomato;
				}
				lvSelected.Items.RemoveByKey(item.Key);
			}
		}
		UpdatePreView();
	}

	private void UpdatePreView()
	{
		string text = "";
		for (int i = 0; i < lvSelected.Items.Count; i++)
		{
			text = text + lvSelected.Items[i].Text + "-";
		}
		int length = text.Length;
		if (length != 0)
		{
			if (text[length - 1] == '-')
			{
				text = text.Substring(0, length - 1);
			}
			foreach (KeyValuePair<string, string> item in Common.LogSample)
			{
				if (text.Contains(item.Key))
				{
					text = text.Replace(item.Key, item.Value);
				}
			}
		}
		else
		{
			text = "Keep default name due to nothing configured";
		}
		tbView.Text = text;
	}

	private void cmsSelect_Opening(object sender, CancelEventArgs e)
	{
		if (lvSelected.SelectedItems.Count == 0)
		{
			renameToolStripMenuItem.Visible = false;
			deleteToolStripMenuItem.Visible = false;
		}
		else
		{
			renameToolStripMenuItem.Visible = true;
			deleteToolStripMenuItem.Visible = true;
		}
	}

	private void cmsOptions_Opening(object sender, CancelEventArgs e)
	{
		if (lvOptions.SelectedItems.Count == 0)
		{
			renameToolStripMenuItem1.Visible = false;
			deleteToolStripMenuItem1.Visible = false;
		}
		else
		{
			renameToolStripMenuItem1.Visible = true;
			deleteToolStripMenuItem1.Visible = true;
		}
	}

	private void newToolStripMenuItem_Click(object sender, EventArgs e)
	{
		Input input = new Input();
		input.ShowDialog();
		string inputString = input.inputString;
		if (!(inputString == ""))
		{
			if (lvSelected.Items.ContainsKey(inputString) || lvOptions.Items.ContainsKey(inputString))
			{
				MessageBox.Show(inputString + " already exist.");
				return;
			}
			lvSelected.Items.Add(inputString, inputString, null);
			UpdatePreView();
		}
	}

	private void renameToolStripMenuItem_Click(object sender, EventArgs e)
	{
		if (lvSelected.SelectedItems.Count == 0)
		{
			return;
		}
		Input input = new Input();
		input.ShowDialog();
		string inputString = input.inputString;
		if (!(inputString == ""))
		{
			if (lvSelected.Items.ContainsKey(inputString) || lvOptions.Items.ContainsKey(inputString))
			{
				MessageBox.Show(inputString + " already exist.");
				return;
			}
			int index = lvSelected.FocusedItem.Index;
			lvSelected.Items.RemoveAt(index);
			lvSelected.Items.Insert(index, inputString, inputString, null);
			UpdatePreView();
		}
	}

	private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
	{
		if (lvSelected.SelectedItems.Count != 0)
		{
			int index = lvSelected.FocusedItem.Index;
			lvSelected.Items.RemoveAt(index);
			UpdatePreView();
		}
	}

	private void newToolStripMenuItem1_Click(object sender, EventArgs e)
	{
		Input input = new Input();
		input.ShowDialog();
		string inputString = input.inputString;
		if (!(inputString == ""))
		{
			if (lvOptions.Items.ContainsKey(inputString) || lvSelected.Items.ContainsKey(inputString))
			{
				MessageBox.Show(inputString + " already exist.");
			}
			else
			{
				lvOptions.Items.Add(inputString, inputString, null);
			}
		}
	}

	private void renameToolStripMenuItem1_Click(object sender, EventArgs e)
	{
		if (lvOptions.SelectedItems.Count == 0)
		{
			return;
		}
		Input input = new Input();
		input.ShowDialog();
		string inputString = input.inputString;
		if (!(inputString == ""))
		{
			if (lvSelected.Items.ContainsKey(inputString) || lvOptions.Items.ContainsKey(inputString))
			{
				MessageBox.Show(inputString + " already exist.");
				return;
			}
			int index = lvOptions.FocusedItem.Index;
			lvOptions.Items.RemoveAt(index);
			lvOptions.Items.Insert(index, inputString, inputString, null);
		}
	}

	private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
	{
		if (lvOptions.SelectedItems.Count != 0)
		{
			int index = lvOptions.FocusedItem.Index;
			lvOptions.Items.RemoveAt(index);
		}
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		string text = "";
		if (lvSelected.Items.Count != 0)
		{
			foreach (ListViewItem item in lvSelected.Items)
			{
				text = text + item.Text + "-";
			}
			text = text.Substring(0, text.Length - 1);
		}
		base.Tag = text;
		base.DialogResult = DialogResult.OK;
		Close();
	}

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

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QDART_MFG_CfgEditor.Namer));
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.btnIN = new System.Windows.Forms.Button();
		this.btnOUT = new System.Windows.Forms.Button();
		this.lbPreview = new System.Windows.Forms.Label();
		this.tbView = new System.Windows.Forms.TextBox();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.cmsSelect = new System.Windows.Forms.ContextMenuStrip(this.components);
		this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
		this.renameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
		this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
		this.lvOptions = new System.Windows.Forms.ListView();
		this.lvSelected = new System.Windows.Forms.ListView();
		this.cmsOptions = new System.Windows.Forms.ContextMenuStrip(this.components);
		this.newToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
		this.renameToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
		this.deleteToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
		this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
		this.cmsSelect.SuspendLayout();
		this.cmsOptions.SuspendLayout();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.label1.Location = new System.Drawing.Point(378, 47);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(65, 16);
		this.label1.TabIndex = 1;
		this.label1.Text = "Options ";
		this.label2.AutoSize = true;
		this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.label2.ForeColor = System.Drawing.SystemColors.Highlight;
		this.label2.Location = new System.Drawing.Point(73, 47);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(88, 16);
		this.label2.TabIndex = 1;
		this.label2.Text = "Customized";
		this.btnIN.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnIN.Location = new System.Drawing.Point(240, 115);
		this.btnIN.Name = "btnIN";
		this.btnIN.Size = new System.Drawing.Size(65, 39);
		this.btnIN.TabIndex = 2;
		this.btnIN.Text = "<<";
		this.btnIN.UseVisualStyleBackColor = true;
		this.btnIN.Click += new System.EventHandler(btnIN_Click);
		this.btnOUT.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnOUT.Location = new System.Drawing.Point(240, 197);
		this.btnOUT.Name = "btnOUT";
		this.btnOUT.Size = new System.Drawing.Size(65, 39);
		this.btnOUT.TabIndex = 2;
		this.btnOUT.Text = ">>";
		this.btnOUT.UseVisualStyleBackColor = true;
		this.btnOUT.Click += new System.EventHandler(btnOUT_Click);
		this.lbPreview.AutoSize = true;
		this.lbPreview.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.lbPreview.ForeColor = System.Drawing.SystemColors.Highlight;
		this.lbPreview.Location = new System.Drawing.Point(26, 312);
		this.lbPreview.Name = "lbPreview";
		this.lbPreview.Size = new System.Drawing.Size(63, 16);
		this.lbPreview.TabIndex = 5;
		this.lbPreview.Text = "Preview";
		this.toolTip1.SetToolTip(this.lbPreview, "Sample for %XXX% match:\r\n%PCNAME%  -> MyPC\r\n%DATE%  -> 2017_12_30\r\n%DATE_TIME%  -> 23_59_59_999\r\n%TECHNOLOGIES%  -> GWL\r\n%DUT_ID%  -> 4\r\n%SN%  -> SN123\r\n%TREENAME%  -> MyTreeName");
		this.tbView.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.tbView.ForeColor = System.Drawing.SystemColors.MenuHighlight;
		this.tbView.Location = new System.Drawing.Point(29, 331);
		this.tbView.Multiline = true;
		this.tbView.Name = "tbView";
		this.tbView.ReadOnly = true;
		this.tbView.Size = new System.Drawing.Size(474, 47);
		this.tbView.TabIndex = 6;
		this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.btnOK.Location = new System.Drawing.Point(312, 393);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(79, 37);
		this.btnOK.TabIndex = 7;
		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.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.btnCancel.Location = new System.Drawing.Point(421, 393);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(79, 37);
		this.btnCancel.TabIndex = 7;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.cmsSelect.Items.AddRange(new System.Windows.Forms.ToolStripItem[3] { this.newToolStripMenuItem, this.renameToolStripMenuItem, this.deleteToolStripMenuItem });
		this.cmsSelect.Name = "cmsSelect";
		this.cmsSelect.Size = new System.Drawing.Size(118, 70);
		this.cmsSelect.Opening += new System.ComponentModel.CancelEventHandler(cmsSelect_Opening);
		this.newToolStripMenuItem.Name = "newToolStripMenuItem";
		this.newToolStripMenuItem.Size = new System.Drawing.Size(117, 22);
		this.newToolStripMenuItem.Text = "New";
		this.newToolStripMenuItem.Click += new System.EventHandler(newToolStripMenuItem_Click);
		this.renameToolStripMenuItem.Name = "renameToolStripMenuItem";
		this.renameToolStripMenuItem.Size = new System.Drawing.Size(117, 22);
		this.renameToolStripMenuItem.Text = "Rename";
		this.renameToolStripMenuItem.Click += new System.EventHandler(renameToolStripMenuItem_Click);
		this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
		this.deleteToolStripMenuItem.Size = new System.Drawing.Size(117, 22);
		this.deleteToolStripMenuItem.Text = "Delete";
		this.deleteToolStripMenuItem.Click += new System.EventHandler(deleteToolStripMenuItem_Click);
		this.lvOptions.CheckBoxes = true;
		this.lvOptions.ContextMenuStrip = this.cmsOptions;
		this.lvOptions.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.lvOptions.Location = new System.Drawing.Point(334, 79);
		this.lvOptions.MultiSelect = false;
		this.lvOptions.Name = "lvOptions";
		this.lvOptions.Size = new System.Drawing.Size(182, 199);
		this.lvOptions.TabIndex = 10;
		this.lvOptions.UseCompatibleStateImageBehavior = false;
		this.lvOptions.View = System.Windows.Forms.View.List;
		this.lvSelected.CheckBoxes = true;
		this.lvSelected.ContextMenuStrip = this.cmsSelect;
		this.lvSelected.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.lvSelected.Location = new System.Drawing.Point(29, 79);
		this.lvSelected.MultiSelect = false;
		this.lvSelected.Name = "lvSelected";
		this.lvSelected.Size = new System.Drawing.Size(182, 199);
		this.lvSelected.TabIndex = 11;
		this.lvSelected.UseCompatibleStateImageBehavior = false;
		this.lvSelected.View = System.Windows.Forms.View.List;
		this.cmsOptions.Items.AddRange(new System.Windows.Forms.ToolStripItem[3] { this.newToolStripMenuItem1, this.renameToolStripMenuItem1, this.deleteToolStripMenuItem1 });
		this.cmsOptions.Name = "cmsOptions";
		this.cmsOptions.Size = new System.Drawing.Size(118, 70);
		this.cmsOptions.Opening += new System.ComponentModel.CancelEventHandler(cmsOptions_Opening);
		this.newToolStripMenuItem1.Name = "newToolStripMenuItem1";
		this.newToolStripMenuItem1.Size = new System.Drawing.Size(117, 22);
		this.newToolStripMenuItem1.Text = "New";
		this.newToolStripMenuItem1.Click += new System.EventHandler(newToolStripMenuItem1_Click);
		this.renameToolStripMenuItem1.Name = "renameToolStripMenuItem1";
		this.renameToolStripMenuItem1.Size = new System.Drawing.Size(117, 22);
		this.renameToolStripMenuItem1.Text = "Rename";
		this.renameToolStripMenuItem1.Click += new System.EventHandler(renameToolStripMenuItem1_Click);
		this.deleteToolStripMenuItem1.Name = "deleteToolStripMenuItem1";
		this.deleteToolStripMenuItem1.Size = new System.Drawing.Size(117, 22);
		this.deleteToolStripMenuItem1.Text = "Delete";
		this.deleteToolStripMenuItem1.Click += new System.EventHandler(deleteToolStripMenuItem1_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(544, 442);
		base.Controls.Add(this.lvSelected);
		base.Controls.Add(this.lvOptions);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.tbView);
		base.Controls.Add(this.lbPreview);
		base.Controls.Add(this.btnOUT);
		base.Controls.Add(this.btnIN);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.MaximizeBox = false;
		this.MaximumSize = new System.Drawing.Size(560, 480);
		this.MinimumSize = new System.Drawing.Size(560, 480);
		base.Name = "Namer";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Namer";
		base.Load += new System.EventHandler(Namer_Load);
		this.cmsSelect.ResumeLayout(false);
		this.cmsOptions.ResumeLayout(false);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
