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

namespace QCRPG.TSL.BT.Packets;

public class LtPastExtremTimingRound : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum RoundEnum
		{
			Disabled,
			ROUND1,
			ROUND2,
			ROUND3,
			ROUND4,
			ROUND5,
			ROUND6,
			InvalidRoundValue
		}

		public static class RoundInfo
		{
			public static RoundEnum ValueToRound(uint value)
			{
				return value switch
				{
					0u => RoundEnum.Disabled, 
					1u => RoundEnum.ROUND1, 
					2u => RoundEnum.ROUND2, 
					3u => RoundEnum.ROUND3, 
					4u => RoundEnum.ROUND4, 
					5u => RoundEnum.ROUND5, 
					6u => RoundEnum.ROUND6, 
					_ => RoundEnum.InvalidRoundValue, 
				};
			}

			public static uint RoundToValue(RoundEnum enumItem)
			{
				return enumItem switch
				{
					RoundEnum.Disabled => 0u, 
					RoundEnum.ROUND1 => 1u, 
					RoundEnum.ROUND2 => 2u, 
					RoundEnum.ROUND3 => 3u, 
					RoundEnum.ROUND4 => 4u, 
					RoundEnum.ROUND5 => 5u, 
					RoundEnum.ROUND6 => 6u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RoundToString(RoundEnum enumItem)
			{
				return enumItem switch
				{
					RoundEnum.Disabled => "Disable PAST Extreme timing test", 
					RoundEnum.ROUND1 => "Minimum distance in the future when PA interval greater than MAX sync offset + 2xconnection interval", 
					RoundEnum.ROUND2 => "Maximum distance in the future when PA interval greater than MAX sync offset + 2xconnection interval", 
					RoundEnum.ROUND3 => "Minimum distance in the past when PA interval greater than MAX sync offset + 2xconnection interval", 
					RoundEnum.ROUND4 => "Maximum distance in the past when PA interval greater than MAX sync offset + 2xconnection interval", 
					RoundEnum.ROUND5 => "Maximum distance in the past when PA interval less than half the connection interval", 
					RoundEnum.ROUND6 => "Minimum distance in the future when PA interval less than half the connection interval", 
					_ => "Invalid Round Value", 
				};
			}

			public static string RoundToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable PAST Extreme timing test");
				stringBuilder.AppendLine("0x1 = Minimum distance in the future when PA interval greater than MAX sync offset + 2xconnection interval");
				stringBuilder.AppendLine("0x2 = Maximum distance in the future when PA interval greater than MAX sync offset + 2xconnection interval");
				stringBuilder.AppendLine("0x3 = Minimum distance in the past when PA interval greater than MAX sync offset + 2xconnection interval");
				stringBuilder.AppendLine("0x4 = Maximum distance in the past when PA interval greater than MAX sync offset + 2xconnection interval");
				stringBuilder.AppendLine("0x5 = Maximum distance in the past when PA interval less than half the connection interval");
				stringBuilder.AppendLine("0x6 = Minimum distance in the future when PA interval less than half the connection interval");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 18u;

		private uint _Round;

		public uint SubOpcode => 18u;

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

		public RoundEnum GetRoundAsRoundEnum()
		{
			return RoundInfo.ValueToRound(_Round);
		}

		public void SetRoundAsRoundEnum(RoundEnum enumItem)
		{
			Round = RoundInfo.RoundToValue(enumItem);
		}
	}

	private const uint _Opcode = 64536u;

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

	public uint Opcode => 64536u;

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