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

namespace QCRPG.TSL.BT.Packets;

public class HostSleep : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SleepLevelEnum
		{
			S3_level_Sleep_mode,
			S4_level_Hibernate_mode,
			InvalidSleepLevelValue
		}

		public static class SleepLevelInfo
		{
			public static SleepLevelEnum ValueToSleepLevel(uint value)
			{
				return value switch
				{
					3u => SleepLevelEnum.S3_level_Sleep_mode, 
					4u => SleepLevelEnum.S4_level_Hibernate_mode, 
					_ => SleepLevelEnum.InvalidSleepLevelValue, 
				};
			}

			public static uint SleepLevelToValue(SleepLevelEnum enumItem)
			{
				return enumItem switch
				{
					SleepLevelEnum.S3_level_Sleep_mode => 3u, 
					SleepLevelEnum.S4_level_Hibernate_mode => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SleepLevelToString(SleepLevelEnum enumItem)
			{
				return enumItem switch
				{
					SleepLevelEnum.S3_level_Sleep_mode => "S3 level (Sleep mode).", 
					SleepLevelEnum.S4_level_Hibernate_mode => "S4 level (Hibernate mode).", 
					_ => "Invalid SleepLevel Value", 
				};
			}

			public static string SleepLevelToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x3 = S3 level (Sleep mode).");
				stringBuilder.AppendLine("0x4 = S4 level (Hibernate mode).");
				return stringBuilder.ToString();
			}
		}

		private uint _SleepLevel;

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

		public SleepLevelEnum GetSleepLevelAsSleepLevelEnum()
		{
			return SleepLevelInfo.ValueToSleepLevel(_SleepLevel);
		}

		public void SetSleepLevelAsSleepLevelEnum(SleepLevelEnum enumItem)
		{
			SleepLevel = SleepLevelInfo.SleepLevelToValue(enumItem);
		}
	}

	private const uint _Opcode = 64596u;

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

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

	public uint Opcode => 64596u;

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