using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using Microsoft.Win32;
using QC.QMSLPhone;

namespace QFDT_Components;

public partial class FTM_FFBM : UserControl, IComponentConnector
{
	public delegate void SendDebugMessage(MessageLevel level, string message);

	public SendDebugMessage DebugMessageCB;

	public Phone phone;

	private List TestList;

	public string[] TestingList;

	public string[] DirectoryList;

	public bool InFFBMMode;

	private string sLocalfilepath = "C:\\Qualcomm\\QDART\\QFDT\\";

	private ushort iTimeOutInS;

	private uint iTestStatus;

	private byte bTimeOutOccurred;

	private string CurrentTest = "";

	private ushort iFTM_Error_Code;

	private byte CurrentBootMode;

	private byte CurrentBootSubMode;

	private byte NextBootMode;

	private byte NextBootSubMode;

	private ushort iNumTest;

	private byte iState;

	private uint FailCount;

	private string ToRead = "";

	private bool FileWasRead;

	public FTM_FFBM()
	{
		InitializeComponent();
		SetLanguageDictionary();
	}

	public void Register_DebugMessageCB(SendDebugMessage me)
	{
		DebugMessageCB = me;
	}

	private void Test_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (Test_List_ComboBox.Items.Count != 0)
		{
			CurrentTest = TestingList[Test_List_ComboBox.SelectedIndex];
		}
	}

	private void ClearAllTextBoxes()
	{
		Wait.Text = "";
		State.Text = "";
		Fail_Count.Text = "";
	}

	public void SetupUI(string[] TestingList)
	{
		Test_List_ComboBox.Items.Clear();
		for (int i = 0; i < TestingList.Length; i++)
		{
			Test_List_ComboBox.Items.Add(TestingList[i]);
		}
	}

	public void GetTestList()
	{
		ushort num = 0;
		string text = "C:\\Qualcomm\\QDART\\QFDT\\testlist.txt";
		if (phone != null)
		{
			phone.FTM_AP_Test_List_To_File(ref num);
			phone.FTM_AP_ReadFile(ref num, "testlist.txt", text);
			TestingList = null;
			TestingList = File.ReadAllLines(text);
			SetupUI(TestingList);
		}
	}

	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 Single_Test_Click(object sender, RoutedEventArgs e)
	{
		if (Test_List_ComboBox.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectTestCase"].ToString());
		}
		else
		{
			if (phone == null)
			{
				return;
			}
			try
			{
				if (CurrentBootMode == 1)
				{
					if (string.IsNullOrWhiteSpace(Wait.Text))
					{
						DebugMessageCB(MessageLevel.FAIL, base.Resources["EnterWaitTimeout"].ToString());
						return;
					}
					if (Wait.Text == "0")
					{
						phone.FTM_AP_Execute_Single_Test(CurrentTest, ref iFTM_Error_Code);
						return;
					}
					iTimeOutInS = Convert.ToUInt16(Wait.Text);
					phone.FTM_AP_Execute_Single_Test_WithWait(CurrentTest, ref iFTM_Error_Code, iTimeOutInS, ref iTestStatus, ref bTimeOutOccurred);
					if (bTimeOutOccurred == 1)
					{
						DebugMessageCB(MessageLevel.FAIL, base.Resources["TimeOut"].ToString());
					}
				}
				else
				{
					DebugMessageCB(MessageLevel.FAIL, base.Resources["NotinFFBM"].ToString());
				}
			}
			catch (Exception ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
			}
		}
	}

	private void GetTestList_Click(object sender, RoutedEventArgs e)
	{
		if (phone != null)
		{
			try
			{
				phone.FFBM_GET_MODE(ref iFTM_Error_Code, ref CurrentBootMode, ref CurrentBootSubMode, ref NextBootMode, ref NextBootSubMode);
				if (CurrentBootMode == 1)
				{
					GetTestList();
				}
				else
				{
					DebugMessageCB(MessageLevel.FAIL, base.Resources["NotinFFBM"].ToString());
				}
				return;
			}
			catch (Exception ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
				return;
			}
		}
		DebugMessageCB(MessageLevel.FAIL, base.Resources["PhoneNot"].ToString());
	}

	private void AllTests_Click(object sender, RoutedEventArgs e)
	{
		if (phone == null)
		{
			return;
		}
		if (CurrentBootMode == 1)
		{
			try
			{
				phone.FTM_AP_Execute_All_Tests(ref iFTM_Error_Code);
				DebugMessageCB(MessageLevel.SUCCESS, base.Resources["ExecutingTests"].ToString());
				return;
			}
			catch (Exception ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
				return;
			}
		}
		DebugMessageCB(MessageLevel.FAIL, base.Resources["NotinFFBM"].ToString());
	}

	private void Display_Click(object sender, RoutedEventArgs e)
	{
		string text = "C:\\Qualcomm\\QDART\\QFDT\\directory.txt";
		if (phone == null)
		{
			return;
		}
		try
		{
			phone.FFBM_GET_MODE(ref iFTM_Error_Code, ref CurrentBootMode, ref CurrentBootSubMode, ref NextBootMode, ref NextBootSubMode);
			if (CurrentBootMode == 1)
			{
				phone.FTM_AP_Dir_To_File(ref iFTM_Error_Code);
				phone.FTM_AP_ReadFile(ref iFTM_Error_Code, "directory.txt", text);
				DirectoryList = File.ReadAllLines(text);
				DirectoryBox.Items.Clear();
				for (int i = 0; i < DirectoryList.Length; i++)
				{
					DirectoryBox.Items.Add(DirectoryList[i]);
				}
			}
			else
			{
				DebugMessageCB(MessageLevel.FAIL, base.Resources["NotinFFBM"].ToString());
			}
		}
		catch (Exception ex)
		{
			DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
		}
	}

	private void Test_List_ComboBox_DropDownOpened(object sender, EventArgs e)
	{
		GetTestList_Click(null, null);
	}

	private void AppStatus_Click(object sender, RoutedEventArgs e)
	{
		if (phone == null)
		{
			return;
		}
		try
		{
			phone.FFBM_GET_MODE(ref iFTM_Error_Code, ref CurrentBootMode, ref CurrentBootSubMode, ref NextBootMode, ref NextBootSubMode);
			if (CurrentBootMode == 1)
			{
				phone.FTM_AP_TEST_APP_STATUS(ref iFTM_Error_Code, ref iState, ref FailCount);
				State.Text = iState.ToString();
				Fail_Count.Text = FailCount.ToString();
			}
			else
			{
				DebugMessageCB(MessageLevel.FAIL, base.Resources["NotinFFBM"].ToString());
			}
		}
		catch (Exception ex)
		{
			DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
		}
	}

	private void DirectoryBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (DirectoryBox.SelectedIndex != -1)
		{
			SelectedSequence.Text = DirectoryList[DirectoryBox.SelectedIndex];
			ReadTextBox.Text = sLocalfilepath + DirectoryList[DirectoryBox.SelectedIndex];
			ToRead = DirectoryList[DirectoryBox.SelectedIndex];
		}
	}

	private void SelectSequence_Click(object sender, RoutedEventArgs e)
	{
		if (string.IsNullOrWhiteSpace(SelectedSequence.Text))
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectSequence"].ToString());
			return;
		}
		phone.FTM_AP_SelectSequence(SelectedSequence.Text, ref iFTM_Error_Code, ref iNumTest);
		if (iFTM_Error_Code == 0)
		{
			DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SequenceLoaded"].ToString());
		}
		else
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["UnableSequence"].ToString());
		}
	}

	private void ReadFile_Click(object sender, RoutedEventArgs e)
	{
		if (phone == null)
		{
			return;
		}
		try
		{
			if (!string.IsNullOrWhiteSpace(ReadTextBox.Text))
			{
				phone.FTM_AP_ReadFile(ref iFTM_Error_Code, ToRead, ReadTextBox.Text);
				DebugMessageCB(MessageLevel.SUCCESS, base.Resources["FileWasRead"].ToString() + ": " + ReadTextBox.Text);
				FileWasRead = true;
			}
			else
			{
				DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectFile"].ToString());
			}
		}
		catch (Exception ex)
		{
			DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
		}
	}

	private void View_Click(object sender, RoutedEventArgs e)
	{
		if (FileWasRead)
		{
			Process.Start(ReadTextBox.Text);
			FileWasRead = false;
		}
		else
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["FileNotRead"].ToString());
		}
	}

	private void BrowseWriteFiles_Click(object sender, RoutedEventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		if (openFileDialog.ShowDialog() == true)
		{
			string fileName = openFileDialog.FileName;
			WriteTextBox.Text = fileName;
		}
	}

	private void WriteFile_Click(object sender, RoutedEventArgs e)
	{
		if (phone == null)
		{
			return;
		}
		try
		{
			if (!string.IsNullOrWhiteSpace(WriteTextBox.Text))
			{
				string fileName = Path.GetFileName(WriteTextBox.Text);
				phone.FTM_AP_WriteFile(ref iFTM_Error_Code, fileName, WriteTextBox.Text);
				DebugMessageCB(MessageLevel.SUCCESS, base.Resources["FileWrittenATP"].ToString());
			}
			else
			{
				DebugMessageCB(MessageLevel.FAIL, base.Resources["FileWriteATP"].ToString());
			}
		}
		catch (Exception ex)
		{
			DebugMessageCB(MessageLevel.EXCEPTION, ex.Message);
		}
	}
}
