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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForReadLocalExtendedFeatures : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PageNumberEnum
		{
			The_normal_LMP_features,
			The_page_number_of_the_features_returned,
			InvalidPageNumberValue
		}

		public static class PageNumberInfo
		{
			public static PageNumberEnum ValueToPageNumber(uint value)
			{
				if (value == 0)
				{
					return PageNumberEnum.The_normal_LMP_features;
				}
				if (value >= 1 && value <= 255)
				{
					return PageNumberEnum.The_page_number_of_the_features_returned;
				}
				return PageNumberEnum.InvalidPageNumberValue;
			}

			public static uint PageNumberToValue(PageNumberEnum enumItem)
			{
				return enumItem switch
				{
					PageNumberEnum.The_normal_LMP_features => 0u, 
					PageNumberEnum.The_page_number_of_the_features_returned => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PageNumberToString(PageNumberEnum enumItem)
			{
				return enumItem switch
				{
					PageNumberEnum.The_normal_LMP_features => "The normal LMP features as returned by Read_Local_Supported_Features.", 
					PageNumberEnum.The_page_number_of_the_features_returned => "The page number of the features returned.", 
					_ => "Invalid PageNumber Value", 
				};
			}

			public static string PageNumberToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The normal LMP features as returned by Read_Local_Supported_Features.");
				stringBuilder.AppendLine("0x1-0xFF = The page number of the features returned.");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 4100u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _PageNumber;

		private uint _MaximumPageNumber;

		private ulong _ExtendedLmpFeatures;

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

		public uint CommandOpcode => 4100u;

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

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

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

		public ulong ExtendedLmpFeatures
		{
			get
			{
				return _ExtendedLmpFeatures;
			}
			set
			{
				_ExtendedLmpFeatures = value;
			}
		}

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(4100u);
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public PageNumberEnum GetPageNumberAsPageNumberEnum()
		{
			return PageNumberInfo.ValueToPageNumber(_PageNumber);
		}

		public void SetPageNumberAsPageNumberEnum(PageNumberEnum enumItem)
		{
			PageNumber = PageNumberInfo.PageNumberToValue(enumItem);
		}

		public SupportedFeaturesEnumCollection GetExtendedLmpFeaturesAsSupportedFeaturesEnumCollection()
		{
			if (_PageNumber != 0)
			{
				throw new Exception("Page Number is not 0x00, can not get Extended LMP Features as SupportedFeaturesEnumCollection");
			}
			return SupportedFeaturesInfo.ValueToSupportedFeatures(_ExtendedLmpFeatures);
		}

		public void SetExtendedLmpFeaturesAsSupportedFeaturesEnumCollection(SupportedFeaturesEnumCollection enumCollection)
		{
			if (_PageNumber != 0)
			{
				throw new Exception("Page Number is not 0x00, can not set Extended LMP Features as SupportedFeaturesEnumCollection");
			}
			ExtendedLmpFeatures = SupportedFeaturesInfo.SupportedFeaturesToValue(enumCollection);
		}

		public ExtendedFeaturesPage1EnumCollection GetExtendedLmpFeaturesAsExtendedFeaturesPage1EnumCollection()
		{
			if (_PageNumber != 1)
			{
				throw new Exception("Page Number is not 0x01, can not get Extended LMP Features as ExtendedFeaturesPage1EnumCollection");
			}
			return ExtendedFeaturesPage1Info.ValueToExtendedFeaturesPage1(_ExtendedLmpFeatures);
		}

		public void SetExtendedLmpFeaturesAsExtendedFeaturesPage1EnumCollection(ExtendedFeaturesPage1EnumCollection enumCollection)
		{
			if (_PageNumber != 1)
			{
				throw new Exception("Page Number is not 0x01, can not set Extended LMP Features as ExtendedFeaturesPage1EnumCollection");
			}
			ExtendedLmpFeatures = ExtendedFeaturesPage1Info.ExtendedFeaturesPage1ToValue(enumCollection);
		}

		public ExtendedFeaturesPage2EnumCollection GetExtendedLmpFeaturesAsExtendedFeaturesPage2EnumCollection()
		{
			if (_PageNumber != 2)
			{
				throw new Exception("Page Number is not 0x02, can not get Extended LMP Features as ExtendedFeaturesPage2EnumCollection");
			}
			return ExtendedFeaturesPage2Info.ValueToExtendedFeaturesPage2(_ExtendedLmpFeatures);
		}

		public void SetExtendedLmpFeaturesAsExtendedFeaturesPage2EnumCollection(ExtendedFeaturesPage2EnumCollection enumCollection)
		{
			if (_PageNumber != 2)
			{
				throw new Exception("Page Number is not 0x02, can not set Extended LMP Features as ExtendedFeaturesPage2EnumCollection");
			}
			ExtendedLmpFeatures = ExtendedFeaturesPage2Info.ExtendedFeaturesPage2ToValue(enumCollection);
		}
	}

	private const uint _EventCode = 14u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint EventCode => 14u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumHciCommandPackets = 1u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PageNumber = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MaximumPageNumber = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ExtendedLmpFeatures = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.PageNumber, 8);
		bitBuilder.AddBitsAsUint(UserFields.MaximumPageNumber, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.ExtendedLmpFeatures, 64);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
