using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using DebugToolInstrumentInterface;
using QC.QMSLPhone;
using QSC_Params_File_Helper;
using Qualcomm_ModemRFA;

namespace QFDT_Components;

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

	public SendDebugMessage DebugMessageCB;

	public bool bDoPwrChangeCheck;

	public bool bContinueRunFlag = true;

	public Phone phone;

	public ISignalGenerator pSG;

	public TechAndBandCombine m_curSelBand;

	public string m_cfgQSCXML;

	public SUB6_Rx_Override_Params m_RxOverrideParams;

	private Thread thread;

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

	public void PassDebugMessageCB(RFPathAndRadioConfiguration.SendDebugMessage me)
	{
		QSC_Path_RadioConfig.DebugMessageCB = me;
	}

	public FTM_RF_Rx_SUB6()
	{
		InitializeComponent();
		SetLanguageDictionary();
		m_RxOverrideParams = new SUB6_Rx_Override_Params();
		InitCbxDLPwrChange();
		phone = null;
		m_curSelBand = null;
		QSC_Path_RadioConfig.m_RFType = DebugRFType.RX;
		QSC_Path_RadioConfig.BandSelMessageCB = UpdateRxComponents;
		checkBxCtrlSG.IsChecked = true;
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxStop, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxChange, SetAttribute.IsEnable, bEnableOrVisbile: 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 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);
		QSC_Path_RadioConfig.SetLanguage(culture);
	}

	public void passPhoneHandle(Phone phone)
	{
		QSC_Path_RadioConfig.phone = phone;
	}

	public void UpdateUIviaXML(string calQSCXML, bool beHeForward = false)
	{
		if (beHeForward)
		{
			QSC_Path_RadioConfig.SetPlatform(Platform.HE);
		}
		else
		{
			QSC_Path_RadioConfig.SetPlatform(Platform.AT);
		}
		m_cfgQSCXML = calQSCXML;
		string strLog = "";
		if (!QSC_Path_RadioConfig.UpdateUIviaXML(calQSCXML, ref strLog))
		{
			DebugMessageCB(MessageLevel.EXCEPTION, strLog);
		}
		AccessComponentInThreadHelper.SetText(this, ULChannel, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, ChanInMHz, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, DLChannel, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, DLChanInMHz, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtDLPower, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtLNAState, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtMaxLimAGC, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtMinLimAGC, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtPRxAGCOrRssi, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtDRxAGCOrRssi, SetAttribute.Text, "");
	}

	private void InitCbxDLPwrChange()
	{
		cbxDLPowerChange.Items.Clear();
		cbxDLPowerChange.Items.Add("3dB");
		cbxDLPowerChange.Items.Add("6dB");
		cbxDLPowerChange.SelectedIndex = 0;
	}

	private void ChannelValueChanged(object sender, TextChangedEventArgs e)
	{
		string text = AccessComponentInThreadHelper.GetText(this, ULChannel, SetAttribute.Text);
		if (text.Length > 0)
		{
			uint result = 0u;
			if (uint.TryParse(text, out result) && phone != null && m_curSelBand != null)
			{
				ushort iBand = TechAndBandCombine.ChangeToBandHelperBandClass(m_curSelBand.tech, m_curSelBand.band);
				m_RxOverrideParams.ULChannel = Convert.ToUInt32(text);
				double num = phone.Band_Helper_CalculateULFrequency(iBand, result);
				AccessComponentInThreadHelper.SetText(this, ChanInMHz, SetAttribute.Text, num + " MHz");
				uint num2 = phone.Band_Helper_CalculateDLChannel(iBand, result);
				m_RxOverrideParams.DLChannel = num2;
				num = phone.Band_Helper_CalculateDLFrequency(iBand, num2);
				m_RxOverrideParams.DLFreq = num;
				AccessComponentInThreadHelper.SetText(this, DLChannel, SetAttribute.Text, num2.ToString());
				AccessComponentInThreadHelper.SetText(this, DLChanInMHz, SetAttribute.Text, num + " MHz");
			}
		}
	}

	private void RefreshRxPathComboBox(bool bDRxValid)
	{
		cbxRxPath.Items.Clear();
		if (bDRxValid)
		{
			cbxRxPath.Items.Add(RxPathOption.PRx);
			cbxRxPath.Items.Add(RxPathOption.DRx);
			cbxRxPath.Items.Add(RxPathOption.Both);
			cbxRxPath.SelectedItem = RxPathOption.Both;
		}
		else
		{
			cbxRxPath.Items.Add(RxPathOption.PRx);
			cbxRxPath.SelectedItem = RxPathOption.PRx;
		}
	}

	public void UpdateRxComponents(TechAndBandCombine selBand)
	{
		m_curSelBand = selBand;
		availablePathType curPath = QSC_Path_RadioConfig.m_curPath;
		RefreshRxPathComboBox(curPath.bValidDRx);
		string text = null;
		string text2 = null;
		string text3 = null;
		string text4 = null;
		string text5 = null;
		string text6;
		string text7;
		string text8;
		string text9;
		string text10;
		if (m_curSelBand.tech == "GSM")
		{
			text6 = TechAndBandCombine.GSMBandChanDefaultMap[m_curSelBand.band];
			text = QSCXMLHelper.getMiddleChan(text6);
			text7 = "-60";
			text2 = "-60";
			text8 = "0";
			text3 = "0";
			text9 = "";
			text4 = "";
			text10 = "";
			text5 = "";
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, spAGC_MaxLim, SetAttribute.Visibility, bEnableOrVisbile: false);
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, spAGC_MinLim, SetAttribute.Visibility, bEnableOrVisbile: false);
			AccessComponentInThreadHelper.SetText(this, lblPRxResultName, SetAttribute.Text, "PRx RSSI");
			AccessComponentInThreadHelper.SetText(this, lblDRxResultName, SetAttribute.Text, "DRx RSSI");
		}
		else
		{
			text6 = QSCXMLHelper.GetRxChannelList(m_curSelBand.tech, m_curSelBand.band, m_curSelBand.subband, m_cfgQSCXML);
			text = QSCXMLHelper.getMiddleChan(text6);
			text7 = QSCXMLHelper.GetRxCalInfoParamsList(m_curSelBand.tech, m_curSelBand.band, m_curSelBand.subband, m_cfgQSCXML, QSC_Param_Query.RxCalParameterType.LNA_Power);
			text2 = QSCXMLHelper.getFirstElementFromList(text7);
			text8 = QSCXMLHelper.GetRxCalInfoParamsList(m_curSelBand.tech, m_curSelBand.band, m_curSelBand.subband, m_cfgQSCXML, QSC_Param_Query.RxCalParameterType.LNA_State);
			text3 = QSCXMLHelper.getFirstElementFromList(text8);
			text9 = QSCXMLHelper.GetRxCalInfoParamsList(m_curSelBand.tech, m_curSelBand.band, m_curSelBand.subband, m_cfgQSCXML, QSC_Param_Query.RxCalParameterType.Gain_Limit_Max);
			text4 = QSCXMLHelper.getFirstElementFromList(text9);
			text10 = QSCXMLHelper.GetRxCalInfoParamsList(m_curSelBand.tech, m_curSelBand.band, m_curSelBand.subband, m_cfgQSCXML, QSC_Param_Query.RxCalParameterType.Gain_Limit_Min);
			text5 = QSCXMLHelper.getFirstElementFromList(text10);
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, spAGC_MaxLim, SetAttribute.Visibility, bEnableOrVisbile: true);
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, spAGC_MinLim, SetAttribute.Visibility, bEnableOrVisbile: true);
			AccessComponentInThreadHelper.SetText(this, lblPRxResultName, SetAttribute.Text, "PRx AGC");
			AccessComponentInThreadHelper.SetText(this, lblDRxResultName, SetAttribute.Text, "DRx AGC");
		}
		AccessComponentInThreadHelper.SetText(this, ULChannel, SetAttribute.Text, text);
		AccessComponentInThreadHelper.SetText(this, txtDLPower, SetAttribute.Text, text2);
		AccessComponentInThreadHelper.SetText(this, txtLNAState, SetAttribute.Text, text3);
		AccessComponentInThreadHelper.SetText(this, txtMaxLimAGC, SetAttribute.Text, text4);
		AccessComponentInThreadHelper.SetText(this, txtMinLimAGC, SetAttribute.Text, text5);
		AccessComponentInThreadHelper.SetText(this, ULChannel, SetAttribute.ToolTip, text6);
		AccessComponentInThreadHelper.SetText(this, txtDLPower, SetAttribute.ToolTip, text7);
		AccessComponentInThreadHelper.SetText(this, txtLNAState, SetAttribute.ToolTip, text8);
		AccessComponentInThreadHelper.SetText(this, txtMaxLimAGC, SetAttribute.ToolTip, text9);
		AccessComponentInThreadHelper.SetText(this, txtMinLimAGC, SetAttribute.ToolTip, text10);
		AccessComponentInThreadHelper.SetText(this, txtPRxAGCOrRssi, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtDRxAGCOrRssi, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetColor(this, txtPRxAGCOrRssi, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		AccessComponentInThreadHelper.SetColor(this, txtDRxAGCOrRssi, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
	}

	private void Refresh_Result_Visibility_Follow_RxPath_Selection()
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, spPRxAGC, SetAttribute.Visibility, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, spDRxAGC, SetAttribute.Visibility, bEnableOrVisbile: true);
		if (cbxRxPath.SelectedItem != null)
		{
			switch ((RxPathOption)cbxRxPath.SelectedItem)
			{
			case RxPathOption.PRx:
				AccessComponentInThreadHelper.SetEnableOrVisibility(this, spDRxAGC, SetAttribute.Visibility, bEnableOrVisbile: false);
				break;
			case RxPathOption.DRx:
				AccessComponentInThreadHelper.SetEnableOrVisibility(this, spPRxAGC, SetAttribute.Visibility, bEnableOrVisbile: false);
				break;
			}
		}
	}

	private void cbxRxChange_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		m_RxOverrideParams.powerChanged = 0.0;
		if (cbxDLPowerChange.SelectedItem != null)
		{
			string text = (string)cbxDLPowerChange.SelectedItem;
			int length = text.Length;
			text = text.Substring(0, length - 2);
			if (double.TryParse(text, out var result))
			{
				m_RxOverrideParams.powerChanged = result;
			}
		}
	}

	private void cbxRxPath_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (cbxRxPath.SelectedItem != null)
		{
			m_RxOverrideParams.rx_path_option = (RxPathOption)cbxRxPath.SelectedItem;
		}
		else
		{
			m_RxOverrideParams.rx_path_option = RxPathOption.PRx;
		}
		Refresh_Result_Visibility_Follow_RxPath_Selection();
	}

	private short GetRxAGCFromPower(string tech, double powerdBm)
	{
		short result = 0;
		switch (tech)
		{
		case "CDMA":
			result = (short)(-512 + ((int)(powerdBm * 10.0) + 1154));
			break;
		case "WCDMA":
		case "TDSCDMA":
			result = (short)(-512 + ((int)(powerdBm * 10.0) + 1062));
			break;
		case "LTE":
		case "NR5GSUB6":
			result = (short)(powerdBm * 10.0);
			break;
		}
		return result;
	}

	private double GetPowerFromRxAGC(string tech, short rxAGC)
	{
		double result = 0.0;
		switch (tech)
		{
		case "CDMA":
			result = ((double)rxAGC + 512.0) / 10.0 - 115.4;
			break;
		case "WCDMA":
		case "TDSCDMA":
			result = ((double)rxAGC + 512.0) / 10.0 - 106.2;
			break;
		case "LTE":
		case "NR5GSUB6":
			result = (double)rxAGC / 10.0;
			break;
		}
		return result;
	}

	private void GenSingleRxPathReq(ref List<FTM_RF_Debug_Property_Date_Type> tmp_properties, RFPathStruct singlePath)
	{
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["RF Path Index"], 0L);
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["Signal Path"], singlePath.Sig_Path);
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["RFM device"], singlePath.RFM_Device);
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["Antenna Path"], singlePath.Ant_Path);
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["Expected AGC"], GetRxAGCFromPower(m_curSelBand.tech, m_RxOverrideParams.expected_dlpower.Value));
		PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["LNA Gain State"], m_RxOverrideParams.lna_gain_state.Value);
		if (m_curSelBand.tech != "NR5GSUB6")
		{
			PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["Signal Type"], PropertyValueMap.Rx_Override_SigType_Map["CW"]);
		}
		if (RFPathAndRadioConfiguration.m_platform == Platform.HE)
		{
			PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["PLL_ID"], singlePath.PLL_ID);
		}
		if (m_curSelBand.tech == "GSM")
		{
			PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["GSM Data Type"], 0L);
			PropertyValueMap.AddOnePropertyToList(ref tmp_properties, PropertyValueMap.Rx_Override_Property_Type_Map["GSM Rx Enable"], 1L);
		}
	}

	private FTM_RF_Debug_Property_Struct GenRxOverrideReqStruct()
	{
		FTM_RF_Debug_Property_Struct result = default(FTM_RF_Debug_Property_Struct);
		List<FTM_RF_Debug_Property_Date_Type> tmp_properties = new List<FTM_RF_Debug_Property_Date_Type>();
		if (m_RxOverrideParams.rx_path_option == RxPathOption.PRx || m_RxOverrideParams.rx_path_option == RxPathOption.Both)
		{
			GenSingleRxPathReq(ref tmp_properties, QSC_Path_RadioConfig.m_curPath.PRxPath);
		}
		if ((m_RxOverrideParams.rx_path_option == RxPathOption.DRx || m_RxOverrideParams.rx_path_option == RxPathOption.Both) && QSC_Path_RadioConfig.m_curPath.bValidDRx)
		{
			GenSingleRxPathReq(ref tmp_properties, QSC_Path_RadioConfig.m_curPath.DRxPath);
		}
		result.num_properties = (uint)tmp_properties.Count;
		result.properties = new FTM_RF_Debug_Property_Date_Type[tmp_properties.Count];
		for (int i = 0; i < tmp_properties.Count; i++)
		{
			ref FTM_RF_Debug_Property_Date_Type reference = ref result.properties[i];
			reference = tmp_properties[i];
		}
		return result;
	}

	private void JudgeValueInLimit<T>(T? value, T? maxLim, T? minLim, TextBox txtValueTextBox, string valueName) where T : struct, IComparable<T>
	{
		if (value.HasValue && (maxLim.HasValue || minLim.HasValue))
		{
			bool flag = false;
			if (maxLim.HasValue && value.Value.CompareTo(maxLim.Value) >= 0)
			{
				flag = true;
				DebugMessageCB(MessageLevel.FAIL, "The " + valueName + " is above the upper limit");
				AccessComponentInThreadHelper.SetColor(this, txtValueTextBox, SetAttribute.BGColor, UserDefinedColorType.RED);
			}
			if (minLim.HasValue && value.Value.CompareTo(minLim.Value) <= 0)
			{
				flag = true;
				DebugMessageCB(MessageLevel.FAIL, "The " + valueName + " is below the lower limit");
				AccessComponentInThreadHelper.SetColor(this, txtValueTextBox, SetAttribute.BGColor, UserDefinedColorType.RED);
			}
			if (!flag)
			{
				AccessComponentInThreadHelper.SetColor(this, txtValueTextBox, SetAttribute.BGColor, UserDefinedColorType.GREEN);
			}
		}
		else
		{
			AccessComponentInThreadHelper.SetColor(this, txtValueTextBox, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		}
	}

	private void GetAGCFromRsp(FTM_RF_Debug_Property_Struct rspStruct, out long? PRx_AGC, out long? DRx_AGC)
	{
		PRx_AGC = null;
		DRx_AGC = null;
		List<Dictionary<string, long>> list = new List<Dictionary<string, long>>();
		Dictionary<string, long> dictionary = null;
		if (rspStruct.num_properties != 0 && rspStruct.properties != null)
		{
			int i;
			for (i = 0; i < rspStruct.num_properties; i++)
			{
				if (rspStruct.properties[i].type == PropertyValueMap.Rx_Override_Property_Type_Map["RF Path Index"])
				{
					if (dictionary != null)
					{
						list.Add(dictionary);
					}
					dictionary = new Dictionary<string, long>();
				}
				string key = PropertyValueMap.Rx_Override_Property_Type_Map.FirstOrDefault((KeyValuePair<string, uint> q) => q.Value == rspStruct.properties[i].type).Key;
				dictionary.Add(key, rspStruct.properties[i].value);
			}
			list.Add(dictionary);
		}
		if (list.Count <= 0)
		{
			return;
		}
		for (int j = 0; j < list.Count; j++)
		{
			if (!list[j].ContainsKey("Signal Path") || !list[j].ContainsKey("RFM device") || !list[j].ContainsKey("Antenna Path"))
			{
				continue;
			}
			string key2 = ((m_curSelBand.tech == "GSM") ? "GSM RSSI" : "AGC Offset Result");
			if (QSC_Path_RadioConfig.m_curPath.PRxPath.EqualPath((ushort)list[j]["RFM device"], (uint)list[j]["Signal Path"], (ushort)list[j]["Antenna Path"], 999))
			{
				if (list[j].ContainsKey(key2))
				{
					PRx_AGC = list[j][key2];
				}
			}
			else if (QSC_Path_RadioConfig.m_curPath.bValidDRx && QSC_Path_RadioConfig.m_curPath.DRxPath.EqualPath((ushort)list[j]["RFM device"], (uint)list[j]["Signal Path"], (ushort)list[j]["Antenna Path"], 999) && list[j].ContainsKey(key2))
			{
				DRx_AGC = list[j][key2];
			}
		}
	}

	private void JudgeResult(long? PRx_AGC, long? DRx_AGC)
	{
		long? maxLim = null;
		long? minLim = null;
		string text = AccessComponentInThreadHelper.GetText(this, txtMaxLimAGC, SetAttribute.Text);
		string text2 = AccessComponentInThreadHelper.GetText(this, txtMinLimAGC, SetAttribute.Text);
		if (long.TryParse(text, out var result))
		{
			maxLim = result;
		}
		if (long.TryParse(text2, out result))
		{
			minLim = result;
		}
		JudgeValueInLimit(PRx_AGC, maxLim, minLim, txtPRxAGCOrRssi, "PRx AGC");
		if (PRx_AGC.HasValue)
		{
			AccessComponentInThreadHelper.SetText(this, txtPRxAGCOrRssi, SetAttribute.Text, PRx_AGC.Value.ToString());
		}
		JudgeValueInLimit(DRx_AGC, maxLim, minLim, txtDRxAGCOrRssi, "DRx AGC");
		if (DRx_AGC.HasValue)
		{
			AccessComponentInThreadHelper.SetText(this, txtDRxAGCOrRssi, SetAttribute.Text, DRx_AGC.Value.ToString());
		}
	}

	private bool SetupSigGen(double? dlFreq, double? expected_dlpower)
	{
		if (pSG != null && dlFreq.HasValue && expected_dlpower.HasValue)
		{
			double num = dlFreq.Value;
			if (m_curSelBand.tech == "LTE")
			{
				num = dlFreq.Value + 0.5;
			}
			pSG.SetupGen();
			Thread.Sleep(800);
			pSG.SetPowerLevel(expected_dlpower.Value);
			pSG.EnableOutput(onOff: true);
			Thread.Sleep(800);
			if (pSG.SetCenterFrequency(num, 0.0))
			{
				DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SigGenat"].ToString() + " " + num + "MHz " + base.Resources["Success"].ToString());
				return true;
			}
			DebugMessageCB(MessageLevel.FAIL, base.Resources["SigGenat"].ToString() + " " + num + "MHz " + base.Resources["Fail"].ToString());
			return false;
		}
		DebugMessageCB(MessageLevel.FAIL, "Setup SG fail, please check SG handle or Freq/Power");
		return false;
	}

	private bool DoQSCRFDebugRxOverride(out long? PRx_AGC, out long? DRx_AGC, double? dlFreq, double? expected_dlpower)
	{
		PRx_AGC = null;
		DRx_AGC = null;
		if (m_RxOverrideParams.bCtrlSG)
		{
			SetupSigGen(dlFreq, expected_dlpower);
		}
		FTM_RF_Debug_Property_Struct req_struct = GenRxOverrideReqStruct();
		FTM_RF_Debug_Property_Struct rsp_struct = default(FTM_RF_Debug_Property_Struct);
		uint error_code = 0u;
		uint specific_err_code = 0u;
		if (phone != null)
		{
			try
			{
				phone.FTM_RF_DEBUG_CMD_RX_OVERRIDE(PropertyValueMap.FTM_RF_Debug_Tech_Map[m_curSelBand.tech], ref req_struct, ref rsp_struct, ref error_code, ref specific_err_code);
				if (error_code == 0 && specific_err_code == 0)
				{
					DebugMessageCB(MessageLevel.SUCCESS, "QLIB_FTM_RF_DEBUG_CMD_RX_OVERRIDE success");
					GetAGCFromRsp(rsp_struct, out PRx_AGC, out DRx_AGC);
					return true;
				}
				DebugMessageCB(MessageLevel.FAIL, "Rx Override fail, return error code != 0");
				return false;
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
				return false;
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Unknown Exception " + ex2.Message);
				return false;
			}
		}
		MessageBox.Show("Invalid phone handle");
		return false;
	}

	private void getRSSI(out long? outPRxRSSI, out long? outDRxRSSI, RxPathOption rxPathOption)
	{
		outPRxRSSI = null;
		outDRxRSSI = null;
		GSM_Bands gsmBand = GSM_Bands.GSM_Band_Cell_850;
		if (m_curSelBand.band == "0")
		{
			gsmBand = GSM_Bands.GSM_Band_Cell_850;
		}
		else if (m_curSelBand.band == "1")
		{
			gsmBand = GSM_Bands.GSM_Band_EGSM_900;
		}
		else if (m_curSelBand.band == "2")
		{
			gsmBand = GSM_Bands.GSM_Band_DCS_1800;
		}
		else if (m_curSelBand.band == "3")
		{
			gsmBand = GSM_Bands.GSM_Band_PCS_1900;
		}
		phone.FTM_SET_MODE(gsmBand);
		phone.FTM_SET_CHAN((ushort)m_RxOverrideParams.DLChannel.Value);
		phone.FTM_SET_RX_CONTINUOUS(0, 0);
		phone.FTM_SET_LNA_RANGE(0);
		Thread.Sleep(200);
		uint pRxRssiValue = 0u;
		uint drxRssiValue = 0u;
		if (rxPathOption == RxPathOption.PRx)
		{
			phone.FTM_GET_RSSI(ref pRxRssiValue);
			outPRxRSSI = pRxRssiValue;
			return;
		}
		phone.FTM_SET_SECONDARY_CHAIN(FTM_SecondaryChainMode.FTM_Secondary_Rx_Diversity);
		phone.FTM_GET_GSM_PRX_DRX_RSSI(ref pRxRssiValue, ref drxRssiValue);
		outDRxRSSI = drxRssiValue;
		if (rxPathOption == RxPathOption.Both)
		{
			outPRxRSSI = pRxRssiValue;
		}
	}

	private bool DoTraditionalGSMRxRSSI(out long? PRx_RSSI, out long? DRx_RSSI, double? dlFreq, double? expected_dlpower)
	{
		PRx_RSSI = null;
		DRx_RSSI = null;
		if (m_RxOverrideParams.bCtrlSG)
		{
			SetupSigGen(dlFreq, expected_dlpower);
		}
		if (phone != null)
		{
			try
			{
				getRSSI(out PRx_RSSI, out DRx_RSSI, m_RxOverrideParams.rx_path_option);
				DebugMessageCB(MessageLevel.SUCCESS, "GET GSM RSSI by traditional way --- success");
				return true;
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
				return false;
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Unknown Exception " + ex2.Message);
				return false;
			}
		}
		MessageBox.Show("Invalid phone handle");
		return false;
	}

	private bool DoRxOverride(out long? PRx_AGC, out long? DRx_AGC, double? dlFreq, double? expected_dlpower)
	{
		return DoQSCRFDebugRxOverride(out PRx_AGC, out DRx_AGC, dlFreq, expected_dlpower);
	}

	private void GetRxParamsFromUI()
	{
		m_RxOverrideParams.Clear();
		string text = AccessComponentInThreadHelper.GetText(this, txtDLPower, SetAttribute.Text);
		string text2 = AccessComponentInThreadHelper.GetText(this, txtLNAState, SetAttribute.Text);
		if (double.TryParse(text, out var result))
		{
			m_RxOverrideParams.expected_dlpower = result;
		}
		if (int.TryParse(text2, out var result2))
		{
			m_RxOverrideParams.lna_gain_state = result2;
		}
	}

	private void GetChangeDBMsg(string tech, long agcOrRssi0, long agcOrRssi1, ref string Msg)
	{
		if (tech == "GSM")
		{
			double num = Math.Log(agcOrRssi0, 10.0);
			double num2 = Math.Log(agcOrRssi1, 10.0);
			double num3 = (num2 - num) * 10.0;
			Msg = Msg + " RSSI Changed from " + agcOrRssi0 + " to " + agcOrRssi1 + ", Change is " + $"{num3:0.##}" + "dB";
		}
		else
		{
			long num4 = agcOrRssi1 - agcOrRssi0;
			double num5 = (double)num4 / 10.0;
			Msg = Msg + " AGC Changed from " + agcOrRssi0 + " to " + agcOrRssi1 + ", Change is " + num5 + "dB";
		}
	}

	private void OneShotRxStartOn()
	{
		GetRxParamsFromUI();
		if (m_curSelBand == null || QSC_Path_RadioConfig.m_curPath == null || !m_RxOverrideParams.CheckValid())
		{
			DebugMessageCB(MessageLevel.FAIL, "Invalid params, please check");
		}
		else
		{
			if (!QSC_Path_RadioConfig.SetupRadioConfiguration(m_RxOverrideParams.ULChannel.Value, PropertyValueMap.Radio_Cfg_WFM_Value_Map["SUB6 PUSCH"], PropertyValueMap.Radio_Cfg_BW_Map_SUB6["20MHz"], 50, 0, IsTearDown: false))
			{
				return;
			}
			long? PRx_AGC = null;
			long? DRx_AGC = null;
			DoRxOverride(out PRx_AGC, out DRx_AGC, m_RxOverrideParams.DLFreq, m_RxOverrideParams.expected_dlpower);
			JudgeResult(PRx_AGC, DRx_AGC);
			if (bDoPwrChangeCheck)
			{
				long? PRx_AGC2 = null;
				long? DRx_AGC2 = null;
				DoRxOverride(out PRx_AGC2, out DRx_AGC2, m_RxOverrideParams.DLFreq, m_RxOverrideParams.expected_dlpower + m_RxOverrideParams.powerChanged);
				string text = "";
				if (PRx_AGC.HasValue && PRx_AGC2.HasValue)
				{
					string Msg = "PRx";
					GetChangeDBMsg(m_curSelBand.tech, PRx_AGC.Value, PRx_AGC2.Value, ref Msg);
					DebugMessageCB(MessageLevel.SUCCESS, Msg);
					text += Msg;
					text += "\n";
				}
				if (DRx_AGC.HasValue && DRx_AGC2.HasValue)
				{
					string Msg = "DRx";
					GetChangeDBMsg(m_curSelBand.tech, DRx_AGC.Value, DRx_AGC2.Value, ref Msg);
					DebugMessageCB(MessageLevel.SUCCESS, Msg);
					text += Msg;
				}
				MessageBox.Show(text);
				bDoPwrChangeCheck = false;
				AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxChange, SetAttribute.IsEnable, bEnableOrVisbile: true);
			}
			Thread.Sleep(100);
			QSC_Path_RadioConfig.SetupRadioConfiguration(m_RxOverrideParams.ULChannel.Value, PropertyValueMap.Radio_Cfg_WFM_Value_Map["SUB6 PUSCH"], PropertyValueMap.Radio_Cfg_BW_Map_SUB6["20MHz"], 50, 0, IsTearDown: true);
		}
	}

	private void RunRxOverrideOn()
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxStart, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxStop, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, spPathInfo, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxChange, SetAttribute.IsEnable, bEnableOrVisbile: true);
		bContinueRunFlag = true;
		while (bContinueRunFlag)
		{
			OneShotRxStartOn();
			Thread.Sleep(500);
		}
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxStart, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxStop, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, spPathInfo, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxChange, SetAttribute.IsEnable, bEnableOrVisbile: false);
	}

	private void RxOverrideOn_Click(object sender, RoutedEventArgs e)
	{
		AccessComponentInThreadHelper.SetText(this, txtPRxAGCOrRssi, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetText(this, txtDRxAGCOrRssi, SetAttribute.Text, "");
		AccessComponentInThreadHelper.SetColor(this, txtPRxAGCOrRssi, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		AccessComponentInThreadHelper.SetColor(this, txtDRxAGCOrRssi, SetAttribute.BGColor, UserDefinedColorType.DEFAULT);
		thread = new Thread(RunRxOverrideOn);
		thread.Name = "Meas Rx - QSC";
		thread.Start();
	}

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

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

	private void RxChange_Click(object sender, RoutedEventArgs e)
	{
		bDoPwrChangeCheck = true;
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btnRxChange, SetAttribute.IsEnable, bEnableOrVisbile: false);
	}

	private void UpdateSGFlag()
	{
		m_RxOverrideParams.bCtrlSG = AccessComponentInThreadHelper.getIsChecked(this, checkBxCtrlSG, SetAttribute.IsChecked);
	}

	private void SG_Enable_Checked(object sender, RoutedEventArgs e)
	{
		UpdateSGFlag();
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
	internal Delegate _CreateDelegate(Type delegateType, string handler)
	{
		return Delegate.CreateDelegate(delegateType, this, handler);
	}
}
