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

namespace QCRPG.TSL.BT.Packets;

public class CreateConnection : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AllowRoleSwitchEnum
		{
			LOCAL_WILL_BE_MASTER,
			LOCAL_MAY_BE_A_MASTER,
			RESERVED,
			InvalidAllowRoleSwitchValue
		}

		public static class AllowRoleSwitchInfo
		{
			public static AllowRoleSwitchEnum ValueToAllowRoleSwitch(uint value)
			{
				switch (value)
				{
				case 0u:
					return AllowRoleSwitchEnum.LOCAL_WILL_BE_MASTER;
				case 1u:
					return AllowRoleSwitchEnum.LOCAL_MAY_BE_A_MASTER;
				default:
					if (value >= 2 && value <= 255)
					{
						return AllowRoleSwitchEnum.RESERVED;
					}
					return AllowRoleSwitchEnum.InvalidAllowRoleSwitchValue;
				}
			}

			public static uint AllowRoleSwitchToValue(AllowRoleSwitchEnum enumItem)
			{
				return enumItem switch
				{
					AllowRoleSwitchEnum.LOCAL_WILL_BE_MASTER => 0u, 
					AllowRoleSwitchEnum.LOCAL_MAY_BE_A_MASTER => 1u, 
					AllowRoleSwitchEnum.RESERVED => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AllowRoleSwitchToString(AllowRoleSwitchEnum enumItem)
			{
				return enumItem switch
				{
					AllowRoleSwitchEnum.LOCAL_WILL_BE_MASTER => "LOCAL_WILL_BE_MASTER", 
					AllowRoleSwitchEnum.LOCAL_MAY_BE_A_MASTER => "LOCAL_MAY_BE_A_MASTER", 
					AllowRoleSwitchEnum.RESERVED => "RESERVED", 
					_ => "Invalid AllowRoleSwitch Value", 
				};
			}

			public static string AllowRoleSwitchToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = LOCAL_WILL_BE_MASTER");
				stringBuilder.AppendLine("0x1 = LOCAL_MAY_BE_A_MASTER");
				stringBuilder.AppendLine("0x2-0xFF = RESERVED");
				return stringBuilder.ToString();
			}
		}

		private const uint _Reserved = 0u;

		private ulong _BdAddress;

		private uint _PacketTypeMask;

		private uint _PageScanRepetitionMode;

		private uint _ClockOffset;

		private uint _AllowRoleSwitch;

		public ulong BdAddress
		{
			get
			{
				return _BdAddress;
			}
			set
			{
				_BdAddress = value;
			}
		}

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

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

		public uint Reserved => 0u;

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

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

		public AclPacketTypeEnumCollection GetPacketTypeMaskAsAclPacketTypeEnumCollection()
		{
			return AclPacketTypeInfo.ValueToAclPacketType(_PacketTypeMask);
		}

		public void SetPacketTypeMaskAsAclPacketTypeEnumCollection(AclPacketTypeEnumCollection enumCollection)
		{
			PacketTypeMask = AclPacketTypeInfo.AclPacketTypeToValue(enumCollection);
		}

		public PageScanRepetitionModeEnum GetPageScanRepetitionModeAsPageScanRepetitionModeEnum()
		{
			return PageScanRepetitionModeInfo.ValueToPageScanRepetitionMode(_PageScanRepetitionMode);
		}

		public void SetPageScanRepetitionModeAsPageScanRepetitionModeEnum(PageScanRepetitionModeEnum enumItem)
		{
			PageScanRepetitionMode = PageScanRepetitionModeInfo.PageScanRepetitionModeToValue(enumItem);
		}

		public ClockOffsetEnumCollection GetClockOffsetAsClockOffsetEnumCollection()
		{
			return ClockOffsetInfo.ValueToClockOffset(_ClockOffset);
		}

		public void SetClockOffsetAsClockOffsetEnumCollection(ClockOffsetEnumCollection enumCollection)
		{
			ClockOffset = ClockOffsetInfo.ClockOffsetToValue(enumCollection);
		}

		public AllowRoleSwitchEnum GetAllowRoleSwitchAsAllowRoleSwitchEnum()
		{
			return AllowRoleSwitchInfo.ValueToAllowRoleSwitch(_AllowRoleSwitch);
		}

		public void SetAllowRoleSwitchAsAllowRoleSwitchEnum(AllowRoleSwitchEnum enumItem)
		{
			AllowRoleSwitch = AllowRoleSwitchInfo.AllowRoleSwitchToValue(enumItem);
		}
	}

	private const uint _Opcode = 1029u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForCreateConnection),
		typeof(ConnectionComplete)
	};

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

	public uint Opcode => 1029u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.PacketTypeMask = 52248u;
		UserFields.PageScanRepetitionMode = 1u;
		UserFields.AllowRoleSwitch = 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.BdAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.PacketTypeMask = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.PageScanRepetitionMode = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Reserved)
		{
			return false;
		}
		UserFields.ClockOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AllowRoleSwitch = 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.AddBitsAsUlongLittleEndian(UserFields.BdAddress, 48);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PacketTypeMask, 16);
		bitBuilder.AddBitsAsUint(UserFields.PageScanRepetitionMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Reserved, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ClockOffset, 16);
		bitBuilder.AddBitsAsUint(UserFields.AllowRoleSwitch, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
