using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Qualcomm_ModemRFA;

public class CalV4Parser
{
	public enum Tech
	{
		[XmlEnum(Name = "CDMA")]
		CDMA,
		[XmlEnum(Name = "WCDMA")]
		WCDMA,
		[XmlEnum(Name = "LTE")]
		LTE,
		[XmlEnum(Name = "TDSDMA")]
		TDSCDMA
	}

	public enum FTM_PHONE_MODE_ENUM
	{
		PHONE_MODE_FM = 1,
		PHONE_MODE_SLEEP = 2,
		PHONE_MODE_GPS = 3,
		PHONE_MODE_GPS_SINAD = 4,
		PHONE_MODE_CDMA_800 = 5,
		PHONE_MODE_CDMA_1900 = 6,
		PHONE_MODE_CDMA_1800 = 8,
		PHONE_MODE_J_CDMA = 14,
		PHONE_MODE_CDMA_450 = 17,
		PHONE_MODE_CDMA_IMT = 19,
		PHONE_MODE_CDMA_1900_EXT = 26,
		PHONE_MODE_CDMA_BC11 = 27,
		PHONE_MODE_CDMA_BC15 = 32,
		PHONE_MODE_CDMA_800_SEC = 33,
		PHONE_MODE_WCDMA_IMT = 9,
		PHONE_MODE_GSM_900 = 10,
		PHONE_MODE_GSM_1800 = 11,
		PHONE_MODE_GSM_1900 = 12,
		PHONE_MODE_BLUETOOTH = 13,
		PHONE_MODE_WCDMA_1900A = 15,
		PHONE_MODE_WCDMA_1900B = 16,
		PHONE_MODE_GSM_850 = 18,
		PHONE_MODE_WCDMA_800 = 22,
		PHONE_MODE_WCDMA_800A = 23,
		PHONE_MODE_WCDMA_1800 = 25,
		PHONE_MODE_WCDMA_BC4 = 28,
		PHONE_MODE_WCDMA_BC8 = 29,
		PHONE_MODE_MF_700 = 30,
		PHONE_MODE_WCDMA_BC9 = 31,
		PHONE_MODE_WCDMA_1500 = 40,
		PHONE_MODE_LTE_BC1 = 34,
		PHONE_MODE_LTE_BC7 = 35,
		PHONE_MODE_LTE_BC4 = 42,
		PHONE_MODE_LTE_BC11 = 41,
		PHONE_MODE_LTE_BC13 = 36,
		PHONE_MODE_LTE_BC17 = 37,
		PHONE_MODE_LTE_BC38 = 38,
		PHONE_MODE_LTE_BC40 = 39,
		PHONE_MODE_LTE_B2 = 43,
		PHONE_MODE_LTE_B3 = 44,
		PHONE_MODE_LTE_B5 = 45,
		PHONE_MODE_LTE_B6 = 46,
		PHONE_MODE_LTE_B8 = 47,
		PHONE_MODE_LTE_B9 = 48,
		PHONE_MODE_LTE_B10 = 49,
		PHONE_MODE_LTE_B12 = 50,
		PHONE_MODE_LTE_B14 = 51,
		PHONE_MODE_LTE_B15 = 52,
		PHONE_MODE_LTE_B16 = 53,
		PHONE_MODE_LTE_B18 = 54,
		PHONE_MODE_LTE_B19 = 55,
		PHONE_MODE_LTE_B20 = 56,
		PHONE_MODE_LTE_B21 = 57,
		PHONE_MODE_LTE_B22 = 58,
		PHONE_MODE_LTE_B23 = 59,
		PHONE_MODE_LTE_B24 = 60,
		PHONE_MODE_LTE_B25 = 61,
		PHONE_MODE_LTE_B26 = 62,
		PHONE_MODE_LTE_B27 = 63,
		PHONE_MODE_LTE_B28 = 64,
		PHONE_MODE_LTE_B29 = 65,
		PHONE_MODE_LTE_B30 = 66,
		PHONE_MODE_LTE_B31 = 67,
		PHONE_MODE_LTE_B32 = 68,
		PHONE_MODE_LTE_B33 = 69,
		PHONE_MODE_LTE_B34 = 70,
		PHONE_MODE_LTE_B35 = 71,
		PHONE_MODE_LTE_B36 = 72,
		PHONE_MODE_LTE_B37 = 73,
		PHONE_MODE_LTE_B39 = 74,
		PHONE_MODE_WCDMA_BC19 = 75,
		PHONE_MODE_LTE_B41 = 76,
		PHONE_MODE_LTE_B42 = 77,
		PHONE_MODE_TDSCDMA_B34 = 90,
		PHONE_MODE_TDSCDMA_B39 = 91,
		PHONE_MODE_TDSCDMA_B40 = 92
	}

	public enum ESC_SWEEP_TYPE
	{
		MAX_BIAS_LINEARIZER = 0,
		APT_LINEARIZER = 1,
		XPT_SWP1 = 2,
		MAX_BIAS_FREQCOMP = 3,
		APT_FREQCOMP = 4,
		MULTI_CHAN_LINEARIZER = 5,
		XPT_SWP2 = 6,
		XPT_SWP3 = 7,
		XPT_FREQCOMP = 8,
		XPT_DELAY = 9,
		APT_FREQCOMP_STAGGER_RXTX = 10,
		XPT_FREQCOMP_STAGGER_RXTX = 11,
		XPT_FREQCOMP_SERIAL_TX = 12,
		XPT_FREQCOMP_SERIAL_RX = 13,
		INTRACA_FCOMP = 14,
		XPT_PIN_CAL = 15,
		REF_CAL = 100,
		INT_CAL_MAX_BIAS_LINEARIZER = 101,
		INT_CAL_APT_LINEARIZER = 102,
		INT_CAL_MAX_BIAS_FREQCOMP = 103,
		INT_CAL_APT_FREQCOMP = 104,
		MULTI_TECH_MULTI_BAND_REF_CAL = 105,
		INT_CAL_XPT_SWP1 = 106,
		INT_CAL_XPT_SWP2 = 107,
		INT_CAL_XPT_SWP3 = 108,
		INT_CAL_XPT_FREQCOMP = 109,
		MULTI_BAND_RX_CHARACTERIZED_CAL = 110,
		MAX_SWEEP_TYPE = 65535
	}

	public enum FTM_Mode_Id_Enum
	{
		FTM_MODE_ID_CDMA_1X = 0,
		FTM_MODE_ID_WCDMA = 1,
		FTM_MODE_ID_GSM = 2,
		FTM_MODE_ID_CDMA_1X_RX1 = 3,
		FTM_MODE_ID_BLUETOOTH = 4,
		FTM_MODE_ID_CDMA_1X_CALL = 7,
		FTM_MODE_ID_HDR_C = 8,
		FTM_MODE_ID_LOGGING = 9,
		FTM_MODE_ID_AGPS = 10,
		FTM_MODE_ID_PMIC = 11,
		FTM_MODE_GSM_BER = 13,
		FTM_MODE_ID_AUDIO = 14,
		FTM_MODE_ID_CAMERA = 15,
		FTM_MODE_WCDMA_BER = 16,
		FTM_MODE_ID_GSM_EXTENDED_C = 17,
		FTM_MODE_CDMA_API_V2 = 18,
		FTM_MODE_ID_MF_C = 19,
		FTM_MODE_RF_COMMON = 20,
		FTM_MODE_WCDMA_RX1 = 21,
		FTM_MODE_WLAN = 22,
		FTM_MODE_QFUSE = 24,
		FTM_MODE_ID_MF_NS = 26,
		FTM_MODE_GPS = 28,
		FTM_MODE_FMRDS = 28,
		FTM_MODE_ID_LTE = 29,
		FTM_MODE_LTE_NS = 30,
		FTM_MODE_ID_CDMA_1X_C2 = 32,
		FTM_MODE_IRAT = 33,
		FTM_MODE_ID_DVBH = 240,
		FTM_MODE_ID_ISDBT = 240,
		FTM_MODE_ID_MBP = 241,
		FTM_MODE_SE_BER = 27,
		FTM_MODE_GNSS_RF = 34,
		FTM_MODE_LTE_RX1 = 35,
		FTM_MODE_RFNV = 36,
		FTM_MODE_SEQUENCER = 37,
		FTM_MODE_SV = 38,
		FTM_MODE_RFM_C = 39,
		FTM_MODE_ID_1X_C3_C = 40,
		FTM_MODE_ID_RFCA_MAL_C = 41,
		FTM_MODE_ID_TDSCDMA = 42,
		FTM_MODE_ID_TDSCDMA_RX1 = 43,
		FTM_MODE_ID_TDSCDMA_BER = 44,
		FTM_MODE_ID_1X_C4_C = 45,
		FTM_MODE_ID_LTE_C3 = 47,
		FTM_MODE_ID_LTE_C4 = 48,
		FTM_MODE_ID_GSM_C2 = 49,
		FTM_MODE_ID_GSM_EXTENDED_C2 = 50,
		FTM_MODE_ID_GSM_BER_C2 = 51,
		FTM_MODE_ID_FTM_AP = 52,
		FTM_MODE_ID_FFBM = 53,
		FTM_MODE_ID_NFC = 55,
		FTM_MODE_WCDMA_RX3 = 56,
		FTM_MODE_WCDMA_RX4 = 57,
		FTM_MODE_WP_C = 64,
		FTM_MODE_ID_LTE_C5 = 65,
		FTM_MODE_ID_LTE_C6 = 66,
		FTM_MODE_WCDMA_RX5_C = 68,
		FTM_MODE_WCDMA_RX6_C = 69,
		FTM_MODE_WCDMA_RX7_C = 70,
		FTM_MODE_WCDMA_RX8_C = 71,
		FTM_MODE_ID_ANT = 94,
		FTM_MODE_ID_HDR_C0 = 95,
		FTM_MODE_ID_HDR_C1 = 96,
		FTM_MODE_ID_HDR_C2 = 97,
		FTM_MODE_ID_HDR_C3 = 98,
		FTM_TDSCDMA_C2_C = 99,
		FTM_TDSCDMA_C3_C = 100,
		FTM_MODE_ID_PRODUCTION = 32768,
		FTM_MODE_ID_LTM = 32769
	}

	private CalV4 m_CalV4;

	public void ParseCalV4File(string path)
	{
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(CalV4));
		FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
		try
		{
			m_CalV4 = (CalV4)xmlSerializer.Deserialize(fileStream);
		}
		catch (Exception ex)
		{
			throw ex;
		}
		fileStream.Close();
	}

	public static int ModeIDToBandClass(int m_RFmode_ID)
	{
		return m_RFmode_ID switch
		{
			9 => 1, 
			16 => 2, 
			25 => 3, 
			29 => 8, 
			28 => 4, 
			22 => 5, 
			75 => 19, 
			5 => 0, 
			6 => 1, 
			19 => 6, 
			33 => 10, 
			34 => 1, 
			35 => 7, 
			42 => 4, 
			41 => 11, 
			36 => 13, 
			37 => 17, 
			38 => 38, 
			39 => 40, 
			43 => 2, 
			44 => 3, 
			45 => 5, 
			46 => 6, 
			47 => 8, 
			48 => 9, 
			49 => 10, 
			50 => 12, 
			51 => 14, 
			52 => 15, 
			53 => 16, 
			54 => 18, 
			55 => 19, 
			56 => 20, 
			57 => 21, 
			58 => 22, 
			59 => 23, 
			60 => 24, 
			61 => 25, 
			62 => 26, 
			63 => 27, 
			64 => 28, 
			65 => 29, 
			66 => 30, 
			67 => 31, 
			68 => 32, 
			69 => 33, 
			70 => 34, 
			71 => 35, 
			72 => 36, 
			73 => 37, 
			74 => 39, 
			76 => 41, 
			90 => 34, 
			91 => 39, 
			92 => 40, 
			_ => 99, 
		};
	}

	public static int ModeIDToQMSLBandHelperBandClass(int m_RFmode_ID)
	{
		return m_RFmode_ID switch
		{
			18 => 4, 
			10 => 0, 
			11 => 2, 
			12 => 3, 
			9 => 100, 
			16 => 101, 
			25 => 102, 
			29 => 107, 
			28 => 103, 
			22 => 104, 
			75 => 118, 
			40 => 110, 
			5 => 200, 
			6 => 201, 
			19 => 206, 
			33 => 210, 
			32 => 215, 
			34 => 20000, 
			35 => 20006, 
			42 => 20003, 
			41 => 20010, 
			36 => 20012, 
			37 => 20016, 
			38 => 20037, 
			39 => 20039, 
			43 => 20001, 
			44 => 20002, 
			45 => 20004, 
			46 => 20005, 
			47 => 20007, 
			48 => 20008, 
			49 => 20009, 
			50 => 20011, 
			51 => 20013, 
			52 => 20014, 
			53 => 20015, 
			54 => 20017, 
			55 => 20018, 
			56 => 20019, 
			57 => 20020, 
			58 => 20021, 
			59 => 20022, 
			60 => 20023, 
			61 => 20024, 
			62 => 20025, 
			63 => 20026, 
			64 => 20027, 
			65 => 20028, 
			66 => 20029, 
			67 => 20030, 
			68 => 20031, 
			69 => 20032, 
			70 => 20033, 
			71 => 20034, 
			72 => 20035, 
			73 => 20036, 
			74 => 20038, 
			76 => 20040, 
			90 => 1001, 
			91 => 1006, 
			92 => 1007, 
			_ => 65535, 
		};
	}

	public List<RxPowerAction> Get_HighestGainState_RxPowerAction_FromAllTechs(int chain)
	{
		List<RxPowerAction> list = Get_HighestGainState_RxPowerAction(Tech.CDMA, m_CalV4, chain);
		List<RxPowerAction> list2 = Get_HighestGainState_RxPowerAction(Tech.WCDMA, m_CalV4, chain);
		List<RxPowerAction> list3 = Get_HighestGainState_RxPowerAction(Tech.LTE, m_CalV4, chain);
		List<RxPowerAction> list4 = Get_HighestGainState_RxPowerAction(Tech.TDSCDMA, m_CalV4, chain);
		List<RxPowerAction> list5 = new List<RxPowerAction>();
		if (list != null)
		{
			list5.AddRange(list);
		}
		if (list2 != null)
		{
			list5.AddRange(list2);
		}
		if (list3 != null)
		{
			list5.AddRange(list3);
		}
		if (list4 != null)
		{
			list5.AddRange(list4);
		}
		return list5;
	}

	public List<RxPowerAction> Get_HighestGainState_RxPowerAction(Tech tech, CalV4 calV4, int chain)
	{
		List<RxPowerAction> list = new List<RxPowerAction>();
		for (int i = 0; i < calV4.Node_Name.Length; i++)
		{
			if (!IsCalInstance0(calV4.Node_Name[i].name) || !IsTech(tech, (FTM_PHONE_MODE_ENUM)calV4.Node_Name[i].RF_Mode))
			{
				continue;
			}
			RxPowerAction rxPowerAction = new RxPowerAction();
			rxPowerAction = GetHighestGainStateRxAction(calV4.Node_Name[i].Sweep_Params, chain);
			if (rxPowerAction == null)
			{
				continue;
			}
			rxPowerAction.RFmode_ID = calV4.Node_Name[i].RF_Mode;
			rxPowerAction.Description = calV4.Node_Name[i].name;
			switch (tech)
			{
			case Tech.CDMA:
				if (chain == 0 && rxPowerAction.Device == 0)
				{
					rxPowerAction.modeID = 0;
				}
				else if (chain == 1 && rxPowerAction.Device == 0)
				{
					rxPowerAction.modeID = 3;
				}
				else if (chain == 0 && rxPowerAction.Device == 1)
				{
					rxPowerAction.modeID = 3;
				}
				else if (chain == 1 && rxPowerAction.Device == 1)
				{
					rxPowerAction.modeID = 3;
				}
				break;
			case Tech.WCDMA:
				if (chain == 0 && rxPowerAction.Device == 0)
				{
					rxPowerAction.modeID = 1;
				}
				else if (chain == 1 && rxPowerAction.Device == 1)
				{
					rxPowerAction.modeID = 21;
				}
				break;
			case Tech.LTE:
				if (chain == 0 && rxPowerAction.Device == 0)
				{
					rxPowerAction.modeID = 29;
				}
				else if (chain == 1 && rxPowerAction.Device == 1)
				{
					rxPowerAction.modeID = 35;
				}
				break;
			case Tech.TDSCDMA:
				if (chain == 0 && rxPowerAction.Device == 0)
				{
					rxPowerAction.modeID = 42;
				}
				else if (chain == 1 && rxPowerAction.Device == 1)
				{
					rxPowerAction.modeID = 43;
				}
				break;
			}
			list.Add(rxPowerAction);
		}
		return list;
	}

	public RxPowerAction GetHighestGainStateRxAction(Sweep_Params[] sweep_Params, int chain)
	{
		if (sweep_Params != null)
		{
			for (int i = 0; i < sweep_Params.Length; i++)
			{
				if (sweep_Params[i].Sweep_Type != 4 && sweep_Params[i].Sweep_Type != 3 && sweep_Params[i].Sweep_Type != 8 && sweep_Params[i].Sweep_Type != 11)
				{
					continue;
				}
				int rxDeviceID = sweep_Params[i].Device_Selection.GetRxDeviceID(chain);
				if (rxDeviceID != -1)
				{
					RxPowerAction rxPowerAction = new RxPowerAction();
					if (sweep_Params[i].Sweep_Type == 8 || sweep_Params[i].Sweep_Type == 11)
					{
						rxPowerAction.Ref_Chan = GetElementFromList(0, sweep_Params[i].XPT_Fcomp_Ref_Channel_List);
					}
					else
					{
						rxPowerAction.Ref_Chan = GetElementFromList(0, sweep_Params[i].Rx_Channel_List);
					}
					rxPowerAction.DLPower = GetElementFromList(0, sweep_Params[i].DL_Level_List);
					rxPowerAction.Device = rxDeviceID;
					return rxPowerAction;
				}
			}
		}
		return null;
	}

	public List<TxPowerAction> GetMaxTxPowerActionFromAllTechnologies()
	{
		List<TxPowerAction> maxTxPowerAction = GetMaxTxPowerAction(Tech.CDMA, m_CalV4);
		List<TxPowerAction> maxTxPowerAction2 = GetMaxTxPowerAction(Tech.LTE, m_CalV4);
		List<TxPowerAction> maxTxPowerAction3 = GetMaxTxPowerAction(Tech.WCDMA, m_CalV4);
		List<TxPowerAction> maxTxPowerAction4 = GetMaxTxPowerAction(Tech.TDSCDMA, m_CalV4);
		List<TxPowerAction> list = new List<TxPowerAction>();
		if (maxTxPowerAction != null)
		{
			list.AddRange(maxTxPowerAction);
		}
		if (maxTxPowerAction2 != null)
		{
			list.AddRange(maxTxPowerAction2);
		}
		if (maxTxPowerAction3 != null)
		{
			list.AddRange(maxTxPowerAction3);
		}
		if (maxTxPowerAction4 != null)
		{
			list.AddRange(maxTxPowerAction4);
		}
		return list;
	}

	public List<TxPowerAction> GetMaxTxPowerAction(Tech tech)
	{
		return GetMaxTxPowerAction(tech, m_CalV4);
	}

	public int GetModeID(FTM_PHONE_MODE_ENUM rf_mode_ID, int device)
	{
		switch (rf_mode_ID)
		{
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_800:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1900:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1800:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_J_CDMA:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_450:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_IMT:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1900_EXT:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_BC11:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_BC15:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_800_SEC:
			switch (device)
			{
			case 0:
				return 0;
			case 2:
				return 32;
			}
			break;
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_IMT:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1900A:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1900B:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_800:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_800A:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1800:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC4:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC8:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC9:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1500:
			return device switch
			{
				0 => 1, 
				2 => 56, 
				_ => 1, 
			};
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC1:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC7:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC13:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC17:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC38:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC40:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC11:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC4:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B2:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B3:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B5:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B6:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B8:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B9:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B10:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B12:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B14:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B15:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B16:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B18:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B19:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B20:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B21:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B22:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B23:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B24:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B25:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B26:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B27:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B28:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B29:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B30:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B31:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B32:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B33:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B34:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B35:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B36:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B37:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B39:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B41:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B42:
			if (device == 0 || device == 2)
			{
				return 29;
			}
			return 29;
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B34:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B39:
		case FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B40:
			return device switch
			{
				0 => 42, 
				2 => 99, 
				_ => 42, 
			};
		}
		return 0;
	}

	public static bool IsTech(Tech tech, FTM_PHONE_MODE_ENUM rf_mode_ID)
	{
		bool result = false;
		switch (tech)
		{
		case Tech.CDMA:
			if (rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_800 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1900 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1800 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_J_CDMA || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_450 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_IMT || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_1900_EXT || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_BC11 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_BC15 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_CDMA_800_SEC)
			{
				result = true;
			}
			break;
		case Tech.WCDMA:
			if (rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_IMT || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1900A || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1900B || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_800 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_800A || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1800 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC4 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC8 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC9 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_1500 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_WCDMA_BC19)
			{
				result = true;
			}
			break;
		case Tech.LTE:
			if (rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC1 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC7 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC4 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC11 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC13 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC17 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC38 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_BC40 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B2 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B3 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B5 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B6 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B8 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B9 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B10 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B12 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B14 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B15 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B16 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B18 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B19 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B20 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B21 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B22 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B23 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B24 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B25 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B26 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B27 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B28 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B29 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B30 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B31 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B32 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B33 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B34 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B35 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B36 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B37 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B39 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_LTE_B41)
			{
				result = true;
			}
			break;
		case Tech.TDSCDMA:
			if (rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B34 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B39 || rf_mode_ID == FTM_PHONE_MODE_ENUM.PHONE_MODE_TDSCDMA_B40)
			{
				result = true;
			}
			break;
		}
		return result;
	}

	public bool IsCalInstance0(string Node_Name)
	{
		string[] array = Node_Name.Split('_');
		if (array[3] == "1")
		{
			return false;
		}
		return true;
	}

	private int GetMaxPowerRGI(string RGI_List)
	{
		char[] separator = new char[4] { ',', ' ', '(', ';' };
		int result = 0;
		string[] array = RGI_List.Split(separator);
		if (short.TryParse(array[1], out var result2))
		{
			result = result2;
		}
		return result;
	}

	private int GetElementFromList(int index, string list)
	{
		if (list != null)
		{
			char[] separator = new char[4] { ',', ' ', '(', ';' };
			string[] array = list.Split(separator);
			if (int.TryParse(array[index], out var result))
			{
				return result;
			}
			return 0;
		}
		return 0;
	}

	private TxPowerAction GetMaxPowerFromSweep(Sweep_Params sweep_param)
	{
		TxPowerAction txPowerAction = new TxPowerAction();
		txPowerAction.Ref_Chan = sweep_param.Ref_Channel;
		txPowerAction.PA_State = GetElementFromList(0, sweep_param.PA_State_List);
		txPowerAction.RGI = GetMaxPowerRGI(sweep_param.RGI_List);
		txPowerAction.Bias = GetElementFromList(0, sweep_param.Bias_List);
		txPowerAction.PA_Quiescent_Current = GetElementFromList(0, sweep_param.PA_Quiescent_Current);
		txPowerAction.IQGain = GetElementFromList(0, sweep_param.IQ_Gain);
		txPowerAction.EnvGain = GetElementFromList(0, sweep_param.Env_Scale);
		txPowerAction.Max_Expected_Pwr = GetElementFromList(0, sweep_param.Max_Expected_Pwr);
		txPowerAction.Min_Start_Pwr = GetElementFromList(0, sweep_param.Min_Start_Pwr);
		return txPowerAction;
	}

	private TxPowerAction GetMaxTxPowerActionFromSweepParams(Sweep_Params[] sweep_params)
	{
		TxPowerAction txPowerAction = new TxPowerAction();
		bool flag = false;
		if (sweep_params != null)
		{
			for (int i = 0; i < sweep_params.Length; i++)
			{
				if (sweep_params[i].Sweep_Type == 0 || sweep_params[i].Sweep_Type == 2)
				{
					flag = true;
					txPowerAction = GetMaxPowerFromSweep(sweep_params[i]);
					if (sweep_params[i].Waveform != null)
					{
						txPowerAction.LTE_waveform = new Waveform();
						txPowerAction.LTE_waveform.Waveform_Type = sweep_params[i].Waveform.Waveform_Type;
						txPowerAction.LTE_waveform.Number_Of_RBs = sweep_params[i].Waveform.Number_Of_RBs;
						txPowerAction.LTE_waveform.Rx_BW = sweep_params[i].Waveform.Rx_BW;
						txPowerAction.LTE_waveform.Tx_BW = sweep_params[i].Waveform.Tx_BW;
						txPowerAction.LTE_waveform.Start_RB = sweep_params[i].Waveform.Start_RB;
					}
					break;
				}
			}
			if (!flag)
			{
				txPowerAction = null;
			}
			return txPowerAction;
		}
		return null;
	}

	private List<TxPowerAction> GetMaxTxPowerAction(Tech tech, CalV4 calV4)
	{
		List<TxPowerAction> list = new List<TxPowerAction>();
		for (int i = 0; i < calV4.Node_Name.Length; i++)
		{
			if (IsTech(tech, (FTM_PHONE_MODE_ENUM)calV4.Node_Name[i].RF_Mode))
			{
				TxPowerAction txPowerAction = new TxPowerAction();
				txPowerAction = GetMaxTxPowerActionFromSweepParams(calV4.Node_Name[i].Sweep_Params);
				if (txPowerAction != null)
				{
					txPowerAction.modeID = GetModeID((FTM_PHONE_MODE_ENUM)calV4.Node_Name[i].RF_Mode, calV4.Node_Name[i].Sweep_Params[0].Device_Selection.Master_Device_ID);
					txPowerAction.Device = calV4.Node_Name[i].Sweep_Params[0].Device_Selection.Master_Device_ID;
					txPowerAction.RFmode_ID = calV4.Node_Name[i].RF_Mode;
					txPowerAction.Description = calV4.Node_Name[i].name;
					list.Add(txPowerAction);
				}
			}
		}
		return list;
	}
}
