using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class ProdTestRxContinuous : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TechTypeEnum
		{
			BR_EDR,
			BLE,
			InvalidTechTypeValue
		}

		public static class TechTypeInfo
		{
			public static TechTypeEnum ValueToTechType(uint value)
			{
				return value switch
				{
					0u => TechTypeEnum.BR_EDR, 
					1u => TechTypeEnum.BLE, 
					_ => TechTypeEnum.InvalidTechTypeValue, 
				};
			}

			public static uint TechTypeToValue(TechTypeEnum enumItem)
			{
				return enumItem switch
				{
					TechTypeEnum.BR_EDR => 0u, 
					TechTypeEnum.BLE => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TechTypeToString(TechTypeEnum enumItem)
			{
				return enumItem switch
				{
					TechTypeEnum.BR_EDR => "BR/EDR", 
					TechTypeEnum.BLE => "BLE", 
					_ => "Invalid TechType Value", 
				};
			}

			public static string TechTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = BR/EDR");
				stringBuilder.AppendLine("0x1 = BLE");
				return stringBuilder.ToString();
			}
		}

		public enum SwGainEnum
		{
			HIGH,
			NORMAL,
			LOW,
			LOW_MINUS,
			HIGH_PLUS,
			InvalidSwGainValue
		}

		public static class SwGainInfo
		{
			public static SwGainEnum ValueToSwGain(uint value)
			{
				return value switch
				{
					0u => SwGainEnum.HIGH, 
					1u => SwGainEnum.NORMAL, 
					2u => SwGainEnum.LOW, 
					3u => SwGainEnum.LOW_MINUS, 
					4u => SwGainEnum.HIGH_PLUS, 
					_ => SwGainEnum.InvalidSwGainValue, 
				};
			}

			public static uint SwGainToValue(SwGainEnum enumItem)
			{
				return enumItem switch
				{
					SwGainEnum.HIGH => 0u, 
					SwGainEnum.NORMAL => 1u, 
					SwGainEnum.LOW => 2u, 
					SwGainEnum.LOW_MINUS => 3u, 
					SwGainEnum.HIGH_PLUS => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SwGainToString(SwGainEnum enumItem)
			{
				return enumItem switch
				{
					SwGainEnum.HIGH => "HIGH", 
					SwGainEnum.NORMAL => "NORMAL", 
					SwGainEnum.LOW => "LOW", 
					SwGainEnum.LOW_MINUS => "LOW MINUS", 
					SwGainEnum.HIGH_PLUS => "HIGH PLUS", 
					_ => "Invalid SwGain Value", 
				};
			}

			public static string SwGainToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = HIGH");
				stringBuilder.AppendLine("0x1 = NORMAL");
				stringBuilder.AppendLine("0x2 = LOW");
				stringBuilder.AppendLine("0x3 = LOW MINUS");
				stringBuilder.AppendLine("0x4 = HIGH PLUS");
				return stringBuilder.ToString();
			}
		}

		public enum LePhyEnum
		{
			ONE_M,
			TWO_M,
			CODED_PHY_S_8,
			CODED_PHY_S_2,
			HIGH_PLUS,
			InvalidLePhyValue
		}

		public static class LePhyInfo
		{
			public static LePhyEnum ValueToLePhy(uint value)
			{
				return value switch
				{
					0u => LePhyEnum.ONE_M, 
					1u => LePhyEnum.TWO_M, 
					2u => LePhyEnum.CODED_PHY_S_8, 
					3u => LePhyEnum.CODED_PHY_S_2, 
					4u => LePhyEnum.HIGH_PLUS, 
					_ => LePhyEnum.InvalidLePhyValue, 
				};
			}

			public static uint LePhyToValue(LePhyEnum enumItem)
			{
				return enumItem switch
				{
					LePhyEnum.ONE_M => 0u, 
					LePhyEnum.TWO_M => 1u, 
					LePhyEnum.CODED_PHY_S_8 => 2u, 
					LePhyEnum.CODED_PHY_S_2 => 3u, 
					LePhyEnum.HIGH_PLUS => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LePhyToString(LePhyEnum enumItem)
			{
				return enumItem switch
				{
					LePhyEnum.ONE_M => "LE_1M", 
					LePhyEnum.TWO_M => "LE_2M", 
					LePhyEnum.CODED_PHY_S_8 => "CODED PHY S=8", 
					LePhyEnum.CODED_PHY_S_2 => "CODED PHY S=2", 
					LePhyEnum.HIGH_PLUS => "HIGH PLUS", 
					_ => "Invalid LePhy Value", 
				};
			}

			public static string LePhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = LE_1M");
				stringBuilder.AppendLine("0x1 = LE_2M");
				stringBuilder.AppendLine("0x2 = CODED PHY S=8");
				stringBuilder.AppendLine("0x3 = CODED PHY S=2");
				stringBuilder.AppendLine("0x4 = HIGH PLUS");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 30u;

		private uint _TechType;

		private uint _Channel;

		private uint _SwGain;

		private List<uint> _LePhyList = new List<uint>();

		public uint SubOpcode => 30u;

		public uint TechType
		{
			get
			{
				return _TechType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for TechType is 0xFF");
				}
				_TechType = value;
				uint num = 0u;
				if (_TechType == 0)
				{
					num = 0u;
				}
				else if (_TechType == 1)
				{
					num = 1u;
				}
				while (_LePhyList.Count < num)
				{
					_LePhyList.Add(0u);
				}
				while (_LePhyList.Count > num)
				{
					_LePhyList.RemoveAt(_LePhyList.Count - 1);
				}
			}
		}

		public uint Channel
		{
			get
			{
				return _Channel;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Channel is 0xFF");
				}
				_Channel = value;
			}
		}

		public uint SwGain
		{
			get
			{
				return _SwGain;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SwGain is 0xFF");
				}
				_SwGain = value;
			}
		}

		public List<uint> LePhyList => _LePhyList;

		public TechTypeEnum GetTechTypeAsTechTypeEnum()
		{
			return TechTypeInfo.ValueToTechType(_TechType);
		}

		public void SetTechTypeAsTechTypeEnum(TechTypeEnum enumItem)
		{
			TechType = TechTypeInfo.TechTypeToValue(enumItem);
		}

		public SwGainEnum GetSwGainAsSwGainEnum()
		{
			return SwGainInfo.ValueToSwGain(_SwGain);
		}

		public void SetSwGainAsSwGainEnum(SwGainEnum enumItem)
		{
			SwGain = SwGainInfo.SwGainToValue(enumItem);
		}

		public LePhyEnum GetLePhyAsLePhyEnum(int index)
		{
			return LePhyInfo.ValueToLePhy(_LePhyList[index]);
		}

		public void SetLePhyAsLePhyEnum(int index, LePhyEnum enumItem)
		{
			_LePhyList[index] = LePhyInfo.LePhyToValue(enumItem);
		}
	}

	private const uint _Opcode = 64516u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForProdTest) };

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new ProdTestRxContinuousInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64516u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.TechType = 0u;
		UserFields.Channel = 0u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.TechType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SwGain = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.LePhyList.Count; i++)
		{
			UserFields.LePhyList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.TechType, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channel, 8);
		bitBuilder.AddBitsAsUint(UserFields.SwGain, 8);
		for (int i = 0; i < UserFields.LePhyList.Count; i++)
		{
			if (UserFields.LePhyList[i] > 255)
			{
				throw new Exception("Maximum possible value for LePhy is 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.LePhyList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Channel < 0)
		{
			list.Add("Minimum allowed value for Channel is 0x0");
		}
		if (UserFields.Channel > 78)
		{
			list.Add("Maximum allowed value for Channel is 0x4E");
		}
		return list;
	}
}
