using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using QC.QMSLPhone;

namespace QFDT_Components;

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

	public delegate void EnableBackupButtonCB(bool bEnable);

	public delegate void EnableRestoreButtonCB(bool bEnable);

	public delegate void EnableCompareButtonCB(bool bEnable);

	private const string m_RefQCNXML = "C:\\Qualcomm\\QDART\\QFDT\\ReferenceQCN.xml";

	private const string m_CurrentQCNPath = "C:\\Qualcomm\\QDART\\QFDT\\backup.qcn";

	private const string m_CurrentXMLPath = "C:\\Qualcomm\\QDART\\QFDT\\CurrentQCN.xml";

	private const string m_SummaryXMLPath = "C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.xml";

	private const string m_SummaryHTMLPath = "C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.html";

	public SendDebugMessage DebugMessageCB;

	public Phone phone;

	private Thread thread;

	private string backupNVPath;

	private string restoreNVPath;

	private string m_RefQCNPath = "";

	public Non_Volatile_Items_Management()
	{
		InitializeComponent();
	}

	private void Compare()
	{
	}

	private void BackupNVsIntoQCN()
	{
		if (phone != null)
		{
			try
			{
				DebugMessageCB(MessageLevel.SUCCESS, "Backing up NV items into QCN.  Please wait...");
				phone.CreateQCNFile(backupNVPath, "000000", out var _);
				EnableBackupButton(Enable: true);
				EnableRestoreButton(Enable: true);
				DebugMessageCB(MessageLevel.SUCCESS, "Backing up NV items into QCN completed");
			}
			catch (PhoneException)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Failed to backing up NV items into QCN");
				EnableBackupButton(Enable: true);
				EnableRestoreButton(Enable: true);
			}
		}
	}

	private void RestoreNVsIntoQCN()
	{
		if (phone != null)
		{
			try
			{
				DebugMessageCB(MessageLevel.SUCCESS, "Restoring NV items into mobile.  Please wait...");
				phone.RestoreQCNFile(restoreNVPath, "000000", bSkipReset: true, out var _);
				EnableBackupButton(Enable: true);
				EnableRestoreButton(Enable: true);
				DebugMessageCB(MessageLevel.SUCCESS, "Restoring NV items into mobile completed.");
			}
			catch (PhoneException)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Failed to restore NV items into mobile");
				EnableBackupButton(Enable: true);
				EnableRestoreButton(Enable: true);
			}
		}
	}

	public void EnableBackupButton(bool Enable)
	{
		if (bnBackup.Dispatcher.CheckAccess())
		{
			bnBackup.IsEnabled = Enable;
			return;
		}
		EnableBackupButtonCB method = EnableBackupButton;
		base.Dispatcher.BeginInvoke(method, Enable);
	}

	public void EnableRestoreButton(bool Enable)
	{
		if (bnRestore.Dispatcher.CheckAccess())
		{
			bnRestore.IsEnabled = Enable;
			return;
		}
		EnableRestoreButtonCB method = EnableRestoreButton;
		base.Dispatcher.BeginInvoke(method, Enable);
	}

	public void EnableCompareButton(bool Enable)
	{
		if (bnRestore.Dispatcher.CheckAccess())
		{
			bnCompare.IsEnabled = Enable;
			return;
		}
		EnableCompareButtonCB method = EnableCompareButton;
		base.Dispatcher.BeginInvoke(method, Enable);
	}

	private void bnBackup_Click(object sender, RoutedEventArgs e)
	{
		if (phone != null)
		{
			thread = new Thread(BackupNVsIntoQCN);
			thread.Name = "Backup NVs";
			bnBackup.IsEnabled = false;
			backupNVPath = txtBackupPath.Text;
			EnableRestoreButton(Enable: false);
			thread.Start();
		}
	}

	private void bnRestore_Click(object sender, RoutedEventArgs e)
	{
		if (phone != null)
		{
			thread = new Thread(RestoreNVsIntoQCN);
			thread.Name = "Restore NVs";
			bnRestore.IsEnabled = false;
			restoreNVPath = txtRestorePath.Text;
			EnableBackupButton(Enable: false);
			thread.Start();
		}
	}

	public void SetReferenceQCNPath(string sRefQCNPath)
	{
		if (sRefQCNPath.Length > 0)
		{
			m_RefQCNPath = sRefQCNPath;
			EnableCompareButton(Enable: true);
		}
		else
		{
			m_RefQCNPath = "";
			EnableCompareButton(Enable: false);
		}
	}

	private void Compare_V2()
	{
		if (phone != null)
		{
			try
			{
				int nvItemsCount = 0;
				int resultCode = 0;
				string ErrorMessage = "";
				DebugMessageCB(MessageLevel.SUCCESS, "Comparing NV items.  Please wait...");
				DebugMessageCB(MessageLevel.SUCCESS, "Loading NV items from Reference QCN.  Please wait...");
				phone.NV_ClearNVDefinitionFile();
				phone.NV_LoadNVsFromQCN(m_RefQCNPath, ref nvItemsCount, out resultCode);
				phone.NV_WriteNVsToSource("C:\\Qualcomm\\QDART\\QFDT\\ReferenceQCN.xml", ref resultCode);
				DebugMessageCB(MessageLevel.SUCCESS, "Loading NV items from Reference QCN completed");
				phone.NV_ClearNVDefinitionFile();
				DebugMessageCB(MessageLevel.SUCCESS, "Backing up NV items into QCN.  Please wait...");
				phone.CreateQCNFile("C:\\Qualcomm\\QDART\\QFDT\\backup.qcn", "000000", out ErrorMessage);
				DebugMessageCB(MessageLevel.SUCCESS, "Backing up NV items into QCN completed");
				DebugMessageCB(MessageLevel.SUCCESS, "Loading NV items data from QCN.  Please wait...");
				phone.NV_LoadNVsFromQCN("C:\\Qualcomm\\QDART\\QFDT\\backup.qcn", ref nvItemsCount, out resultCode);
				phone.NV_WriteNVsToSource("C:\\Qualcomm\\QDART\\QFDT\\CurrentQCN.xml", ref resultCode);
				DebugMessageCB(MessageLevel.SUCCESS, "Loading NV items data from QCN completed");
				NVDiff nVDiff = new NVDiff();
				DebugMessageCB(MessageLevel.SUCCESS, "Comparing Items. Please wait...");
				nVDiff.Compare("C:\\Qualcomm\\QDART\\QFDT\\ReferenceQCN.xml", "C:\\Qualcomm\\QDART\\QFDT\\CurrentQCN.xml");
				nVDiff.CreateSummary("C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.xml");
				string stylesheetPath = "C:\\Qualcomm\\QDART\\QFDT\\QCNDiffResult.xsl";
				nVDiff.TransformXMLtoHTML("C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.xml", "C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.html", stylesheetPath);
				DebugMessageCB(MessageLevel.SUCCESS, "Transforming result into HTML completed");
				Process process = new Process();
				process.StartInfo.FileName = "C:\\Qualcomm\\QDART\\QFDT\\DiffSummary.html";
				process.Start();
				EnableCompareButton(Enable: true);
			}
			catch (PhoneException)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Failed to compare NV items");
				EnableCompareButton(Enable: true);
			}
		}
	}

	private void bnCompare_Click(object sender, RoutedEventArgs e)
	{
		if (phone != null)
		{
			thread = new Thread(Compare_V2);
			thread.Name = "Comapre NV";
			bnCompare.IsEnabled = false;
			thread.Start();
		}
	}

	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);
	}
}
