using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LtAoxTestMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			DISABLED,
			ENABLED,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				return value switch
				{
					0u => EnableEnum.DISABLED, 
					1u => EnableEnum.ENABLED, 
					_ => EnableEnum.InvalidEnableValue, 
				};
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.DISABLED => 0u, 
					EnableEnum.ENABLED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.DISABLED => "Lower tester disabled", 
					EnableEnum.ENABLED => "Lower tester enabled", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Lower tester disabled");
				stringBuilder.AppendLine("0x1 = Lower tester enabled");
				return stringBuilder.ToString();
			}
		}

		public enum AoxTestModeEnum
		{
			SendUnrequested_Supp_Response,
			NAK_on_AoA,
			CRC_Error,
			InvalidAoxTestModeValue
		}

		public static class AoxTestModeInfo
		{
			public static AoxTestModeEnum ValueToAoxTestMode(uint value)
			{
				return value switch
				{
					1u => AoxTestModeEnum.SendUnrequested_Supp_Response, 
					2u => AoxTestModeEnum.NAK_on_AoA, 
					4u => AoxTestModeEnum.CRC_Error, 
					_ => AoxTestModeEnum.InvalidAoxTestModeValue, 
				};
			}

			public static uint AoxTestModeToValue(AoxTestModeEnum enumItem)
			{
				return enumItem switch
				{
					AoxTestModeEnum.SendUnrequested_Supp_Response => 1u, 
					AoxTestModeEnum.NAK_on_AoA => 2u, 
					AoxTestModeEnum.CRC_Error => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AoxTestModeToString(AoxTestModeEnum enumItem)
			{
				return enumItem switch
				{
					AoxTestModeEnum.SendUnrequested_Supp_Response => "Send unrequested CTE/Supp response", 
					AoxTestModeEnum.NAK_on_AoA => "NAK on AoA response", 
					AoxTestModeEnum.CRC_Error => "CRC error on AoX Tx", 
					_ => "Invalid AoxTestMode Value", 
				};
			}

			public static string AoxTestModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = Send unrequested CTE/Supp response");
				stringBuilder.AppendLine("0x2 = NAK on AoA response");
				stringBuilder.AppendLine("0x4 = CRC error on AoX Tx");
				return stringBuilder.ToString();
			}
		}

		public enum SupplementalLengthEnum
		{
			No_Supplemental,
			RESERVED,
			Supplemental_Length_In_8_us_Units,
			Reserved_For_Future_Use,
			InvalidSupplementalLengthValue
		}

		public static class SupplementalLengthInfo
		{
			public static SupplementalLengthEnum ValueToSupplementalLength(uint value)
			{
				switch (value)
				{
				case 0u:
					return SupplementalLengthEnum.No_Supplemental;
				case 1u:
					return SupplementalLengthEnum.RESERVED;
				default:
					if (value >= 2 && value <= 20)
					{
						return SupplementalLengthEnum.Supplemental_Length_In_8_us_Units;
					}
					if (value >= 21 && value <= 255)
					{
						return SupplementalLengthEnum.Reserved_For_Future_Use;
					}
					return SupplementalLengthEnum.InvalidSupplementalLengthValue;
				}
			}

			public static uint SupplementalLengthToValue(SupplementalLengthEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalLengthEnum.No_Supplemental => 0u, 
					SupplementalLengthEnum.RESERVED => 1u, 
					SupplementalLengthEnum.Supplemental_Length_In_8_us_Units => 2u, 
					SupplementalLengthEnum.Reserved_For_Future_Use => 21u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SupplementalLengthToString(SupplementalLengthEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalLengthEnum.No_Supplemental => "No_Supplemental", 
					SupplementalLengthEnum.RESERVED => "RESERVED", 
					SupplementalLengthEnum.Supplemental_Length_In_8_us_Units => "Supplemental_Length_In_8_us_Units", 
					SupplementalLengthEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid SupplementalLength Value", 
				};
			}

			public static string SupplementalLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No_Supplemental");
				stringBuilder.AppendLine("0x1 = RESERVED");
				stringBuilder.AppendLine("0x2-0x14 = Supplemental_Length_In_8_us_Units");
				stringBuilder.AppendLine("0x15-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 19u;

		private uint _Enable;

		private uint _AoxTestMode;

		private uint _SupplementalType;

		private uint _SupplementalLength;

		public uint SubOpcode => 19u;

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

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

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

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

		public EnableEnum GetEnableAsEnableEnum()
		{
			return EnableInfo.ValueToEnable(_Enable);
		}

		public void SetEnableAsEnableEnum(EnableEnum enumItem)
		{
			Enable = EnableInfo.EnableToValue(enumItem);
		}

		public AoxTestModeEnum GetAoxTestModeAsAoxTestModeEnum()
		{
			return AoxTestModeInfo.ValueToAoxTestMode(_AoxTestMode);
		}

		public void SetAoxTestModeAsAoxTestModeEnum(AoxTestModeEnum enumItem)
		{
			AoxTestMode = AoxTestModeInfo.AoxTestModeToValue(enumItem);
		}

		public AodSupplementalTypeEnum GetSupplementalTypeAsAodSupplementalTypeEnum()
		{
			return AodSupplementalTypeInfo.ValueToAodSupplementalType(_SupplementalType);
		}

		public void SetSupplementalTypeAsAodSupplementalTypeEnum(AodSupplementalTypeEnum enumItem)
		{
			SupplementalType = AodSupplementalTypeInfo.AodSupplementalTypeToValue(enumItem);
		}

		public SupplementalLengthEnum GetSupplementalLengthAsSupplementalLengthEnum()
		{
			return SupplementalLengthInfo.ValueToSupplementalLength(_SupplementalLength);
		}

		public void SetSupplementalLengthAsSupplementalLengthEnum(SupplementalLengthEnum enumItem)
		{
			SupplementalLength = SupplementalLengthInfo.SupplementalLengthToValue(enumItem);
		}
	}

	private const uint _Opcode = 64536u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLowerTester),
		typeof(CommandCompleteForNop)
	};

	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 LtAoxTestModeInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64536u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AoxTestMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalLength = 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.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.AoxTestMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalType, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalLength, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
