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

namespace QCRPG.TSL.BT.Packets;

public class EnhancedFlush : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketTypeEnum
		{
			Automatically_flushable_only,
			Reserved_for_future_use,
			InvalidPacketTypeValue
		}

		public static class PacketTypeInfo
		{
			public static PacketTypeEnum ValueToPacketType(uint value)
			{
				if (value == 0)
				{
					return PacketTypeEnum.Automatically_flushable_only;
				}
				if (value >= 1 && value <= 255)
				{
					return PacketTypeEnum.Reserved_for_future_use;
				}
				return PacketTypeEnum.InvalidPacketTypeValue;
			}

			public static uint PacketTypeToValue(PacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeEnum.Automatically_flushable_only => 0u, 
					PacketTypeEnum.Reserved_for_future_use => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketTypeToString(PacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeEnum.Automatically_flushable_only => "Automatically flushable only", 
					PacketTypeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid PacketType Value", 
				};
			}

			public static string PacketTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Automatically flushable only");
				stringBuilder.AppendLine("0x1-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _Handle;

		private uint _PacketType;

		public uint Handle
		{
			get
			{
				return _Handle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Handle is 0xFFFF");
				}
				_Handle = value;
			}
		}

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

		public PacketTypeEnum GetPacketTypeAsPacketTypeEnum()
		{
			return PacketTypeInfo.ValueToPacketType(_PacketType);
		}

		public void SetPacketTypeAsPacketTypeEnum(PacketTypeEnum enumItem)
		{
			PacketType = PacketTypeInfo.PacketTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 3167u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForEnhancedFlush),
		typeof(EnhancedFlushComplete)
	};

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

	public uint Opcode => 3167u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Handle = 0u;
		UserFields.PacketType = 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.Handle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.PacketType = 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.AddBitsAsUintLittleEndian(UserFields.Handle, 16);
		bitBuilder.AddBitsAsUint(UserFields.PacketType, 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.Handle < 0)
		{
			list.Add("Minimum allowed value for Handle is 0x0");
		}
		if (UserFields.Handle > 3839)
		{
			list.Add("Maximum allowed value for Handle is 0xEFF");
		}
		return list;
	}
}
