using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using MultiLanguageManger;
using ProductConfigurationController;

namespace QC.QMSCT;

public class AppConfiguration : Form
{
	private enum AppConfigTabIndex
	{
		CONFIGURATION_INDEX,
		DEVICE_SETUP,
		VERIFICATION
	}

	public delegate void AppConfigLanguageChanged(string Language);

	public delegate void AppConfigResetStationYield();

	public delegate void AppConfigClosed();

	private const string PCFG_NAME = "ProductConfiguration.pcfg";

	private const string VID = "VID";

	private const string PID = "PID";

	private const int PRODUCT_TAB = 0;

	private const int STATION_TAB = 1;

	private const int CP_NOCLOSE_BUTTON = 512;

	private QmsctSettings qmsctsettings = new QmsctSettings();

	private bool showAllOption = true;

	private bool roundRobinSchedulingSelected;

	private DataTable vidpidTable;

	private IContainer components;

	private GroupBox groupBox_SetUp;

	private TabControl tabControl_Configuration;

	private TabPage tabPage_Station;

	private DeviceSetup deviceSetup;

	private GroupBox groupBox_ConfigControls;

	private GroupBox groupBox_SaveOrExit;

	private Button button_SaveAsFile;

	private Button button_Exit;

	private Button button_Save;

	private GroupBox groupBox_Options;

	private GroupBox groupBox_Scheduling;

	private CheckBox checkBox_StartQSPROnSetup;

	private Label label_Mode;

	private ComboBox comboBox_Mode;

	private GroupBox groupBox_VidPid;

	private DataGridView vidPidView;

	private QMSCTConfiguration qmsctConfiguration;

	private TextBox txtConfigRootFolder;

	private Label lblRootFolder;

	private TabPage tabPage_Product;

	private ProductConfigControl productController;

	private Button button_SetVidPid;

	public StationConfiguration QmsctStationSetup => qmsctsettings.StationConfiguration;

	public ProductConfigManager QmsctProductSetup => qmsctsettings.ProductConfigManager;

	public PortDetectionSettings QmsctPortDetectionSettings => qmsctsettings.PortDetectionSettings;

	public UnitProgrammerGenericSetup QmsctUnitProgrammerGenericSetup => qmsctsettings.UnitProgrammerGenericSetup;

	public string ConfigRootFolder => qmsctsettings.ConfigRootFolder;

	public string Product
	{
		get
		{
			return qmsctsettings.ProductName;
		}
		set
		{
			qmsctsettings.ProductName = value;
		}
	}

	public string Configuration
	{
		get
		{
			return qmsctsettings.ProductConfig;
		}
		set
		{
			qmsctsettings.ProductConfig = value;
		}
	}

	public string[] RougeProcess => qmsctsettings.RougeProcess;

	public bool EnableAllDoneProcess => qmsctsettings.EnableAllDoneProcess;

	public bool ShowAllOption
	{
		set
		{
			showAllOption = value;
		}
	}

	protected override CreateParams CreateParams
	{
		get
		{
			CreateParams createParams = base.CreateParams;
			createParams.ClassStyle |= 512;
			return createParams;
		}
	}

	public QMSCTUserMode QMSCTMode
	{
		get
		{
			return qmsctConfiguration.QMSCTMode;
		}
		set
		{
			qmsctConfiguration.QMSCTMode = value;
		}
	}

	public event AppConfigLanguageChanged OnAppConfigLanguageChanged;

	public event AppConfigResetStationYield OnAppConfigResetStationYield;

	public event AppConfigClosed OnAppConfigClosed;

	public UnitProgrammerTechnicalSetup GetGetQmsctUnitProgrammerTechnicalSetup(int PanelIndex)
	{
		return qmsctsettings.GetUnitProgrammerTechnicalSetup(PanelIndex);
	}

	public AppConfiguration(string product, string config)
	{
		InitializeComponent();
		if (!qmsctsettings.Load())
		{
			if (!GetUserInputYesNo("There may because of a incompitable version of QMSCT config, Will you reset it to default setting ?", "Warning"))
			{
				throw new Exception("QMSCT will exit");
			}
			qmsctsettings.CreateDefaultFile();
			if (!qmsctsettings.Load())
			{
				throw new Exception("QMSCT will exit");
			}
		}
		StationConfiguration qmsctStationSetup = QmsctStationSetup;
		qmsctStationSetup.OnLoad = (StationConfiguration.LoadEvent)Delegate.Combine(qmsctStationSetup.OnLoad, new StationConfiguration.LoadEvent(UpdateStationConfigGUI));
		deviceSetup.OnDeviceSetupInProgress += deviceSetup_OnDeviceSetupInProgress;
		qmsctConfiguration.OnQmsctConfigurationNumberOfPanelsChanged += qmsctConfiguration_OnQmsctConfigurationNumberOfPanelsChanged;
		qmsctConfiguration.OnQmsctConfigurationNumberOfColumnsChanged += qmsctConfiguration_OnQmsctConfigurationNumberOfColumnsChanged;
		qmsctConfiguration.OnQmsctConfigurationLanguageChanged += qmsctConfiguration_OnQmsctConfigurationLanguageChanged;
		qmsctConfiguration.OnQmsctResetStationYieldClick += qmsctConfiguration_OnQmsctResetStationYieldClick;
		qmsctConfiguration.OnQmsctLoadStationConfiguration += qmsctConfiguration_OnQmsctLoadStationConfiguration;
		qmsctConfiguration.InitializePage();
		string text = Path.Combine(ConfigRootFolder, "ProductConfiguration.pcfg");
		if (text == null || text.Length == 0)
		{
			text = Path.Combine("C:\\Qualcomm\\QMSCT\\configuration", "ProductConfiguration.pcfg");
		}
		QmsctProductSetup.SetFileName(text);
		ProductConfigControl productConfigControl = productController;
		productConfigControl.OnChangeStationConfiguration = (EventHandler)Delegate.Combine(productConfigControl.OnChangeStationConfiguration, new EventHandler(LoadStationConfigurationFileFromProductConfigurationFile));
		ProductConfigControl productConfigControl2 = productController;
		productConfigControl2.OnDiscardChanges = (EventHandler)Delegate.Combine(productConfigControl2.OnDiscardChanges, new EventHandler(OnDiscardChanges));
		productController.InitializePage(QmsctProductSetup);
		if (!File.Exists(QmsctProductSetup.Filename))
		{
			QmsctProductSetup.CreateDefaultProductSetupXml();
		}
		QmsctProductSetup.Load();
		txtConfigRootFolder.Text = ConfigRootFolder;
		SelectProductConfiguration(product, config);
		deviceSetup.InitializePage(QmsctPortDetectionSettings, QmsctStationSetup);
	}

	private void qmsctConfiguration_OnQmsctResetStationYieldClick()
	{
		this.OnAppConfigResetStationYield();
	}

	private void qmsctConfiguration_OnQmsctLoadStationConfiguration(string stationConfigFile)
	{
		QmsctStationSetup.Load(stationConfigFile);
	}

	private bool IsValidFileName(string fileName)
	{
		if (string.IsNullOrEmpty(fileName))
		{
			MessageBox.Show(CommonResource.BoxMsgConfigNameIsNull, CommonResource.BoxTitleWarning);
			return false;
		}
		foreach (char c in fileName)
		{
			if (!char.IsLetter(c) && !char.IsDigit(c) && c != '_')
			{
				MessageBox.Show(CommonResource.BoxMsgConfigNameInvalid, CommonResource.BoxTitleWarning);
				return false;
			}
		}
		return true;
	}

	private void reloadTabWithPreviouslySavedValues()
	{
		productController.DiscardChanges();
	}

	private bool SaveStationConfiguration(string filename = null)
	{
		try
		{
			if (!IsDataValidToSave())
			{
				reloadTabWithPreviouslySavedValues();
				return false;
			}
			if (!deviceSetup.UpdateAndSave())
			{
				MessageBox.Show(CommonResource.BoxMsgCheckPortFail, CommonResource.BoxTitleWarning);
				return false;
			}
			qmsctConfiguration.UpdateAndSave();
			QmsctStationSetup.DetectionInstanceValue = deviceSetup.DetectionInstance;
			QmsctStationSetup.ManualSetup = deviceSetup.ManualSetup;
			QmsctStationSetup.DualPortSolution = deviceSetup.DualPortSolution;
			QmsctStationSetup.PortMapping = deviceSetup.PortMapping;
			QmsctStationSetup.RunRoundRobinScheduling = roundRobinSchedulingSelected;
			QmsctStationSetup.StartQSPRAtStartofQMSCT = checkBox_StartQSPROnSetup.Checked;
			if (filename == null)
			{
				QmsctStationSetup.Filename = qmsctConfiguration.FileName;
			}
			else
			{
				QmsctStationSetup.Filename = filename;
			}
			QmsctStationSetup.NumberOfPanels = qmsctConfiguration.NumberOfPanels;
			QmsctStationSetup.NumberOfColumns = qmsctConfiguration.NumberOfColumns;
			QmsctStationSetup.GetSNFromEngine = qmsctConfiguration.GetSNFromEngine;
			QmsctStationSetup.ShowDebugMessagesFromEngine = qmsctConfiguration.ShowDebugMessagesFromEngine;
			QmsctStationSetup.SaveMessagesToFile = qmsctConfiguration.SaveMessagesToFile;
			QmsctStationSetup.ShowSameColor = qmsctConfiguration.ShowSameColor;
			QmsctStationSetup.AppLanguage = qmsctConfiguration.AppLanguage;
			if (qmsctConfiguration.LogFilter.Count == 0)
			{
				QmsctStationSetup.LogCategories = null;
			}
			else
			{
				QmsctStationSetup.LogCategories = qmsctConfiguration.LogFilter.ToArray();
			}
			UpdateVIDPIDConfigurationByGrid();
			QmsctStationSetup.Save();
			SaveProductConfiguration();
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(CommonResource.BoxMsgFailToSaveStation + ex.Message);
			return false;
		}
	}

	private void SaveNewStationConfiguration()
	{
		try
		{
			InputForm inputForm = new InputForm();
			inputForm.ValidFunction = IsValidFileName;
			if (inputForm.ShowUserInput(CommonResource.lblInputStationFile) == DialogResult.OK)
			{
				string inputText = inputForm.InputText;
				string text = Path.Combine(ConfigRootFolder, inputText + ".scfg");
				if ((!File.Exists(text) || GetUserInputYesNo(string.Format(CommonResource.BoxMsgStationConfigExist, text), "Warning")) && SaveStationConfiguration(text))
				{
					qmsctConfiguration.textBox_StationName.Text = QmsctStationSetup.Filename;
				}
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(CommonResource.BoxMsgFailToSaveNewStation + ex.Message);
		}
	}

	private bool SaveProductConfiguration(string filename = null)
	{
		try
		{
			if (!productController.CheckAndSaveProductConfigurationFromUI())
			{
				return false;
			}
			if (!IsDataValidToSave())
			{
				reloadTabWithPreviouslySavedValues();
				return false;
			}
			QmsctProductSetup.StationConfigName = QmsctStationSetup.StationName;
			QmsctProductSetup.Save();
			SaveLastProductConfiguration();
			MessageBox.Show(CommonResource.BoxMsgSaveSuccessfully);
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show(CommonResource.BoxMsgFailToSaveProduct + ex.Message);
			return false;
		}
	}

	private bool ConfigurationCancelled(int tabkey)
	{
		bool result = true;
		string text = "";
		string boxTitleSettingNotSaved = CommonResource.BoxTitleSettingNotSaved;
		if (tabkey == 0)
		{
			try
			{
				if (HasProductTabBeenModified())
				{
					text = CommonResource.BoxMsgSettingNotSaved;
				}
				else if (HasProductTabBeenChanged())
				{
					text = CommonResource.BoxMsgSettingNotApplied;
				}
				if (text != "")
				{
					if (GetUserInputYesNo(text, boxTitleSettingNotSaved))
					{
						result = SaveProductConfiguration();
					}
					else
					{
						reloadTabWithPreviouslySavedValues();
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = false;
			}
		}
		else
		{
			try
			{
				if (HasStationTabBeenModified())
				{
					text = CommonResource.BoxMsgSettingNotSaved;
				}
				else if (HasStationTabBeenChanged())
				{
					text = CommonResource.BoxMsgSettingNotApplied;
				}
				if (text != "" && GetUserInputYesNo(text, boxTitleSettingNotSaved))
				{
					result = SaveStationConfiguration();
				}
				else
				{
					reloadTabWithPreviouslySavedValues();
				}
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message);
				result = false;
			}
		}
		return result;
	}

	public void SetupUsbFlags()
	{
		try
		{
			List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
			list.Add(new KeyValuePair<string, string>("05C6", "9006"));
			list.Add(new KeyValuePair<string, string>("05C6", "9008"));
			list.Add(new KeyValuePair<string, string>("05C6", "9025"));
			list.Add(new KeyValuePair<string, string>("05C6", "9026"));
			list.Add(new KeyValuePair<string, string>("05C6", "9031"));
			list.Add(new KeyValuePair<string, string>("05C6", "9032"));
			list.Add(new KeyValuePair<string, string>("05C6", "9037"));
			list.Add(new KeyValuePair<string, string>("05C6", "9038"));
			list.Add(new KeyValuePair<string, string>("05C6", "9053"));
			list.Add(new KeyValuePair<string, string>("05C6", "9091"));
			if (QmsctStationSetup == null || QmsctStationSetup.VidPids == null)
			{
				setVidPidDataGrid(list);
				return;
			}
			VidPidType[] vidPids = QmsctStationSetup.VidPids;
			foreach (VidPidType vidPidType in vidPids)
			{
				if (vidPidType.Ignore && !list.Contains(new KeyValuePair<string, string>(vidPidType.VID, vidPidType.PID)))
				{
					list.Add(new KeyValuePair<string, string>(vidPidType.VID, vidPidType.PID));
				}
			}
			setVidPidDataGrid(list);
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void setVidPidDataGrid(List<KeyValuePair<string, string>> vidpidList)
	{
		if (vidpidTable == null)
		{
			vidpidTable = new DataTable();
			vidpidTable.Columns.Add(new DataColumn("VID", typeof(string)));
			vidpidTable.Columns.Add(new DataColumn("PID", typeof(string)));
		}
		else
		{
			vidpidTable.Rows.Clear();
		}
		for (int i = 0; i < vidpidList.Count; i++)
		{
			DataRow dataRow = vidpidTable.NewRow();
			KeyValuePair<string, string> keyValuePair = vidpidList[i];
			dataRow["VID"] = keyValuePair.Key;
			dataRow["PID"] = keyValuePair.Value;
			vidpidTable.Rows.Add(dataRow);
		}
		vidPidView.DataSource = vidpidTable;
	}

	private bool SetupVidPidRegistryEntry(string vid, string pid)
	{
		RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\UsbFlags\\", writable: true);
		if (registryKey == null)
		{
			return false;
		}
		string name = "IgnoreHWSerNum" + vid.ToUpper() + pid.ToUpper();
		if (registryKey.GetValue(name) == null)
		{
			registryKey.SetValue(name, new byte[1] { 1 }, RegistryValueKind.Binary);
			return true;
		}
		return false;
	}

	private void button_SetVidPid_Click(object sender, EventArgs e)
	{
		try
		{
			bool flag = false;
			foreach (DataRow row in vidpidTable.Rows)
			{
				flag |= SetupVidPidRegistryEntry(row["VID"].ToString(), row["PID"].ToString());
			}
			if (flag)
			{
				MessageBox.Show(CommonResource.BoxMsgSetupUsbFlags, CommonResource.BoxTitleSetupUsbFlags);
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void qmsctConfiguration_OnQmsctConfigurationNumberOfPanelsChanged(int numberOfPanels)
	{
		deviceSetup.TotalNumberOfPanels = numberOfPanels;
	}

	private void qmsctConfiguration_OnQmsctConfigurationNumberOfColumnsChanged(int numberOfPanels)
	{
	}

	private void qmsctConfiguration_OnQmsctConfigurationLanguageChanged(string Language)
	{
		if (this.OnAppConfigLanguageChanged != null)
		{
			this.OnAppConfigLanguageChanged(Language);
		}
	}

	private void deviceSetup_OnDeviceSetupInProgress(bool started)
	{
		try
		{
			if (started)
			{
				button_Exit.Enabled = false;
				button_Save.Enabled = false;
				button_SaveAsFile.Enabled = false;
				EnableControls(qmsctConfiguration.Controls, enable: false);
				vidPidView.Enabled = false;
				checkBox_StartQSPROnSetup.Enabled = false;
			}
			else
			{
				button_Exit.Enabled = true;
				button_Save.Enabled = true;
				button_SaveAsFile.Enabled = true;
				EnableControls(qmsctConfiguration.Controls, enable: true);
				vidPidView.Enabled = true;
				checkBox_StartQSPROnSetup.Enabled = true;
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private static void EnableControls(Control.ControlCollection collection, bool enable)
	{
		foreach (Control item in collection)
		{
			item.Enabled = enable;
			EnableControls(item.Controls, enable);
		}
	}

	private bool GetUserInputYesNo(string message, string caption)
	{
		MessageBoxButtons buttons = MessageBoxButtons.YesNo;
		DialogResult dialogResult = MessageBox.Show(this, message, caption, buttons);
		if (dialogResult == DialogResult.No)
		{
			return false;
		}
		return true;
	}

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

	private void UpdateStationConfigGUI()
	{
		try
		{
			deviceSetup.InitializePage(QmsctPortDetectionSettings, QmsctStationSetup);
			qmsctConfiguration.LoadConfig(ConfigRootFolder, QmsctStationSetup);
			SetupUsbFlags();
			checkBox_StartQSPROnSetup.Checked = QmsctStationSetup.StartQSPRAtStartofQMSCT;
			if (!QmsctStationSetup.RunRoundRobinScheduling)
			{
				comboBox_Mode.SelectedItem = comboBox_Mode.Items[0];
			}
			if (QmsctStationSetup.RunRoundRobinScheduling)
			{
				comboBox_Mode.SelectedItem = comboBox_Mode.Items[1];
			}
			if (showAllOption)
			{
				qmsctConfiguration.Enabled = true;
			}
			else
			{
				qmsctConfiguration.Enabled = false;
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private bool HasProductTabBeenChanged()
	{
		if (!Product.Equals(QmsctProductSetup.SelectedProduct, StringComparison.OrdinalIgnoreCase) || !Configuration.Equals(QmsctProductSetup.SelectedConfiguration, StringComparison.OrdinalIgnoreCase))
		{
			return true;
		}
		return false;
	}

	private bool HasProductTabBeenModified()
	{
		return productController.HasUIbeenModified();
	}

	private bool HasStationTabBeenChanged()
	{
		if (!QmsctProductSetup.StationConfigName.Equals(QmsctStationSetup.StationName, StringComparison.OrdinalIgnoreCase))
		{
			return true;
		}
		return false;
	}

	private bool HasStationTabBeenModified()
	{
		if (vidpidTable.Rows.Count != QmsctStationSetup.VidPids.Length)
		{
			return true;
		}
		bool flag = false;
		for (int i = 0; i < QmsctStationSetup.VidPids.Length; i++)
		{
			if (QmsctStationSetup.VidPids[i].VID != vidPidView[0, i].Value.ToString() || QmsctStationSetup.VidPids[i].PID != vidPidView[1, i].Value.ToString() || !QmsctStationSetup.VidPids[i].Ignore)
			{
				flag = true;
			}
		}
		if (flag)
		{
			return true;
		}
		if (!deviceSetup.HasGUIBeenModified() && !qmsctConfiguration.HasGUIBeenModified() && roundRobinSchedulingSelected == QmsctStationSetup.RunRoundRobinScheduling)
		{
			return QmsctStationSetup.StartQSPRAtStartofQMSCT != checkBox_StartQSPROnSetup.Checked;
		}
		return true;
	}

	private void UpdateVIDPIDConfigurationByGrid()
	{
		if (vidPidView.Rows.Count - 1 != QmsctStationSetup.VidPids.Length)
		{
			QmsctStationSetup.VidPids = new VidPidType[vidPidView.Rows.Count - 1];
			for (int i = 0; i < vidPidView.Rows.Count - 1; i++)
			{
				QmsctStationSetup.VidPids[i] = new VidPidType();
				QmsctStationSetup.VidPids[i].VID = vidPidView[0, i].Value.ToString();
				QmsctStationSetup.VidPids[i].PID = vidPidView[1, i].Value.ToString();
				QmsctStationSetup.VidPids[i].Ignore = true;
			}
			return;
		}
		for (int j = 0; j < QmsctStationSetup.VidPids.Length; j++)
		{
			if (QmsctStationSetup.VidPids[j].VID != vidPidView[0, j].Value.ToString() || QmsctStationSetup.VidPids[j].PID != vidPidView[1, j].Value.ToString() || !QmsctStationSetup.VidPids[j].Ignore)
			{
				QmsctStationSetup.VidPids[j].VID = vidPidView[0, j].Value.ToString();
				QmsctStationSetup.VidPids[j].PID = vidPidView[1, j].Value.ToString();
				QmsctStationSetup.VidPids[j].Ignore = true;
			}
		}
	}

	private void button_Save_Click(object sender, EventArgs e)
	{
		try
		{
			if (tabControl_Configuration.SelectedIndex == 0)
			{
				if (!SaveProductConfiguration())
				{
					return;
				}
			}
			else if (!SaveStationConfiguration())
			{
				return;
			}
			CloseForm();
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void button_SaveAsFile_Click(object sender, EventArgs e)
	{
		try
		{
			if (tabControl_Configuration.SelectedIndex == 0)
			{
				productController.NewProduct();
			}
			else
			{
				SaveNewStationConfiguration();
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void button_Exit_Click(object sender, EventArgs e)
	{
		try
		{
			if (ConfigurationCancelled(tabControl_Configuration.SelectedIndex))
			{
				CloseForm();
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	private void tabControl_Configuration_Deselected(object sender, TabControlEventArgs e)
	{
		ConfigurationCancelled(e.TabPageIndex);
	}

	private void tabControl_Configuration_Selecting(object sender, TabControlCancelEventArgs e)
	{
		if (!button_Save.Enabled)
		{
			deviceSetup.AbortSetupProcess(ignoreConfirmation: true);
		}
	}

	private void comboBox_Mode_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (comboBox_Mode.SelectedItem == comboBox_Mode.Items[1])
		{
			roundRobinSchedulingSelected = true;
		}
		else
		{
			roundRobinSchedulingSelected = false;
		}
	}

	private bool IsDataValidToSave()
	{
		int num = qmsctConfiguration.NumOfUnitsSelected();
		int totalNumberOfPanels = deviceSetup.TotalNumberOfPanels;
		if (num > totalNumberOfPanels)
		{
			string message = string.Format(CommonResource.BoxMsgDisplayedPanelWarning + "\r\r" + CommonResource.BoxMsgConfirmToSave, num.ToString(), totalNumberOfPanels.ToString(), totalNumberOfPanels.ToString());
			return GetUserInputYesNo(message, CommonResource.BoxTitlePanelPortMismatch);
		}
		return true;
	}

	public void UpdateLanguage()
	{
		lblRootFolder.Text = CommonResource.lblRootFolder;
		groupBox_ConfigControls.Text = CommonResource.GroupBox_ConfigControls;
		button_Exit.Text = CommonResource.Button_Exit;
		button_Save.Text = CommonResource.Button_Save;
		button_SaveAsFile.Text = CommonResource.Button_SaveAsFile;
		Text = CommonResource.AppConfiguration;
		tabControl_Configuration.TabPages[0].Text = CommonResource.tabProduct;
		tabControl_Configuration.TabPages[1].Text = CommonResource.tabStation;
		groupBox_Scheduling.Text = CommonResource.GroupBox_Scheduling;
		groupBox_Options.Text = CommonResource.GroupBox_Option;
		label_Mode.Text = CommonResource.LabelMode;
		checkBox_StartQSPROnSetup.Text = CommonResource.CheckBox_QmsctStartsServerUponStartup;
		productController.UpdateLanguage();
		deviceSetup.UpdateLanguage();
		qmsctConfiguration.UpdateLanguage();
	}

	private void SaveLastProductConfiguration()
	{
		Product = QmsctProductSetup.SelectedProduct;
		Configuration = QmsctProductSetup.SelectedConfiguration;
		qmsctsettings.SaveAppConfiguration();
	}

	public void SelectProductConfiguration(string product, string config)
	{
		productController.ChangeProduct(Product);
		productController.ChangeProductConfiguration(Configuration);
	}

	public void LoadStationConfigurationFileFromProductConfigurationFile(object obj, EventArgs args)
	{
		string text = "";
		text = Path.Combine(ConfigRootFolder, "SWDL.scfg");
		if (!File.Exists(text))
		{
			QmsctStationSetup.CreateDefaultStationConfigSetupXml(text);
		}
		if (QmsctProductSetup.StationConfigName != "")
		{
			text = Path.Combine(ConfigRootFolder, QmsctProductSetup.StationConfigName + ".scfg");
		}
		else
		{
			MessageBox.Show(CommonResource.BoxMsgUseDefultStationConfig, CommonResource.BoxTitleWarning);
		}
		if (text == null || text.Length == 0 || !File.Exists(text))
		{
			MessageBox.Show(string.Format(CommonResource.BoxMsgStationConfigFail, text), CommonResource.BoxTitleWarning);
			QmsctProductSetup.StationConfigName = "SWDL";
			text = Path.Combine(ConfigRootFolder, QmsctProductSetup.StationConfigName + ".scfg");
			if (text == null || text.Length == 0 || !File.Exists(text))
			{
				QmsctStationSetup.CreateDefaultStationConfigSetupXml(text);
			}
		}
		QmsctStationSetup.Load(text);
	}

	public void OnDiscardChanges(object obj, EventArgs args)
	{
		SelectProductConfiguration("", "");
	}

	private void CloseForm()
	{
		try
		{
			if (this.OnAppConfigClosed != null)
			{
				this.OnAppConfigClosed();
			}
			Close();
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
	}

	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.QMSCT.AppConfiguration));
		this.groupBox_SetUp = new System.Windows.Forms.GroupBox();
		this.tabControl_Configuration = new System.Windows.Forms.TabControl();
		this.tabPage_Product = new System.Windows.Forms.TabPage();
		this.productController = new QC.QMSCT.ProductConfigControl();
		this.tabPage_Station = new System.Windows.Forms.TabPage();
		this.groupBox_Options = new System.Windows.Forms.GroupBox();
		this.groupBox_Scheduling = new System.Windows.Forms.GroupBox();
		this.checkBox_StartQSPROnSetup = new System.Windows.Forms.CheckBox();
		this.label_Mode = new System.Windows.Forms.Label();
		this.comboBox_Mode = new System.Windows.Forms.ComboBox();
		this.groupBox_VidPid = new System.Windows.Forms.GroupBox();
		this.button_SetVidPid = new System.Windows.Forms.Button();
		this.vidPidView = new System.Windows.Forms.DataGridView();
		this.groupBox_ConfigControls = new System.Windows.Forms.GroupBox();
		this.qmsctConfiguration = new QC.QMSCT.QMSCTConfiguration();
		this.deviceSetup = new QC.QMSCT.DeviceSetup();
		this.groupBox_SaveOrExit = new System.Windows.Forms.GroupBox();
		this.button_SaveAsFile = new System.Windows.Forms.Button();
		this.button_Exit = new System.Windows.Forms.Button();
		this.button_Save = new System.Windows.Forms.Button();
		this.txtConfigRootFolder = new System.Windows.Forms.TextBox();
		this.lblRootFolder = new System.Windows.Forms.Label();
		this.tabControl_Configuration.SuspendLayout();
		this.tabPage_Product.SuspendLayout();
		this.tabPage_Station.SuspendLayout();
		this.groupBox_Options.SuspendLayout();
		this.groupBox_Scheduling.SuspendLayout();
		this.groupBox_VidPid.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.vidPidView).BeginInit();
		this.groupBox_ConfigControls.SuspendLayout();
		this.groupBox_SaveOrExit.SuspendLayout();
		base.SuspendLayout();
		this.groupBox_SetUp.Location = new System.Drawing.Point(12, 293);
		this.groupBox_SetUp.Name = "groupBox_SetUp";
		this.groupBox_SetUp.Size = new System.Drawing.Size(1003, 285);
		this.groupBox_SetUp.TabIndex = 4;
		this.groupBox_SetUp.TabStop = false;
		this.groupBox_SetUp.Text = "Set Up";
		this.tabControl_Configuration.Controls.Add(this.tabPage_Product);
		this.tabControl_Configuration.Controls.Add(this.tabPage_Station);
		this.tabControl_Configuration.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.tabControl_Configuration.Location = new System.Drawing.Point(12, 65);
		this.tabControl_Configuration.Name = "tabControl_Configuration";
		this.tabControl_Configuration.SelectedIndex = 0;
		this.tabControl_Configuration.Size = new System.Drawing.Size(1142, 591);
		this.tabControl_Configuration.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
		this.tabControl_Configuration.TabIndex = 5;
		this.tabControl_Configuration.Selecting += new System.Windows.Forms.TabControlCancelEventHandler(tabControl_Configuration_Selecting);
		this.tabControl_Configuration.Deselected += new System.Windows.Forms.TabControlEventHandler(tabControl_Configuration_Deselected);
		this.tabPage_Product.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.tabPage_Product.Controls.Add(this.productController);
		this.tabPage_Product.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.tabPage_Product.Location = new System.Drawing.Point(4, 22);
		this.tabPage_Product.Name = "tabPage_Product";
		this.tabPage_Product.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage_Product.Size = new System.Drawing.Size(1134, 565);
		this.tabPage_Product.TabIndex = 0;
		this.tabPage_Product.Text = "Product";
		this.productController.Location = new System.Drawing.Point(10, -1);
		this.productController.Name = "productController";
		this.productController.Size = new System.Drawing.Size(1116, 565);
		this.productController.TabIndex = 0;
		this.tabPage_Station.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.tabPage_Station.Controls.Add(this.groupBox_Options);
		this.tabPage_Station.Controls.Add(this.groupBox_ConfigControls);
		this.tabPage_Station.Controls.Add(this.deviceSetup);
		this.tabPage_Station.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.tabPage_Station.Location = new System.Drawing.Point(4, 22);
		this.tabPage_Station.Name = "tabPage_Station";
		this.tabPage_Station.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage_Station.Size = new System.Drawing.Size(1134, 565);
		this.tabPage_Station.TabIndex = 1;
		this.tabPage_Station.Text = "Station";
		this.groupBox_Options.Controls.Add(this.groupBox_Scheduling);
		this.groupBox_Options.Controls.Add(this.groupBox_VidPid);
		this.groupBox_Options.Location = new System.Drawing.Point(12, 431);
		this.groupBox_Options.Name = "groupBox_Options";
		this.groupBox_Options.Size = new System.Drawing.Size(1113, 124);
		this.groupBox_Options.TabIndex = 6;
		this.groupBox_Options.TabStop = false;
		this.groupBox_Options.Text = "Options";
		this.groupBox_Scheduling.Controls.Add(this.checkBox_StartQSPROnSetup);
		this.groupBox_Scheduling.Controls.Add(this.label_Mode);
		this.groupBox_Scheduling.Controls.Add(this.comboBox_Mode);
		this.groupBox_Scheduling.Location = new System.Drawing.Point(802, 14);
		this.groupBox_Scheduling.Name = "groupBox_Scheduling";
		this.groupBox_Scheduling.Size = new System.Drawing.Size(300, 100);
		this.groupBox_Scheduling.TabIndex = 1;
		this.groupBox_Scheduling.TabStop = false;
		this.groupBox_Scheduling.Text = "Scheduling";
		this.checkBox_StartQSPROnSetup.AutoSize = true;
		this.checkBox_StartQSPROnSetup.Location = new System.Drawing.Point(6, 63);
		this.checkBox_StartQSPROnSetup.Name = "checkBox_StartQSPROnSetup";
		this.checkBox_StartQSPROnSetup.Size = new System.Drawing.Size(148, 17);
		this.checkBox_StartQSPROnSetup.TabIndex = 2;
		this.checkBox_StartQSPROnSetup.Text = "Start QSPR On Setup";
		this.checkBox_StartQSPROnSetup.UseVisualStyleBackColor = true;
		this.label_Mode.AutoSize = true;
		this.label_Mode.Location = new System.Drawing.Point(6, 20);
		this.label_Mode.Name = "label_Mode";
		this.label_Mode.Size = new System.Drawing.Size(38, 13);
		this.label_Mode.TabIndex = 1;
		this.label_Mode.Text = "Mode";
		this.comboBox_Mode.Enabled = false;
		this.comboBox_Mode.FormattingEnabled = true;
		this.comboBox_Mode.Items.AddRange(new object[2] { "Plug And Play", "RoundRobin Scheduling" });
		this.comboBox_Mode.Location = new System.Drawing.Point(9, 36);
		this.comboBox_Mode.Name = "comboBox_Mode";
		this.comboBox_Mode.Size = new System.Drawing.Size(282, 21);
		this.comboBox_Mode.TabIndex = 0;
		this.comboBox_Mode.SelectedIndexChanged += new System.EventHandler(comboBox_Mode_SelectedIndexChanged);
		this.groupBox_VidPid.Controls.Add(this.button_SetVidPid);
		this.groupBox_VidPid.Controls.Add(this.vidPidView);
		this.groupBox_VidPid.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.groupBox_VidPid.Location = new System.Drawing.Point(6, 14);
		this.groupBox_VidPid.Name = "groupBox_VidPid";
		this.groupBox_VidPid.Size = new System.Drawing.Size(790, 100);
		this.groupBox_VidPid.TabIndex = 0;
		this.groupBox_VidPid.TabStop = false;
		this.groupBox_VidPid.Text = "VID / PID";
		this.button_SetVidPid.Location = new System.Drawing.Point(429, 21);
		this.button_SetVidPid.Name = "button_SetVidPid";
		this.button_SetVidPid.Size = new System.Drawing.Size(355, 73);
		this.button_SetVidPid.TabIndex = 1;
		this.button_SetVidPid.Text = "Set VID PID to Ignore HW Serial Number (Only for USB 2.0)";
		this.button_SetVidPid.UseVisualStyleBackColor = true;
		this.button_SetVidPid.Click += new System.EventHandler(button_SetVidPid_Click);
		this.vidPidView.AllowUserToResizeColumns = false;
		this.vidPidView.AllowUserToResizeRows = false;
		this.vidPidView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.vidPidView.Location = new System.Drawing.Point(6, 20);
		this.vidPidView.Name = "vidPidView";
		this.vidPidView.Size = new System.Drawing.Size(417, 74);
		this.vidPidView.TabIndex = 0;
		this.groupBox_ConfigControls.Controls.Add(this.qmsctConfiguration);
		this.groupBox_ConfigControls.Location = new System.Drawing.Point(6, 9);
		this.groupBox_ConfigControls.Name = "groupBox_ConfigControls";
		this.groupBox_ConfigControls.Size = new System.Drawing.Size(1119, 180);
		this.groupBox_ConfigControls.TabIndex = 4;
		this.groupBox_ConfigControls.TabStop = false;
		this.groupBox_ConfigControls.Text = "General";
		this.qmsctConfiguration.Location = new System.Drawing.Point(6, 19);
		this.qmsctConfiguration.Name = "qmsctConfiguration";
		this.qmsctConfiguration.QMSCTMode = QC.QMSCT.QMSCTUserMode.Production;
		this.qmsctConfiguration.Size = new System.Drawing.Size(1107, 158);
		this.qmsctConfiguration.TabIndex = 0;
		this.deviceSetup.DetectionInstance = QC.QMSCT.DetectionInstance.DETECT_INST_PORT_NO;
		this.deviceSetup.DualPortSolution = false;
		this.deviceSetup.Location = new System.Drawing.Point(6, 171);
		this.deviceSetup.ManualSetup = true;
		this.deviceSetup.Name = "deviceSetup";
		this.deviceSetup.Size = new System.Drawing.Size(1124, 270);
		this.deviceSetup.TabIndex = 5;
		this.deviceSetup.TotalNumberOfPanels = 0;
		this.groupBox_SaveOrExit.Controls.Add(this.button_SaveAsFile);
		this.groupBox_SaveOrExit.Controls.Add(this.button_Exit);
		this.groupBox_SaveOrExit.Controls.Add(this.button_Save);
		this.groupBox_SaveOrExit.Location = new System.Drawing.Point(738, 658);
		this.groupBox_SaveOrExit.Name = "groupBox_SaveOrExit";
		this.groupBox_SaveOrExit.Size = new System.Drawing.Size(416, 40);
		this.groupBox_SaveOrExit.TabIndex = 7;
		this.groupBox_SaveOrExit.TabStop = false;
		this.button_SaveAsFile.Location = new System.Drawing.Point(143, 11);
		this.button_SaveAsFile.Name = "button_SaveAsFile";
		this.button_SaveAsFile.Size = new System.Drawing.Size(131, 23);
		this.button_SaveAsFile.TabIndex = 6;
		this.button_SaveAsFile.Text = "Save As...";
		this.button_SaveAsFile.UseVisualStyleBackColor = true;
		this.button_SaveAsFile.Click += new System.EventHandler(button_SaveAsFile_Click);
		this.button_Exit.Location = new System.Drawing.Point(280, 11);
		this.button_Exit.Name = "button_Exit";
		this.button_Exit.Size = new System.Drawing.Size(131, 23);
		this.button_Exit.TabIndex = 5;
		this.button_Exit.Text = "Cancel";
		this.button_Exit.UseVisualStyleBackColor = true;
		this.button_Exit.Click += new System.EventHandler(button_Exit_Click);
		this.button_Save.Location = new System.Drawing.Point(6, 11);
		this.button_Save.Name = "button_Save";
		this.button_Save.Size = new System.Drawing.Size(131, 23);
		this.button_Save.TabIndex = 3;
		this.button_Save.Text = "Save";
		this.button_Save.UseVisualStyleBackColor = true;
		this.button_Save.Click += new System.EventHandler(button_Save_Click);
		this.txtConfigRootFolder.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.txtConfigRootFolder.Location = new System.Drawing.Point(136, 25);
		this.txtConfigRootFolder.Name = "txtConfigRootFolder";
		this.txtConfigRootFolder.ReadOnly = true;
		this.txtConfigRootFolder.Size = new System.Drawing.Size(1018, 20);
		this.txtConfigRootFolder.TabIndex = 8;
		this.lblRootFolder.AutoSize = true;
		this.lblRootFolder.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.lblRootFolder.Location = new System.Drawing.Point(13, 28);
		this.lblRootFolder.Name = "lblRootFolder";
		this.lblRootFolder.Size = new System.Drawing.Size(117, 13);
		this.lblRootFolder.TabIndex = 9;
		this.lblRootFolder.Text = "Config Root Folder:";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.AutoSize = true;
		base.ClientSize = new System.Drawing.Size(1170, 705);
		base.Controls.Add(this.lblRootFolder);
		base.Controls.Add(this.txtConfigRootFolder);
		base.Controls.Add(this.groupBox_SaveOrExit);
		base.Controls.Add(this.tabControl_Configuration);
		base.Controls.Add(this.groupBox_SetUp);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.MaximizeBox = false;
		base.Name = "AppConfiguration";
		base.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
		this.Text = "Application Configuration";
		base.Load += new System.EventHandler(AppConfiguration_Load);
		this.tabControl_Configuration.ResumeLayout(false);
		this.tabPage_Product.ResumeLayout(false);
		this.tabPage_Station.ResumeLayout(false);
		this.groupBox_Options.ResumeLayout(false);
		this.groupBox_Scheduling.ResumeLayout(false);
		this.groupBox_Scheduling.PerformLayout();
		this.groupBox_VidPid.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.vidPidView).EndInit();
		this.groupBox_ConfigControls.ResumeLayout(false);
		this.groupBox_SaveOrExit.ResumeLayout(false);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
