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

namespace QFDT_Components;

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

	public Phone phone;

	public SendDebugMessage DebugMessageCB;

	public ModeSelection()
	{
		InitializeComponent();
	}

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

	private void bnAPSSBootMode_Click(object sender, RoutedEventArgs e)
	{
		ushort iFTM_Error_Code = 0;
		if (cboAPSSBootMode.SelectedIndex == -1)
		{
			MessageBox.Show("Please select a boot mode first");
		}
		else
		{
			if (phone == null)
			{
				return;
			}
			try
			{
				if (cboAPSSBootMode.SelectedIndex == 0)
				{
					phone.FFBM_SET_MODE(0, 0, ref iFTM_Error_Code);
					DebugMessageCB(MessageLevel.SUCCESS, "Set HLOS Mode Success");
				}
				else
				{
					phone.FFBM_SET_MODE(1, 0, ref iFTM_Error_Code);
					DebugMessageCB(MessageLevel.SUCCESS, "Set FFBM Mode Success");
				}
				if (MessageBox.Show("Do you want to reset the phone now? ", "DebugTool", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
				{
					phone.ModeControl(mode_enum_type.MODE_RESET_F, 1000);
					DebugMessageCB(MessageLevel.SUCCESS, "Reset Device");
				}
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
			}
		}
	}

	private void bnModemMode_Click(object sender, RoutedEventArgs e)
	{
		if (cboMPSSBootMode.SelectedIndex == -1)
		{
			MessageBox.Show("Please select a boot mode first");
		}
		if (phone == null)
		{
			return;
		}
		try
		{
			phone.ModeControl(mode_enum_type.MODE_OFFLINE_D_F, 0);
			DebugMessageCB(MessageLevel.SUCCESS, "Set Offline mode");
			if (cboMPSSBootMode.SelectedIndex == 1)
			{
				byte[] buffer = new byte[1] { 1 };
				phone.NVWrite(nv_items_enum_type.NV_FTM_MODE_I, buffer, 1);
				DebugMessageCB(MessageLevel.SUCCESS, "Write NV 453 to 1");
			}
			else
			{
				byte[] array = new byte[1];
				byte[] buffer2 = array;
				phone.NVWrite(nv_items_enum_type.NV_FTM_MODE_I, buffer2, 1);
				DebugMessageCB(MessageLevel.SUCCESS, "Write NV 453 to 0");
			}
			phone.EFS_SyncWithWait(2000);
			if (MessageBox.Show("Do you want to reset the phone now? ", "DebugTool", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
			{
				phone.ModeControl(mode_enum_type.MODE_RESET_F, 1000);
				DebugMessageCB(MessageLevel.SUCCESS, "Reset Device");
			}
		}
		catch (PhoneException ex)
		{
			DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
		}
	}
}
