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

namespace QCRPG.TSL.BT.Packets;

public class LegacyAssociationNotification : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AssociationEnableEnum
		{
			Associate_disable,
			Associate_enable,
			InvalidAssociationEnableValue
		}

		public static class AssociationEnableInfo
		{
			public static AssociationEnableEnum ValueToAssociationEnable(uint value)
			{
				if (value == 0)
				{
					return AssociationEnableEnum.Associate_disable;
				}
				if (value >= 1 && value <= 255)
				{
					return AssociationEnableEnum.Associate_enable;
				}
				return AssociationEnableEnum.InvalidAssociationEnableValue;
			}

			public static uint AssociationEnableToValue(AssociationEnableEnum enumItem)
			{
				return enumItem switch
				{
					AssociationEnableEnum.Associate_disable => 0u, 
					AssociationEnableEnum.Associate_enable => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AssociationEnableToString(AssociationEnableEnum enumItem)
			{
				return enumItem switch
				{
					AssociationEnableEnum.Associate_disable => "Associate disable", 
					AssociationEnableEnum.Associate_enable => "Associate enable", 
					_ => "Invalid AssociationEnable Value", 
				};
			}

			public static string AssociationEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Associate disable");
				stringBuilder.AppendLine("0x1-0xFF = Associate enable");
				return stringBuilder.ToString();
			}
		}

		private uint _AssociationEnable;

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

		public AssociationEnableEnum GetAssociationEnableAsAssociationEnableEnum()
		{
			return AssociationEnableInfo.ValueToAssociationEnable(_AssociationEnable);
		}

		public void SetAssociationEnableAsAssociationEnableEnum(AssociationEnableEnum enumItem)
		{
			AssociationEnable = AssociationEnableInfo.AssociationEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 64531u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLegacyAssociationNotification),
		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 LegacyAssociationNotificationInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64531u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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