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

namespace QFDT_Components;

public partial class RFFE_Scan : UserControl, IComponentConnector
{
	private delegate void PrintDebugMessageCB(uint time, ushort logLevel, string msg);

	private const int vendorQualcomm = 535;

	private const int vendorSony = 432;

	private const int vendorAvago = 263;

	private const int vendorRFMicro = 308;

	private const int vendorMurata = 524;

	private const int vendorInfineon = 394;

	private const int vendorSkyworks = 421;

	private const int vendorTDK = 584;

	private const int vendorTriquint = 646;

	private const int vendorRFMD = 308;

	private const string UNKNOWN = "";

	public Phone phone;

	public RFFE_Scan()
	{
		InitializeComponent();
	}

	private void DisplayReport_Click(object sender, RoutedEventArgs e)
	{
		txtDebugMessage.Document.Blocks.Clear();
		if (phone != null)
		{
			string text = "c:\\Qualcomm\\QDART\\QFDT\\RFFE_SCAN.dat";
			try
			{
				phone.EFSReadFile(text, "/rfc/rffe_scan/rffe_scan.dat");
				ParseRFFEScanFile(text);
			}
			catch
			{
				PrintDebugMessage(0u, 0, "Failed to read /rfc/rffe_scan/rffe_scan.dat file from device.  Your device does not support this feature.");
			}
		}
	}

	private void ParseRFFEScanFile(string path)
	{
		byte[] array = new byte[2048];
		long length;
		using (FileStream fileStream = new FileStream(path, FileMode.Open))
		{
			fileStream.Seek(0L, SeekOrigin.Begin);
			length = fileStream.Length;
			for (int i = 0; i < fileStream.Length; i++)
			{
				array[i] = (byte)fileStream.ReadByte();
			}
			fileStream.Close();
		}
		string msg = "File Size = " + length;
		PrintDebugMessage(0u, 0, msg);
		msg = "File Version = " + array[0];
		PrintDebugMessage(0u, 0, msg);
		if (array[0] == 1 || array[0] == 2)
		{
			msg = "RFHW ID = " + BitConverter.ToUInt32(array, 12);
			PrintDebugMessage(0u, 0, msg);
			msg = "Number of Device Instances = " + array[16];
			PrintDebugMessage(0u, 0, msg);
			msg = "Number of Device Instances Present = " + array[20];
			PrintDebugMessage(0u, 0, msg);
			if (array[0] == 2)
			{
				msg = "Error Code = " + array[24];
				PrintDebugMessage(0u, 0, msg);
			}
			long num = 44L;
			long num2 = 24L;
			if (array[0] == 1)
			{
				num2 = 24L;
			}
			else if (array[0] == 2)
			{
				num2 = 28L;
			}
			long num3 = (length - num2) / num;
			msg = "";
			PrintDebugMessage(0u, 0, msg);
			msg = "Dev Inst, BUS, MID,        PID,           P_REV, USID, Status";
			PrintDebugMessage(0u, 0, msg);
			msg = "=============================================================";
			PrintDebugMessage(0u, 0, msg);
			for (int j = 0; j < num3; j++)
			{
				msg = array[num2 + j * num + 4].ToString().PadRight(3) + ",";
				msg = msg + array[num2 + j * num + 12].ToString().PadRight(3) + ",";
				int ManufactureID = 0;
				msg = msg + ConvertToMID(array[num2 + j * num + 17], array[num2 + j * num + 16], ref ManufactureID) + ", ";
				msg = msg + ConvertPIDToString(array[num2 + j * num + 21], array[num2 + j * num + 20], ManufactureID) + ", ";
				msg = msg + $"0x{array[num2 + j * num + 24]:X}".PadRight(2) + ",";
				msg = msg + $"0x{array[num2 + j * num + 36]:X}".PadRight(2) + ",";
				msg += ConvertDeviceStatus(array[num2 + j * num + 40]);
				PrintDebugMessage(0u, 0, msg);
			}
		}
		else
		{
			msg = "Unsupported file version number";
			PrintDebugMessage(0u, 0, msg);
		}
	}

	private string ConvertDeviceStatus(byte status)
	{
		return status switch
		{
			0 => "PRESENT", 
			1 => "MISSING_NON_FATAL", 
			2 => "MISSING_FATAL", 
			_ => "UNKNOWN STATUS", 
		};
	}

	private string ConvertToMID(byte MSB, byte LSB, ref int ManufactureID)
	{
		int num = MSB << 8;
		num = (ManufactureID = num + LSB);
		string text = "";
		text = num switch
		{
			535 => "QUALCOMM", 
			308 => "RF MICRO", 
			432 => "SONY", 
			263 => "AVAGO", 
			524 => "MURATA", 
			394 => "INFINEON", 
			421 => "SKYWORKS", 
			584 => "TDK", 
			646 => "TRIQUINT", 
			_ => (num != 308) ? "" : "RFMD", 
		} + $"(0x{num:X})";
		return text.PadRight(15);
	}

	private string ConvertPIDToString(byte PID_MSB, byte PID_LSB, int MID)
	{
		int num = PID_MSB << 8;
		num += PID_LSB;
		string text = "";
		switch (MID)
		{
		case 535:
			text = num switch
			{
				192 => "WTR3605/3925", 
				200 => "WTR4905", 
				201 => "WTR3950(LTEU)", 
				204 => "WTR3956", 
				203 => "WTR5975", 
				202 => "WTR2955", 
				16 => "QFE1040", 
				17 => "QFE1045", 
				18 => "QFE1035", 
				19 => "QFE1055", 
				0 => "QFE2035FC", 
				513 => "WTL5000", 
				80 => "QFE1510 v2.0", 
				1 => "QFE1520", 
				2 => "QFE1550", 
				3 => "QFE4100/2520(dep)", 
				4 => "QFE2550", 
				5 => "QFE3510", 
				49 => "QFE2101", 
				32 => "QFE2310", 
				33 => "QFE2320/30/40", 
				35 => "QFE3345/E", 
				36 => "QFE3340FC", 
				37 => "QFE3330", 
				38 => "QFE3320", 
				41 => "QFE3350", 
				50 => "QFE3100", 
				64 => "QFE4335", 
				65 => "QFE4345", 
				292 => "QFE3340FC", 
				517 => "QLN1020", 
				518 => "QLN1030", 
				_ => "", 
			} + $"(0x{num:X})";
			break;
		case 432:
			switch (num)
			{
			case 38:
				text = "ASM_SP4T";
				break;
			case 0:
			case 2:
				text = "ASM_SP4T";
				break;
			default:
				text = "";
				break;
			}
			text += $"(0x{num:X})";
			break;
		case 263:
			text = num switch
			{
				3 => "PA_B13", 
				7 => "PA_B34_B39", 
				131 => "HB_PA", 
				_ => "", 
			} + $"(0x{num:X})";
			break;
		case 308:
			switch (num)
			{
			case 24:
				text = "PA_B7";
				break;
			case 25:
				text = "PA_B17";
				break;
			case 44:
				text = "ASM_SP4T";
				break;
			case 27:
			case 82:
				text = "PA_B17_B28";
				break;
			case 26:
				text = "ASM_SP10T";
				break;
			case 6:
			case 29:
				text = "PA_B11_B21";
				break;
			case 21:
				text = "PA_B21";
				break;
			case 43:
				text = "PA_B41";
				break;
			default:
				text = num switch
				{
					44 => "PA_B42", 
					41 => "PA_LTEU", 
					_ => "", 
				};
				break;
			}
			text += $"(0x{num:X})";
			break;
		case 524:
			switch (num)
			{
			case 37:
			case 129:
				text = "ASM_SP12T";
				break;
			case 136:
				text = "ASM_SP10T";
				break;
			case 24:
			case 32:
			case 139:
				text = "ASM_SP6T";
				break;
			case 131:
				text = "ASM_SP4T";
				break;
			default:
				text = "";
				break;
			}
			text += $"(0x{num:X})";
			break;
		case 421:
			text = ((num != 87) ? "" : "GSM_PA");
			text += $"(0x{num:X})";
			break;
		case 646:
			text = ((num != 16) ? "" : "B42_PA");
			text += $"(0x{num:X})";
			break;
		default:
			text += $"(0x{num:X})";
			break;
		}
		return text.PadRight(20);
	}

	public void PrintQMSLTextLog(uint time, ushort logLevel, string msg)
	{
		PrintDebugMessage(time, logLevel, msg);
	}

	public void PrintDebugMessage(uint time, ushort logLevel, string msg)
	{
		if (txtDebugMessage.Dispatcher.CheckAccess())
		{
			new Run(msg + "\r\n", txtDebugMessage.CaretPosition.DocumentEnd);
			txtDebugMessage.ScrollToEnd();
			return;
		}
		PrintDebugMessageCB method = PrintDebugMessage;
		base.Dispatcher.BeginInvoke(method, time, logLevel, msg);
	}
}
