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

namespace QCRPG.TSL.BT.Packets;

public class SetMptaDeviceStatus : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			HCI_VSC_SET_MPTA_DEVICE_STATUS,
			InvalidSubOpcodeValue
		}

		public static class SubOpcodeInfo
		{
			public static SubOpcodeEnum ValueToSubOpcode(uint value)
			{
				if (value == 3)
				{
					return SubOpcodeEnum.HCI_VSC_SET_MPTA_DEVICE_STATUS;
				}
				return SubOpcodeEnum.InvalidSubOpcodeValue;
			}

			public static uint SubOpcodeToValue(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.HCI_VSC_SET_MPTA_DEVICE_STATUS)
				{
					return 3u;
				}
				return uint.MaxValue;
			}

			public static string SubOpcodeToString(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.HCI_VSC_SET_MPTA_DEVICE_STATUS)
				{
					return "HCI_VSC_SET_MPTA_DEVICE_STATUS";
				}
				return "Invalid SubOpcode Value";
			}

			public static string SubOpcodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x3 = HCI_VSC_SET_MPTA_DEVICE_STATUS");
				return stringBuilder.ToString();
			}
		}

		public enum StatusEnum
		{
			mPTA_off,
			mPTA_on,
			InvalidStatusValue
		}

		public static class StatusInfo
		{
			public static StatusEnum ValueToStatus(uint value)
			{
				return value switch
				{
					0u => StatusEnum.mPTA_off, 
					1u => StatusEnum.mPTA_on, 
					_ => StatusEnum.InvalidStatusValue, 
				};
			}

			public static uint StatusToValue(StatusEnum enumItem)
			{
				return enumItem switch
				{
					StatusEnum.mPTA_off => 0u, 
					StatusEnum.mPTA_on => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string StatusToString(StatusEnum enumItem)
			{
				return enumItem switch
				{
					StatusEnum.mPTA_off => "mPTA off", 
					StatusEnum.mPTA_on => "mPTA on", 
					_ => "Invalid Status Value", 
				};
			}

			public static string StatusToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = mPTA off");
				stringBuilder.AppendLine("0x1 = mPTA on");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 3u;

		private uint _Status;

		public uint SubOpcode => 3u;

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

		public SubOpcodeEnum GetSubOpcodeAsSubOpcodeEnum()
		{
			return SubOpcodeInfo.ValueToSubOpcode(3u);
		}

		public StatusEnum GetStatusAsStatusEnum()
		{
			return StatusInfo.ValueToStatus(_Status);
		}

		public void SetStatusAsStatusEnum(StatusEnum enumItem)
		{
			Status = StatusInfo.StatusToValue(enumItem);
		}
	}

	private const uint _Opcode = 64608u;

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

	public uint Opcode => 64608u;

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