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

namespace QCRPG.TSL.BT.Packets;

public class DbfwNocRssiEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableModeEnum
		{
			Disabled,
			Enabled,
			RESERVED_FOR_FUTURE_USE,
			InvalidEnableModeValue
		}

		public static class EnableModeInfo
		{
			public static EnableModeEnum ValueToEnableMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return EnableModeEnum.Disabled;
				case 1u:
					return EnableModeEnum.Enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return EnableModeEnum.RESERVED_FOR_FUTURE_USE;
					}
					return EnableModeEnum.InvalidEnableModeValue;
				}
			}

			public static uint EnableModeToValue(EnableModeEnum enumItem)
			{
				return enumItem switch
				{
					EnableModeEnum.Disabled => 0u, 
					EnableModeEnum.Enabled => 1u, 
					EnableModeEnum.RESERVED_FOR_FUTURE_USE => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableModeToString(EnableModeEnum enumItem)
			{
				return enumItem switch
				{
					EnableModeEnum.Disabled => "Disabled", 
					EnableModeEnum.Enabled => "Enabled", 
					EnableModeEnum.RESERVED_FOR_FUTURE_USE => "RESERVED_FOR_FUTURE_USE", 
					_ => "Invalid EnableMode Value", 
				};
			}

			public static string EnableModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disabled");
				stringBuilder.AppendLine("0x1 = Enabled");
				stringBuilder.AppendLine("0x2-0xFF = RESERVED_FOR_FUTURE_USE");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 64u;

		private uint _EnableMode;

		public uint SubOpcode => 64u;

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

		public EnableModeEnum GetEnableModeAsEnableModeEnum()
		{
			return EnableModeInfo.ValueToEnableMode(_EnableMode);
		}

		public void SetEnableModeAsEnableModeEnum(EnableModeEnum enumItem)
		{
			EnableMode = EnableModeInfo.EnableModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 65009u;

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

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

	public uint Opcode => 65009u;

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