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

namespace QCRPG.TSL.BT.Packets;

public class WritePageScanType : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PageScanTypeEnum
		{
			Mandatory_Standard_Scan,
			Optional_Interlaced_Scan,
			Reserved,
			InvalidPageScanTypeValue
		}

		public static class PageScanTypeInfo
		{
			public static PageScanTypeEnum ValueToPageScanType(uint value)
			{
				switch (value)
				{
				case 0u:
					return PageScanTypeEnum.Mandatory_Standard_Scan;
				case 1u:
					return PageScanTypeEnum.Optional_Interlaced_Scan;
				default:
					if (value >= 2 && value <= 255)
					{
						return PageScanTypeEnum.Reserved;
					}
					return PageScanTypeEnum.InvalidPageScanTypeValue;
				}
			}

			public static uint PageScanTypeToValue(PageScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					PageScanTypeEnum.Mandatory_Standard_Scan => 0u, 
					PageScanTypeEnum.Optional_Interlaced_Scan => 1u, 
					PageScanTypeEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PageScanTypeToString(PageScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					PageScanTypeEnum.Mandatory_Standard_Scan => "Mandatory_Standard_Scan", 
					PageScanTypeEnum.Optional_Interlaced_Scan => "Optional_Interlaced_Scan", 
					PageScanTypeEnum.Reserved => "Reserved", 
					_ => "Invalid PageScanType Value", 
				};
			}

			public static string PageScanTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Mandatory_Standard_Scan");
				stringBuilder.AppendLine("0x1 = Optional_Interlaced_Scan");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _PageScanType;

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

		public PageScanTypeEnum GetPageScanTypeAsPageScanTypeEnum()
		{
			return PageScanTypeInfo.ValueToPageScanType(_PageScanType);
		}

		public void SetPageScanTypeAsPageScanTypeEnum(PageScanTypeEnum enumItem)
		{
			PageScanType = PageScanTypeInfo.PageScanTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 3143u;

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

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

	public uint Opcode => 3143u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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