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

namespace QCRPG.TSL.ZigBee.Packets;

public class PlatformWakeEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum WakereasonEnum
		{
			Time_Reached,
			Data_Pending,
			InvalidWakereasonValue
		}

		public static class WakereasonInfo
		{
			public static WakereasonEnum ValueToWakereason(uint value)
			{
				return value switch
				{
					0u => WakereasonEnum.Time_Reached, 
					1u => WakereasonEnum.Data_Pending, 
					_ => WakereasonEnum.InvalidWakereasonValue, 
				};
			}

			public static uint WakereasonToValue(WakereasonEnum enumItem)
			{
				return enumItem switch
				{
					WakereasonEnum.Time_Reached => 0u, 
					WakereasonEnum.Data_Pending => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string WakereasonToString(WakereasonEnum enumItem)
			{
				return enumItem switch
				{
					WakereasonEnum.Time_Reached => "Time reached", 
					WakereasonEnum.Data_Pending => "Data pending", 
					_ => "Invalid Wakereason Value", 
				};
			}

			public static string WakereasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Time reached");
				stringBuilder.AppendLine("0x1 = Data pending");
				return stringBuilder.ToString();
			}
		}

		private const uint _Cmdid = 1u;

		private uint _Packetlength;

		private uint _Wakereason;

		public uint Packetlength
		{
			get
			{
				return _Packetlength;
			}
			set
			{
				_Packetlength = value;
			}
		}

		public uint Cmdid => 1u;

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

		public WakereasonEnum GetWakereasonAsWakereasonEnum()
		{
			return WakereasonInfo.ValueToWakereason(_Wakereason);
		}

		public void SetWakereasonAsWakereasonEnum(WakereasonEnum enumItem)
		{
			Wakereason = WakereasonInfo.WakereasonToValue(enumItem);
		}
	}

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

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Tabi20Event;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.Packetlength = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Cmdid)
		{
			return false;
		}
		UserFields.Wakereason = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Packetlength, 32);
		bitBuilder.AddBitsAsUint(UserFields.Cmdid, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Wakereason, 8);
		return bitBuilder.ToString();
	}
}
