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

namespace QCRPG.TSL.BT.Packets;

public class HeadlessModeEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public class LpmWakeupMethodEnumCollection
		{
			private EnableBitEnum _EnableHogpWakeUp;

			private EnableBitEnum _EnableBrEdrWakeUp;

			public EnableBitEnum EnableHogpWakeUp
			{
				get
				{
					return _EnableHogpWakeUp;
				}
				set
				{
					_EnableHogpWakeUp = value;
				}
			}

			public EnableBitEnum EnableBrEdrWakeUp
			{
				get
				{
					return _EnableBrEdrWakeUp;
				}
				set
				{
					_EnableBrEdrWakeUp = value;
				}
			}
		}

		public static class LpmWakeupMethodInfo
		{
			public static LpmWakeupMethodEnumCollection ValueToLpmWakeupMethod(uint value)
			{
				LpmWakeupMethodEnumCollection lpmWakeupMethodEnumCollection = new LpmWakeupMethodEnumCollection();
				lpmWakeupMethodEnumCollection.EnableHogpWakeUp = EnableBitInfo.ValueToEnableBit(value & 1u);
				lpmWakeupMethodEnumCollection.EnableBrEdrWakeUp = EnableBitInfo.ValueToEnableBit((value & 2) >> 1);
				return lpmWakeupMethodEnumCollection;
			}

			public static uint LpmWakeupMethodToValue(LpmWakeupMethodEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.EnableHogpWakeUp);
				return num | (EnableBitInfo.EnableBitToValue(enumCollection.EnableBrEdrWakeUp) << 1);
			}

			public static string LpmWakeupMethodToString(LpmWakeupMethodEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Enable HOGP wake up: " + EnableBitInfo.EnableBitToString(enumCollection.EnableHogpWakeUp) + "\r\n";
				text = text + "Enable BR EDR wake up : " + EnableBitInfo.EnableBitToString(enumCollection.EnableBrEdrWakeUp) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string LpmWakeupMethodToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Enable HOGP wake up");
				stringBuilder.AppendLine("Bit 1: Enable BR EDR wake up ");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 4u;

		private uint _LpmWakeupMethod;

		public uint SubOpcode => 4u;

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

		public LpmWakeupMethodEnumCollection GetLpmWakeupMethodAsLpmWakeupMethodEnumCollection()
		{
			return LpmWakeupMethodInfo.ValueToLpmWakeupMethod(_LpmWakeupMethod);
		}

		public void SetLpmWakeupMethodAsLpmWakeupMethodEnumCollection(LpmWakeupMethodEnumCollection enumCollection)
		{
			LpmWakeupMethod = LpmWakeupMethodInfo.LpmWakeupMethodToValue(enumCollection);
		}
	}

	private const uint _Opcode = 64566u;

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

	public uint Opcode => 64566u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.LpmWakeupMethod = 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.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.LpmWakeupMethod, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
