using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using QC.QMSLPhone;

namespace QFDT_Components;

public partial class FactoryDataTracking : UserControl, IComponentConnector
{
	private delegate void PrintInfoMessageCB(uint time, ushort logLevel, string msg);

	public delegate void SetButtonEnableCB(bool bEnable);

	public const int STARTID_PCB_HW_NUMBER = 0;

	public const int ENDID_PCB_HW_NUMBER = 19;

	public const int STARTID_ASSY_HW_NUMBER = 25;

	public const int ENDID_ASSY_HW_NUMBER = 44;

	public const int STARTID_MMI_SW_VERSION = 50;

	public const int ENDID_MMI_SW_VERSION = 69;

	public const int STARTID_AMSS_SW_VERSION = 75;

	public const int ENDID_AMSS_SW_VERSION = 94;

	public const int STARTID_CUSTOMER_DEFINED = 100;

	public const int ENDID_CUSTOMER_DEFINE = 119;

	public Phone phone;

	private Thread thread;

	private Factory_Info_fetch_method_enum fetchType;

	private string remotePath;

	public FactoryDataTracking()
	{
		InitializeComponent();
		SetLanguageDictionary();
		InitInterface();
	}

	private void SetLanguageDictionary()
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		string text = Thread.CurrentThread.CurrentCulture.ToString();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_" + text + ".xaml", UriKind.Absolute);
		}
		catch
		{
		}
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}

	public void SetLanguage(string culture)
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "\\Resources\\UI_" + culture + ".xaml", UriKind.Absolute);
		}
		catch
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_en-US.xaml", UriKind.Absolute);
		}
		base.Resources.MergedDictionaries.Clear();
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}

	private void InitInterface()
	{
		methodComboBox.SelectedIndex = 0;
		txtRemotePath.Text = "FactoryTracking.txt";
		UpdateFileNameVisibleAccordingToMethod();
		fetchType = Factory_Info_fetch_method_enum.NV;
		txtDebugMessage.Document.Blocks.Clear();
	}

	public void PrintInfoMessage(uint time, ushort logLevel, string msg)
	{
		if (txtDebugMessage.Dispatcher.CheckAccess())
		{
			new Run(msg + "\r\n", txtDebugMessage.CaretPosition.DocumentEnd);
			txtDebugMessage.ScrollToEnd();
			return;
		}
		PrintInfoMessageCB method = PrintInfoMessage;
		base.Dispatcher.BeginInvoke(method, time, logLevel, msg);
	}

	public void SetFactoryButtonEnable(bool bEnable)
	{
		if (btGetFactoryInfo.Dispatcher.CheckAccess())
		{
			btGetFactoryInfo.IsEnabled = bEnable;
			return;
		}
		SetButtonEnableCB method = SetFactoryButtonEnable;
		base.Dispatcher.BeginInvoke(method, bEnable);
	}

	private string getNVString(nv_items_enum_type nvItem)
	{
		string text = null;
		try
		{
			byte[] array = new byte[128];
			phone.NVRead(nvItem, array, 128);
			char[] array2 = new char[128];
			for (int i = 0; i < 128; i++)
			{
				array2[i] = (char)array[i];
			}
			return new string(array2);
		}
		catch (PhoneException ex)
		{
			throw ex;
		}
		catch (Exception ex2)
		{
			throw ex2;
		}
	}

	private string getSubStringBeforeNullChar(string strInput)
	{
		string text = null;
		int length = strInput.IndexOf("\0");
		return strInput.Substring(0, length);
	}

	private void UpdateFileNameVisibleAccordingToMethod()
	{
		lblInfoFileName.Visibility = Visibility.Collapsed;
		txtRemotePath.Visibility = Visibility.Collapsed;
		if (methodComboBox.SelectedIndex == 1)
		{
			lblInfoFileName.Visibility = Visibility.Visible;
			txtRemotePath.Visibility = Visibility.Visible;
		}
	}

	private void Method_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		UpdateFileNameVisibleAccordingToMethod();
	}

	private void getInfoByNV()
	{
		try
		{
			PrintInfoMessage(0u, 0, "[SN]");
			string nVString = getNVString(nv_items_enum_type.NV_FACTORY_DATA_1_I);
			string subStringBeforeNullChar = getSubStringBeforeNullChar(nVString);
			string[] array = subStringBeforeNullChar.Split(',');
			int num = array.Length;
			if (num > 0)
			{
				for (int i = 0; i < num; i++)
				{
					string msg = "SN_" + (i + 1) + " = " + array[i];
					PrintInfoMessage(0u, 0, msg);
				}
			}
			PrintInfoMessage(0u, 0, " ");
			PrintInfoMessage(0u, 0, "[MCN]");
			string nVString2 = getNVString(nv_items_enum_type.NV_FACTORY_DATA_2_I);
			int length = 20;
			string strInput = nVString2.Substring(0, length);
			string subStringBeforeNullChar2 = getSubStringBeforeNullChar(strInput);
			if (subStringBeforeNullChar2 != "")
			{
				string msg2 = "HW_PCB_MCN = " + subStringBeforeNullChar2;
				PrintInfoMessage(0u, 0, msg2);
			}
			PrintInfoMessage(0u, 0, " ");
			PrintInfoMessage(0u, 0, "[Station_Flags]");
			string nVString3 = getNVString(nv_items_enum_type.NV_FACTORY_DATA_3_I);
			string subStringBeforeNullChar3 = getSubStringBeforeNullChar(nVString3);
			PrintInfoMessage(0u, 0, subStringBeforeNullChar3);
			PrintInfoMessage(0u, 0, " ");
			PrintInfoMessage(0u, 0, "[Error Code]");
			string nVString4 = getNVString(nv_items_enum_type.NV_FACTORY_DATA_4_I);
			string subStringBeforeNullChar4 = getSubStringBeforeNullChar(nVString4);
			PrintInfoMessage(0u, 0, subStringBeforeNullChar4);
			PrintInfoMessage(0u, 0, " ");
		}
		catch (PhoneException ex)
		{
			PrintInfoMessage(0u, 0, "PhoneException " + ex.Message);
		}
		catch (Exception ex2)
		{
			PrintInfoMessage(0u, 0, "Unkonwn Exception " + ex2.Message);
		}
	}

	private void parseFactoryTrackingFile(string filePath)
	{
		StreamReader streamReader = new StreamReader(filePath, Encoding.Default);
		string msg;
		while ((msg = streamReader.ReadLine()) != null)
		{
			PrintInfoMessage(0u, 0, msg);
		}
	}

	private void getInfoByAP()
	{
		try
		{
			string text = "c:\\Qualcomm\\QDART\\QFDT\\factory_tracking.txt";
			ushort iFTM_Error_Code = 0;
			phone.FTM_AP_ReadFile(ref iFTM_Error_Code, remotePath, text);
			parseFactoryTrackingFile(text);
		}
		catch (PhoneException ex)
		{
			PrintInfoMessage(0u, 0, "PhoneException " + ex.Message);
		}
		catch (Exception ex2)
		{
			PrintInfoMessage(0u, 0, "Unkonwn Exception " + ex2.Message);
		}
	}

	private void getInfo(Factory_Info_fetch_method_enum getType)
	{
		switch (getType)
		{
		case Factory_Info_fetch_method_enum.NV:
			getInfoByNV();
			break;
		case Factory_Info_fetch_method_enum.AP:
			getInfoByAP();
			break;
		}
	}

	private void runGetFactoryInfo()
	{
		if (phone != null)
		{
			getInfo(fetchType);
		}
		else
		{
			PrintInfoMessage(0u, 0, "Can't get factory infomation due to incorrect phone HANDLE");
		}
		SetFactoryButtonEnable(bEnable: true);
	}

	private void GetFactoryInfo_Click(object sender, RoutedEventArgs e)
	{
		txtDebugMessage.Document.Blocks.Clear();
		remotePath = txtRemotePath.Text;
		if (methodComboBox.SelectedIndex == 0)
		{
			fetchType = Factory_Info_fetch_method_enum.NV;
		}
		else if (methodComboBox.SelectedIndex == 1)
		{
			fetchType = Factory_Info_fetch_method_enum.AP;
		}
		SetFactoryButtonEnable(bEnable: false);
		thread = new Thread(runGetFactoryInfo);
		thread.Name = "getFactoryInfo";
		thread.Start();
	}
}
