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

namespace QCRPG.TSL.BT.Packets;

public class ReadLocalExtendedFeatures : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PageNumberEnum
		{
			Requests_the_normal_LMP_features,
			Return_the_corresponding_page_of_features,
			InvalidPageNumberValue
		}

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

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

			public static string PageNumberToString(PageNumberEnum enumItem)
			{
				return enumItem switch
				{
					PageNumberEnum.Requests_the_normal_LMP_features => "Requests the normal LMP features as returned by Read_Local_Supported_Features.", 
					PageNumberEnum.Return_the_corresponding_page_of_features => "Return the corresponding page of features.", 
					_ => "Invalid PageNumber Value", 
				};
			}

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

		private uint _PageNumber;

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

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

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

	private const uint _Opcode = 4100u;

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

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

	public uint Opcode => 4100u;

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