using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using DebugToolInstrumentInterface;
using QC.QMSLPhone;
using Qualcomm_ModemRFA;

namespace QFDT_Components;

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

	public delegate string GetTextFromTextBoxCB(TextBox tbx);

	public delegate bool GetCheckBoxIsCheckedCB();

	public delegate int? GetComboBoxSelectedIndexCB();

	public delegate void SetComponentAttributeCB<T, T2>(T set_component, SetAttribute attr, T2 value);

	public SendDebugMessage DebugMessageCB;

	public bool bContinueRunFlag = true;

	private bool bChangeSiggenPwr;

	private ushort? m_channel = null;

	private double? m_SGPower = null;

	private double? m_UpperRxPwrLim = null;

	private double? m_LowerRxPwrLim = null;

	private bool bPathIsPRx = true;

	private int? C0Scenario_SelectedIndex = null;

	private int? C1Scenario_SelectedIndex = null;

	public Phone phone;

	private Thread thread;

	public ISignalGenerator pSG;

	public ISpectrumAnalyzer pSA;

	private List<RxPowerAction> C0_List;

	private List<RxPowerAction> C1_List;

	private RxPowerAction currentPowerAction;

	private double Span;

	private Technology mytech;

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

	public FTM_RF_Rx()
	{
		InitializeComponent();
		SetLanguageDictionary();
		Siggen.SelectedIndex = 0;
		SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
	}

	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 SetUpUIviaXML(string calV4XML)
	{
		CalV4Parser calV4Parser = new CalV4Parser();
		calV4Parser.ParseCalV4File(calV4XML);
		List<RxPowerAction> rxPowerActionC0_List = calV4Parser.Get_HighestGainState_RxPowerAction_FromAllTechs(0);
		List<RxPowerAction> rxPowerActionC1_List = calV4Parser.Get_HighestGainState_RxPowerAction_FromAllTechs(1);
		SetUpUI(rxPowerActionC0_List, rxPowerActionC1_List);
	}

	private void ClearAllTextBoxes()
	{
		Channel.Text = "";
		ChanInMHz.Text = "";
		txtLowerLim.Text = "";
		txtUpperLim.Text = "";
	}

	public void SetUpUI(List<RxPowerAction> RxPowerActionC0_List, List<RxPowerAction> RxPowerActionC1_List)
	{
		ClearAllTextBoxes();
		C0_Scenario.Items.Clear();
		if (RxPowerActionC0_List != null)
		{
			for (int i = 0; i < RxPowerActionC0_List.Count; i++)
			{
				C0_Scenario.Items.Add(RxPowerActionC0_List[i].Description);
			}
		}
		C0_List = RxPowerActionC0_List;
		C1_Scenario.Items.Clear();
		if (RxPowerActionC1_List != null)
		{
			for (int j = 0; j < RxPowerActionC1_List.Count; j++)
			{
				C1_Scenario.Items.Add(RxPowerActionC1_List[j].Description);
			}
		}
		C1_List = RxPowerActionC1_List;
	}

	private void C0_Scenario_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		ComboBox comboBox = (ComboBox)sender;
		if (comboBox.Items.Count > 0)
		{
			currentPowerAction = C0_List[C0_Scenario.SelectedIndex];
			Channel.Text = C0_List[C0_Scenario.SelectedIndex].Ref_Chan.ToString();
			CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0_Scenario.SelectedIndex].RFmode_ID);
			if (phone != null)
			{
				uint iChannel = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0_Scenario.SelectedIndex].RFmode_ID), Convert.ToUInt16(Channel.Text));
				double num = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0_Scenario.SelectedIndex].RFmode_ID), iChannel);
				ChanInMHz.Text = num + "MHz";
			}
			DLPower.Text = C0_List[C0_Scenario.SelectedIndex].DLPower.ToString();
			txtLowerLim.Text = ((C0_List[C0_Scenario.SelectedIndex].LowerLim != null) ? C0_List[C0_Scenario.SelectedIndex].LowerLim : "");
			txtUpperLim.Text = ((C0_List[C0_Scenario.SelectedIndex].UpperLim != null) ? C0_List[C0_Scenario.SelectedIndex].UpperLim : "");
		}
	}

	private void C1_Scenario_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		ComboBox comboBox = (ComboBox)sender;
		if (comboBox.Items.Count > 0)
		{
			currentPowerAction = C1_List[C1_Scenario.SelectedIndex];
			Channel.Text = C1_List[C1_Scenario.SelectedIndex].Ref_Chan.ToString();
			CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1_Scenario.SelectedIndex].RFmode_ID);
			if (phone != null)
			{
				uint iChannel = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1_Scenario.SelectedIndex].RFmode_ID), Convert.ToUInt16(Channel.Text));
				double num = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1_Scenario.SelectedIndex].RFmode_ID), iChannel);
				ChanInMHz.Text = num + "MHz";
			}
			DLPower.Text = C1_List[C1_Scenario.SelectedIndex].DLPower.ToString();
			txtLowerLim.Text = ((C1_List[C1_Scenario.SelectedIndex].LowerLim != null) ? C1_List[C1_Scenario.SelectedIndex].LowerLim : "");
			txtUpperLim.Text = ((C1_List[C1_Scenario.SelectedIndex].UpperLim != null) ? C1_List[C1_Scenario.SelectedIndex].UpperLim : "");
		}
	}

	private short GetRxAGC(CalV4Parser.FTM_PHONE_MODE_ENUM RFModeID, double powerdBm)
	{
		short result = 0;
		if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, RFModeID))
		{
			result = (short)(-512 + ((int)(powerdBm * 10.0) + 1154));
		}
		if (CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, RFModeID) || CalV4Parser.IsTech(CalV4Parser.Tech.TDSCDMA, RFModeID))
		{
			result = (short)(-512 + ((int)(powerdBm * 10.0) + 1062));
		}
		return result;
	}

	private double GetPowerFromRxAGC(CalV4Parser.FTM_PHONE_MODE_ENUM RFModeID, short rxAGC)
	{
		double result = 0.0;
		if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, RFModeID))
		{
			result = ((double)rxAGC + 512.0) / 10.0 - 115.4;
		}
		if (CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, RFModeID) || CalV4Parser.IsTech(CalV4Parser.Tech.TDSCDMA, RFModeID))
		{
			result = ((double)rxAGC + 512.0) / 10.0 - 106.2;
		}
		if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, RFModeID))
		{
			result = (double)rxAGC / 10.0;
		}
		return result;
	}

	private void RunContinueMeas()
	{
		if (m_channel.HasValue && m_SGPower.HasValue)
		{
			RxAGCContinueMeasurement(bPathIsPRx, m_SGPower.Value, m_channel.Value);
		}
		else
		{
			DebugMessageCB(MessageLevel.FAIL, "Invalid Power or Channel input!");
		}
		SetCompAttr(DLPower, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(TxOn, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(ContinueTxOn, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(ContinueTxStop, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(TxOff, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(DivContTxOn, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(DivContTxStop, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxOn, SetAttribute.Visibility, bEnable: true);
		SetCompAttr(ContinueTxStop, SetAttribute.Visibility, bEnable: false);
		SetCompAttr(DivContTxOn, SetAttribute.Visibility, bEnable: true);
		SetCompAttr(DivContTxStop, SetAttribute.Visibility, bEnable: false);
	}

	private void RxAGCContinueMeasurement(bool PriRx, double SGPower, ushort channel)
	{
		SetCompAttr(txtRealtimeValue, SetAttribute.Text, "");
		SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		if (phone != null)
		{
			try
			{
				if (GloVar.IsCatM1NBIoT == 1)
				{
					phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_LTE);
					DebugMessageCB(MessageLevel.SUCCESS, "QLIB_FTM_SET_MODE_ID(LTE) success");
					phone.FTM_LTE_SET_HD_FDD(1);
					DebugMessageCB(MessageLevel.SUCCESS, "QLIB_FTM_LTE_SET_HD_FDD(Half Duplex) success");
					Thread.Sleep(100);
				}
				phone.FTM_SET_CAL_MODE(1);
				phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
				phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
				if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
				{
					phone.FTM_LTE_SET_TX_BANDWIDTH(3);
					phone.FTM_LTE_SET_RX_BANDWIDTH(3);
				}
				phone.FTM_SET_CHAN(channel);
				uint num = 0u;
				double num2 = 0.0;
				if (PriRx)
				{
					num = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0Scenario_SelectedIndex.Value].RFmode_ID), channel);
					num2 = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0Scenario_SelectedIndex.Value].RFmode_ID), num);
				}
				else
				{
					num = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1Scenario_SelectedIndex.Value].RFmode_ID), channel);
					num2 = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1Scenario_SelectedIndex.Value].RFmode_ID), num);
				}
				short piRX_AGC = 0;
				if (pSG != null)
				{
					double num3 = num2;
					if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						num3 = num2 + 0.5;
					}
					pSG.SetupGen();
					Thread.Sleep(800);
					pSG.SetPowerLevel(SGPower);
					pSG.EnableOutput(onOff: true);
					Thread.Sleep(800);
					if (pSG.SetCenterFrequency(num3, 0.0))
					{
						DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SigGenat"].ToString() + " " + num3 + "MHz " + base.Resources["Success"].ToString());
					}
					else
					{
						DebugMessageCB(MessageLevel.FAIL, base.Resources["SigGenat"].ToString() + " " + num3 + "MHz " + base.Resources["Fail"].ToString());
					}
					short rxAGC = GetRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, SGPower);
					short piDVGAoffset = 0;
					if (!PriRx)
					{
						phone.FTM_SET_SECONDARY_CHAIN(FTM_SecondaryChainMode.FTM_Secondary_Rx_Diversity);
						if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
							phone.FTM_SET_CHAN(channel);
						}
					}
					double num4 = 0.0;
					if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID) || CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							if (PriRx)
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_CDMA_1X);
							}
							else
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_CDMA_1X_RX1);
							}
							Span = 2.0;
							mytech = Technology.CDMA;
						}
						else if (CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							if (PriRx)
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_WCDMA);
							}
							else
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_WCDMA_RX1);
							}
							Span = 5.0;
							mytech = Technology.WCDMA;
						}
						phone.FTM_GET_DVGA_OFFSET(rxAGC, ref piDVGAoffset);
						phone.FTM_SET_DVGA_OFFSET(piDVGAoffset);
						while (bContinueRunFlag)
						{
							PassLimitFromUIValue(GetTextFromTextBox(txtLowerLim), GetTextFromTextBox(txtUpperLim));
							phone.FTM_GET_RX_AGC(ref piRX_AGC);
							Thread.Sleep(800);
							if (bChangeSiggenPwr)
							{
								bChangeSiggenPwr = false;
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
								int? num5 = null;
								num5 = GetSiggenChangeSelectedIndex();
								if (num5.HasValue)
								{
									double num6 = SGPower;
									int? num7 = num5;
									if (num7.GetValueOrDefault() == 0 && num7.HasValue)
									{
										SGPower -= 3.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else if (num5 == 1)
									{
										SGPower -= 6.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else
									{
										pSG.EnableOutput(onOff: false);
									}
									Thread.Sleep(800);
									SetupSpecAn(mytech, num3, SGPower, Span);
									short piRX_AGC2 = 0;
									phone.FTM_GET_RX_AGC(ref piRX_AGC2);
									int num8 = piRX_AGC - piRX_AGC2;
									double num9 = (double)num8 / 10.0;
									string message = base.Resources["SigGenOnat"].ToString() + " " + num6.ToString() + base.Resources["RxLevel"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC) + base.Resources["SigGenOff"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC2) + " dBm" + base.Resources["Changeis"].ToString() + " " + num9 + "dB";
									DebugMessageCB(MessageLevel.SUCCESS, message);
								}
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: true);
							}
							num4 = GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC);
							SetCompAttr(txtRealtimeValue, SetAttribute.Text, num4.ToString());
							SetCompAttr(lblRealTimeValueUnits, SetAttribute.Text, "(Power: dBm)");
							JudgeRxLevel(num4);
						}
					}
					else if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (PriRx)
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_LTE);
						}
						else
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_LTE_RX1);
						}
						while (bContinueRunFlag)
						{
							PassLimitFromUIValue(GetTextFromTextBox(txtLowerLim), GetTextFromTextBox(txtUpperLim));
							phone.FTM_GET_RX_AGC(ref piRX_AGC);
							Thread.Sleep(800);
							if (bChangeSiggenPwr)
							{
								bChangeSiggenPwr = false;
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
								int? num10 = null;
								num10 = GetSiggenChangeSelectedIndex();
								if (num10.HasValue)
								{
									double num11 = SGPower;
									int? num12 = num10;
									if (num12.GetValueOrDefault() == 0 && num12.HasValue)
									{
										SGPower -= 3.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else if (num10 == 1)
									{
										SGPower -= 6.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else
									{
										pSG.EnableOutput(onOff: false);
									}
									Thread.Sleep(800);
									SetupSpecAn(Technology.LTE, num3, SGPower, 10.0);
									short piRX_AGC3 = 0;
									phone.FTM_GET_RX_AGC(ref piRX_AGC3);
									int num13 = piRX_AGC - piRX_AGC3;
									double num14 = (double)num13 / 10.0;
									string message2 = base.Resources["SigGenOnat"].ToString() + " " + num11.ToString() + base.Resources["RxLevel"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC) + base.Resources["SigGenOff"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC3) + " dBm" + base.Resources["Changeis"].ToString() + " " + num14 + "dB";
									DebugMessageCB(MessageLevel.SUCCESS, message2);
								}
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: true);
							}
							num4 = GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC);
							SetCompAttr(txtRealtimeValue, SetAttribute.Text, num4.ToString());
							SetCompAttr(lblRealTimeValueUnits, SetAttribute.Text, "(Power: dBm)");
							JudgeRxLevel(num4);
						}
					}
					else if (CalV4Parser.IsTech(CalV4Parser.Tech.TDSCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (PriRx)
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_TDSCDMA);
						}
						else
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_TDSCDMA_RX1);
						}
						while (bContinueRunFlag)
						{
							PassLimitFromUIValue(GetTextFromTextBox(txtLowerLim), GetTextFromTextBox(txtUpperLim));
							phone.FTM_GET_RX_AGC(ref piRX_AGC);
							if (bChangeSiggenPwr)
							{
								bChangeSiggenPwr = false;
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
								int? num15 = null;
								num15 = GetSiggenChangeSelectedIndex();
								if (num15.HasValue)
								{
									double num16 = SGPower;
									int? num17 = num15;
									if (num17.GetValueOrDefault() == 0 && num17.HasValue)
									{
										SGPower -= 3.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else if (num15 == 1)
									{
										SGPower -= 6.0;
										pSG.SetPowerLevel(SGPower);
										SetCompAttr(DLPower, SetAttribute.Text, SGPower.ToString());
									}
									else
									{
										pSG.EnableOutput(onOff: false);
									}
									Thread.Sleep(800);
									SetupSpecAn(Technology.TDSCDMA, num3, SGPower, 5.0);
									short piRX_AGC4 = 0;
									phone.FTM_GET_RX_AGC(ref piRX_AGC4);
									int num18 = piRX_AGC - piRX_AGC4;
									double num19 = (double)num18 / 10.0;
									string message3 = base.Resources["SigGenOnat"].ToString() + " " + num16.ToString() + " " + base.Resources["AGCValue"].ToString() + ": " + piRX_AGC + " " + base.Resources["AGCValue"].ToString() + ": " + piRX_AGC4 + base.Resources["Changeis"].ToString() + " " + num19 + "dB";
									DebugMessageCB(MessageLevel.SUCCESS, message3);
								}
								SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: true);
							}
							num4 = GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC);
							SetCompAttr(txtRealtimeValue, SetAttribute.Text, num4.ToString());
							SetCompAttr(lblRealTimeValueUnits, SetAttribute.Text, "(Power: dBm)");
							JudgeRxLevel(num4);
							Thread.Sleep(800);
						}
					}
				}
				else
				{
					DebugMessageCB(MessageLevel.FAIL, "Invalid SG handle");
				}
				return;
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
				return;
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Unkonwn Exception " + ex2.Message);
				return;
			}
		}
		DebugMessageCB(MessageLevel.FAIL, "Invalid phone handle");
	}

	private void RxAGCMeasurment(bool PriRx)
	{
		if (C0_Scenario.SelectedIndex == -1 && C1_Scenario.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectRx"].ToString());
		}
		else
		{
			if (phone == null)
			{
				return;
			}
			try
			{
				if (GloVar.IsCatM1NBIoT == 1)
				{
					phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_LTE);
					DebugMessageCB(MessageLevel.SUCCESS, "QLIB_FTM_SET_MODE_ID(LTE) success");
					phone.FTM_LTE_SET_HD_FDD(1);
					DebugMessageCB(MessageLevel.SUCCESS, "QLIB_FTM_LTE_SET_HD_FDD(Half Duplex) success");
					Thread.Sleep(100);
				}
				phone.FTM_SET_CAL_MODE(1);
				phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
				phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
				ushort iChannel = Convert.ToUInt16(Channel.Text);
				if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
				{
					phone.FTM_LTE_SET_TX_BANDWIDTH(3);
					phone.FTM_LTE_SET_RX_BANDWIDTH(3);
				}
				phone.FTM_SET_CHAN(iChannel);
				uint num = 0u;
				double num2 = 0.0;
				if (PriRx)
				{
					num = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0_Scenario.SelectedIndex].RFmode_ID), Convert.ToUInt16(Channel.Text));
					num2 = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C0_List[C0_Scenario.SelectedIndex].RFmode_ID), num);
				}
				else
				{
					num = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1_Scenario.SelectedIndex].RFmode_ID), Convert.ToUInt16(Channel.Text));
					num2 = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(C1_List[C1_Scenario.SelectedIndex].RFmode_ID), num);
				}
				short piRX_AGC = 0;
				short piRX_AGC2 = 0;
				string message = "";
				if (pSG != null)
				{
					double num3 = num2;
					if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						num3 = num2 + 0.5;
					}
					pSG.SetupGen();
					Thread.Sleep(800);
					pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text));
					pSG.EnableOutput(onOff: true);
					Thread.Sleep(800);
					if (pSG.SetCenterFrequency(num3, 0.0))
					{
						DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SigGenat"].ToString() + " " + num3 + "MHz " + base.Resources["Success"].ToString());
					}
					else
					{
						DebugMessageCB(MessageLevel.FAIL, base.Resources["SigGenat"].ToString() + " " + num3 + "MHz " + base.Resources["Fail"].ToString());
					}
					short rxAGC = GetRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, Convert.ToDouble(DLPower.Text));
					short piDVGAoffset = 0;
					if (!PriRx)
					{
						phone.FTM_SET_SECONDARY_CHAIN(FTM_SecondaryChainMode.FTM_Secondary_Rx_Diversity);
						if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							phone.FTM_SET_MODE((FTM_RF_Mode_Enum)currentPowerAction.RFmode_ID);
							phone.FTM_SET_CHAN(iChannel);
						}
					}
					int num4 = 0;
					if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID) || CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (CalV4Parser.IsTech(CalV4Parser.Tech.CDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							if (PriRx)
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_CDMA_1X);
							}
							else
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_CDMA_1X_RX1);
							}
							Span = 2.0;
							mytech = Technology.CDMA;
						}
						else if (CalV4Parser.IsTech(CalV4Parser.Tech.WCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
						{
							if (PriRx)
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_WCDMA);
							}
							else
							{
								phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_WCDMA_RX1);
							}
							Span = 5.0;
							mytech = Technology.WCDMA;
						}
						phone.FTM_GET_DVGA_OFFSET(rxAGC, ref piDVGAoffset);
						phone.FTM_SET_DVGA_OFFSET(piDVGAoffset);
						phone.FTM_GET_RX_AGC(ref piRX_AGC);
						if (Siggen.SelectedIndex == 0)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 3.0);
						}
						else if (Siggen.SelectedIndex == 1)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 6.0);
						}
						else
						{
							pSG.EnableOutput(onOff: false);
						}
						Thread.Sleep(800);
						SetupSpecAn(mytech, num3, Convert.ToDouble(DLPower.Text), Span);
						phone.FTM_GET_RX_AGC(ref piRX_AGC2);
						num4 = piRX_AGC - piRX_AGC2;
						double num5 = (double)num4 / 10.0;
						message = base.Resources["SigGenOnat"].ToString() + " " + Convert.ToDouble(DLPower.Text) + base.Resources["RxLevel"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC) + base.Resources["SigGenOff"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC2) + " dBm" + base.Resources["Changeis"].ToString() + " " + num5 + "dB";
					}
					else if (CalV4Parser.IsTech(CalV4Parser.Tech.LTE, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (PriRx)
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_LTE);
						}
						else
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_LTE_RX1);
						}
						phone.FTM_GET_RX_AGC(ref piRX_AGC);
						if (Siggen.SelectedIndex == 0)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 3.0);
						}
						else if (Siggen.SelectedIndex == 1)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 6.0);
						}
						else
						{
							pSG.EnableOutput(onOff: false);
						}
						Thread.Sleep(800);
						phone.FTM_GET_RX_AGC(ref piRX_AGC2);
						num4 = piRX_AGC - piRX_AGC2;
						SetupSpecAn(Technology.LTE, num3, Convert.ToDouble(DLPower.Text), 10.0);
						double num6 = (double)num4 / 10.0;
						message = base.Resources["SigGenOnat"].ToString() + " " + Convert.ToDouble(DLPower.Text) + base.Resources["RxLevel"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC) + base.Resources["SigGenOff"].ToString() + ": " + GetPowerFromRxAGC((CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID, piRX_AGC2) + " dBm" + base.Resources["Changeis"].ToString() + " " + num6 + "dB";
					}
					else if (CalV4Parser.IsTech(CalV4Parser.Tech.TDSCDMA, (CalV4Parser.FTM_PHONE_MODE_ENUM)currentPowerAction.RFmode_ID))
					{
						if (PriRx)
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_TDSCDMA);
						}
						else
						{
							phone.FTM_SET_MODE_ID(FTM_Mode_Id_Enum.FTM_MODE_ID_TDSCDMA_RX1);
						}
						phone.FTM_GET_RX_AGC(ref piRX_AGC);
						if (Siggen.SelectedIndex == 0)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 3.0);
						}
						else if (Siggen.SelectedIndex == 1)
						{
							pSG.SetPowerLevel(Convert.ToDouble(DLPower.Text) - 6.0);
						}
						else
						{
							pSG.EnableOutput(onOff: false);
						}
						Thread.Sleep(800);
						phone.FTM_GET_RX_AGC(ref piRX_AGC2);
						num4 = piRX_AGC - piRX_AGC2;
						SetupSpecAn(Technology.TDSCDMA, num3, Convert.ToDouble(DLPower.Text), 5.0);
						double num7 = (double)num4 / 10.0;
						message = base.Resources["SigGenOnat"].ToString() + " " + Convert.ToDouble(DLPower.Text) + " " + base.Resources["AGCValue"].ToString() + ": " + piRX_AGC + " " + base.Resources["AGCValue"].ToString() + ": " + piRX_AGC2 + base.Resources["Changeis"].ToString() + " " + num7 + "dB";
					}
				}
				DebugMessageCB(MessageLevel.SUCCESS, message);
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Unkonwn Exception " + ex2.Message);
			}
		}
	}

	private void JudgeRxLevel(double RxLevel)
	{
		if (m_UpperRxPwrLim.HasValue || m_LowerRxPwrLim.HasValue)
		{
			bool flag = false;
			if (m_UpperRxPwrLim.HasValue && RxLevel > m_UpperRxPwrLim.Value)
			{
				flag = true;
				DebugMessageCB(MessageLevel.FAIL, "The Rx power is above the upper limit");
				SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.RED);
			}
			if (m_LowerRxPwrLim.HasValue && RxLevel < m_LowerRxPwrLim.Value)
			{
				flag = true;
				DebugMessageCB(MessageLevel.FAIL, "The Rx power is below the lower limit");
				SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.RED);
			}
			if (!flag)
			{
				SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.GREEN);
			}
		}
		else
		{
			SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		}
	}

	public string GetTextFromTextBox(TextBox tbx)
	{
		string text = null;
		if (tbx.Dispatcher.CheckAccess())
		{
			return tbx.Text;
		}
		GetTextFromTextBoxCB method = GetTextFromTextBox;
		return (string)base.Dispatcher.Invoke(method, tbx);
	}

	public bool GetSetupAnChecked()
	{
		bool result = false;
		if (SpecAn_Check.Dispatcher.CheckAccess())
		{
			if (SpecAn_Check.IsChecked == true)
			{
				result = true;
			}
		}
		else
		{
			GetCheckBoxIsCheckedCB method = GetSetupAnChecked;
			result = (bool)base.Dispatcher.Invoke(method);
		}
		return result;
	}

	public int? GetSiggenChangeSelectedIndex()
	{
		int? result = null;
		if (Siggen.Dispatcher.CheckAccess())
		{
			if (Siggen.SelectedItem != null)
			{
				result = Siggen.SelectedIndex;
			}
			return result;
		}
		GetComboBoxSelectedIndexCB method = GetSiggenChangeSelectedIndex;
		return (int?)base.Dispatcher.Invoke(method);
	}

	public void SetCompAttr<T>(T component, SetAttribute attr, UserDefinedColorType value)
	{
		if (!(component is TextBox))
		{
			return;
		}
		TextBox textBox = component as TextBox;
		if (textBox.Dispatcher.CheckAccess())
		{
			if (attr == SetAttribute.BGColor)
			{
				switch (value)
				{
				case UserDefinedColorType.RED:
					textBox.Background = new SolidColorBrush(Color.FromArgb(byte.MaxValue, byte.MaxValue, 0, 0));
					break;
				case UserDefinedColorType.GREEN:
					textBox.Background = new SolidColorBrush(Color.FromArgb(byte.MaxValue, 0, byte.MaxValue, 0));
					break;
				case UserDefinedColorType.DEFAULT:
					textBox.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
					break;
				}
			}
		}
		else
		{
			SetComponentAttributeCB<T, UserDefinedColorType> method = SetCompAttr;
			base.Dispatcher.BeginInvoke(method, component, attr, value);
		}
	}

	public void SetCompAttr<T>(T component, SetAttribute attr, string text)
	{
		if (component is TextBox)
		{
			TextBox textBox = component as TextBox;
			if (textBox.Dispatcher.CheckAccess())
			{
				if (attr == SetAttribute.Text)
				{
					textBox.Text = text;
				}
			}
			else
			{
				SetComponentAttributeCB<T, string> method = SetCompAttr;
				base.Dispatcher.BeginInvoke(method, component, attr, text);
			}
		}
		else
		{
			if (!(component is Label))
			{
				return;
			}
			Label label = component as Label;
			if (label.Dispatcher.CheckAccess())
			{
				if (attr == SetAttribute.Text)
				{
					label.Content = text;
				}
			}
			else
			{
				SetComponentAttributeCB<T, string> method2 = SetCompAttr;
				base.Dispatcher.BeginInvoke(method2, component, attr, text);
			}
		}
	}

	public void SetCompAttr<T>(T component, SetAttribute attr, bool bEnable)
	{
		if (!(component is Control))
		{
			return;
		}
		Control control = component as Control;
		if (control.Dispatcher.CheckAccess())
		{
			switch (attr)
			{
			case SetAttribute.IsEnable:
				control.IsEnabled = bEnable;
				break;
			case SetAttribute.Visibility:
				if (bEnable)
				{
					control.Visibility = Visibility.Visible;
				}
				else
				{
					control.Visibility = Visibility.Collapsed;
				}
				break;
			}
		}
		else
		{
			SetComponentAttributeCB<T, bool> method = SetCompAttr;
			base.Dispatcher.BeginInvoke(method, component, attr, bEnable);
		}
	}

	private void DiversityRxCont_Click(object sender, RoutedEventArgs e)
	{
		if (C1_Scenario.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectRx"].ToString());
			return;
		}
		bContinueRunFlag = true;
		bPathIsPRx = false;
		GetValueFromUI();
		SetCompAttr(DLPower, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(TxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxStop, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(TxOff, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(DivContTxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(DivContTxStop, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(ContinueTxOn, SetAttribute.Visibility, bEnable: true);
		SetCompAttr(ContinueTxStop, SetAttribute.Visibility, bEnable: false);
		SetCompAttr(DivContTxOn, SetAttribute.Visibility, bEnable: false);
		SetCompAttr(DivContTxStop, SetAttribute.Visibility, bEnable: true);
		thread = new Thread(RunContinueMeas);
		thread.Name = "Meas Rx";
		thread.Start();
	}

	private void DiversityRxContStop_Click(object sender, RoutedEventArgs e)
	{
		bContinueRunFlag = false;
	}

	private void DiversityRx_Click(object sender, RoutedEventArgs e)
	{
		SetCompAttr(txtRealtimeValue, SetAttribute.Text, "");
		SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		if (C1_Scenario.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectRx"].ToString());
		}
		else
		{
			RxAGCMeasurment(PriRx: false);
		}
	}

	private void PassLimitFromUIValue(string lowerLimText, string upperLimText)
	{
		m_UpperRxPwrLim = null;
		m_LowerRxPwrLim = null;
		if (double.TryParse(lowerLimText, out var result))
		{
			m_LowerRxPwrLim = result;
		}
		if (double.TryParse(upperLimText, out result))
		{
			m_UpperRxPwrLim = result;
		}
	}

	private void GetValueFromUI()
	{
		m_SGPower = null;
		m_channel = null;
		C0Scenario_SelectedIndex = null;
		C1Scenario_SelectedIndex = null;
		if (ushort.TryParse(Channel.Text, out var result))
		{
			m_channel = result;
		}
		if (double.TryParse(DLPower.Text, out var result2))
		{
			m_SGPower = result2;
		}
		PassLimitFromUIValue(txtLowerLim.Text, txtUpperLim.Text);
		if (C0_Scenario.SelectedItem != null)
		{
			C0Scenario_SelectedIndex = C0_Scenario.SelectedIndex;
		}
		if (C1_Scenario.SelectedItem != null)
		{
			C1Scenario_SelectedIndex = C1_Scenario.SelectedIndex;
		}
	}

	private void PrimaryRxCont_Click(object sender, RoutedEventArgs e)
	{
		if (C0_Scenario.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectRx"].ToString());
			return;
		}
		bContinueRunFlag = true;
		bPathIsPRx = true;
		GetValueFromUI();
		SetCompAttr(DLPower, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(TxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxStop, SetAttribute.IsEnable, bEnable: true);
		SetCompAttr(TxOff, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(DivContTxOn, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(DivContTxStop, SetAttribute.IsEnable, bEnable: false);
		SetCompAttr(ContinueTxOn, SetAttribute.Visibility, bEnable: false);
		SetCompAttr(ContinueTxStop, SetAttribute.Visibility, bEnable: true);
		SetCompAttr(DivContTxOn, SetAttribute.Visibility, bEnable: true);
		SetCompAttr(DivContTxStop, SetAttribute.Visibility, bEnable: false);
		thread = new Thread(RunContinueMeas);
		thread.Name = "Meas Rx";
		thread.Start();
	}

	private void PrimaryRxContStop_Click(object sender, RoutedEventArgs e)
	{
		bContinueRunFlag = false;
	}

	private void SiggenChange_Click(object sender, RoutedEventArgs e)
	{
		SetCompAttr(btSiggenChange, SetAttribute.IsEnable, bEnable: false);
		bChangeSiggenPwr = true;
	}

	public void FTM_RF_Rx_Unloaded(object sender, RoutedEventArgs e)
	{
		bContinueRunFlag = false;
	}

	private void PrimaryRx_Click(object sender, RoutedEventArgs e)
	{
		SetCompAttr(txtRealtimeValue, SetAttribute.Text, "");
		SetCompAttr(txtRealtimeValue, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		if (C0_Scenario.SelectedIndex == -1)
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SelectRx"].ToString());
		}
		else
		{
			RxAGCMeasurment(PriRx: true);
		}
	}

	private void ChannelValueChanged(object sender, TextChangedEventArgs e)
	{
		if (Channel.Text.Length > 0 && phone != null)
		{
			uint iChannel = phone.Band_Helper_CalculateDLChannel((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(currentPowerAction.RFmode_ID), Convert.ToUInt16(Channel.Text));
			double num = phone.Band_Helper_CalculateDLFrequency((ushort)CalV4Parser.ModeIDToQMSLBandHelperBandClass(currentPowerAction.RFmode_ID), iChannel);
			ChanInMHz.Text = num + "MHz";
		}
	}

	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 SetupSpecAn(Technology Tech, double dFreq, double dRefLvl, double dSpanFreq)
	{
		if (GetSetupAnChecked())
		{
			try
			{
				pSA.SetupMeasurement(Tech);
				pSA.SetSpan(dSpanFreq);
				pSA.SetCenterFrequency(dFreq);
				pSA.SetReferenceLevel(dRefLvl);
			}
			catch (Exception)
			{
			}
		}
	}

	private void SpecAn_Check_Checked(object sender, RoutedEventArgs e)
	{
		if (pSA.GetResourceString() != pSG.GetResourceString())
		{
			SpecAn_Check.IsChecked = true;
		}
		else
		{
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SpecAnSigGen"].ToString());
		}
	}
}
