using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class ReadClock : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum WhichClockEnum
		{
			Local_Clock,
			Piconet_Clock,
			Reserved,
			InvalidWhichClockValue
		}

		public static class WhichClockInfo
		{
			public static WhichClockEnum ValueToWhichClock(uint value)
			{
				switch (value)
				{
				case 0u:
					return WhichClockEnum.Local_Clock;
				case 1u:
					return WhichClockEnum.Piconet_Clock;
				default:
					if (value >= 2 && value <= 255)
					{
						return WhichClockEnum.Reserved;
					}
					return WhichClockEnum.InvalidWhichClockValue;
				}
			}

			public static uint WhichClockToValue(WhichClockEnum enumItem)
			{
				return enumItem switch
				{
					WhichClockEnum.Local_Clock => 0u, 
					WhichClockEnum.Piconet_Clock => 1u, 
					WhichClockEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string WhichClockToString(WhichClockEnum enumItem)
			{
				return enumItem switch
				{
					WhichClockEnum.Local_Clock => "Local_Clock", 
					WhichClockEnum.Piconet_Clock => "Piconet_Clock", 
					WhichClockEnum.Reserved => "Reserved", 
					_ => "Invalid WhichClock Value", 
				};
			}

			public static string WhichClockToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Local_Clock");
				stringBuilder.AppendLine("0x1 = Piconet_Clock");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _WhichClock;

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

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

		public WhichClockEnum GetWhichClockAsWhichClockEnum()
		{
			return WhichClockInfo.ValueToWhichClock(_WhichClock);
		}

		public void SetWhichClockAsWhichClockEnum(WhichClockEnum enumItem)
		{
			WhichClock = WhichClockInfo.WhichClockToValue(enumItem);
		}
	}

	private const uint _Opcode = 5127u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForReadClock) };

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

	public uint Opcode => 5127u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.WhichClock = 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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.WhichClock, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection Handle is 0xEFF");
		}
		return list;
	}
}
