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

namespace QCRPG.TSL.BT.Packets;

public class LeMultiAdvtStateChange : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum StateChangeReasonEnum
		{
			Connection_received,
			InvalidStateChangeReasonValue
		}

		public static class StateChangeReasonInfo
		{
			public static StateChangeReasonEnum ValueToStateChangeReason(uint value)
			{
				if (value == 0)
				{
					return StateChangeReasonEnum.Connection_received;
				}
				return StateChangeReasonEnum.InvalidStateChangeReasonValue;
			}

			public static uint StateChangeReasonToValue(StateChangeReasonEnum enumItem)
			{
				if (enumItem == StateChangeReasonEnum.Connection_received)
				{
					return 0u;
				}
				return uint.MaxValue;
			}

			public static string StateChangeReasonToString(StateChangeReasonEnum enumItem)
			{
				if (enumItem == StateChangeReasonEnum.Connection_received)
				{
					return "Connection received";
				}
				return "Invalid StateChangeReason Value";
			}

			public static string StateChangeReasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Connection received");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubEventCode = 85u;

		private uint _AdvertisingInstance;

		private uint _StateChangeReason;

		private uint _ConnectionHandle;

		public uint SubEventCode => 85u;

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

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

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

		public StateChangeReasonEnum GetStateChangeReasonAsStateChangeReasonEnum()
		{
			return StateChangeReasonInfo.ValueToStateChangeReason(_StateChangeReason);
		}

		public void SetStateChangeReasonAsStateChangeReasonEnum(StateChangeReasonEnum enumItem)
		{
			StateChangeReason = StateChangeReasonInfo.StateChangeReasonToValue(enumItem);
		}
	}

	private const uint _EventCode = 255u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new LeMultiAdvtStateChangeInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 255u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubEventCode)
		{
			return false;
		}
		UserFields.AdvertisingInstance = bitBuilder.RemoveBitsAsUint(8);
		UserFields.StateChangeReason = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubEventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingInstance, 8);
		bitBuilder.AddBitsAsUint(UserFields.StateChangeReason, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
