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

namespace QCRPG.TSL.BT.Packets;

public class SetMptaPriorityWeight : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			HCI_VS_SET_MPTA_PRIORITY_WEIGHT,
			InvalidSubOpcodeValue
		}

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

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

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

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

		public enum OpTypeEnum
		{
			Configuring_Low_priority_TX_weight,
			Configuring_High_priority_TX_weight,
			Configuring_Low_priority_RX_weight,
			Configuring_High_priority_RX_weight,
			InvalidOpTypeValue
		}

		public static class OpTypeInfo
		{
			public static OpTypeEnum ValueToOpType(uint value)
			{
				return value switch
				{
					0u => OpTypeEnum.Configuring_Low_priority_TX_weight, 
					1u => OpTypeEnum.Configuring_High_priority_TX_weight, 
					2u => OpTypeEnum.Configuring_Low_priority_RX_weight, 
					3u => OpTypeEnum.Configuring_High_priority_RX_weight, 
					_ => OpTypeEnum.InvalidOpTypeValue, 
				};
			}

			public static uint OpTypeToValue(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Low_priority_TX_weight => 0u, 
					OpTypeEnum.Configuring_High_priority_TX_weight => 1u, 
					OpTypeEnum.Configuring_Low_priority_RX_weight => 2u, 
					OpTypeEnum.Configuring_High_priority_RX_weight => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OpTypeToString(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Low_priority_TX_weight => "Configuring Low priority TX weight", 
					OpTypeEnum.Configuring_High_priority_TX_weight => "Configuring High priority TX weight", 
					OpTypeEnum.Configuring_Low_priority_RX_weight => "Configuring Low priority RX weight", 
					OpTypeEnum.Configuring_High_priority_RX_weight => "Configuring High priority RX weight", 
					_ => "Invalid OpType Value", 
				};
			}

			public static string OpTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Configuring Low priority TX weight");
				stringBuilder.AppendLine("0x1 = Configuring High priority TX weight");
				stringBuilder.AppendLine("0x2 = Configuring Low priority RX weight");
				stringBuilder.AppendLine("0x3 = Configuring High priority RX weight");
				return stringBuilder.ToString();
			}
		}

		public enum LevelEnum
		{
			Weight_0x25_Higher_than_BT_Scan_lower_than_A2dp,
			Weight_0x5B_Higher_than_A2dp_but_lower_than_SCO,
			Weight_0x67_Higher_than_Hid_but_lower_than_SCO,
			Weight_0x79_Higher_than_SCO_but_lower_than_Broadcast_Audio,
			InvalidLevelValue
		}

		public static class LevelInfo
		{
			public static LevelEnum ValueToLevel(uint value)
			{
				return value switch
				{
					0u => LevelEnum.Weight_0x25_Higher_than_BT_Scan_lower_than_A2dp, 
					1u => LevelEnum.Weight_0x5B_Higher_than_A2dp_but_lower_than_SCO, 
					2u => LevelEnum.Weight_0x67_Higher_than_Hid_but_lower_than_SCO, 
					3u => LevelEnum.Weight_0x79_Higher_than_SCO_but_lower_than_Broadcast_Audio, 
					_ => LevelEnum.InvalidLevelValue, 
				};
			}

			public static uint LevelToValue(LevelEnum enumItem)
			{
				return enumItem switch
				{
					LevelEnum.Weight_0x25_Higher_than_BT_Scan_lower_than_A2dp => 0u, 
					LevelEnum.Weight_0x5B_Higher_than_A2dp_but_lower_than_SCO => 1u, 
					LevelEnum.Weight_0x67_Higher_than_Hid_but_lower_than_SCO => 2u, 
					LevelEnum.Weight_0x79_Higher_than_SCO_but_lower_than_Broadcast_Audio => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LevelToString(LevelEnum enumItem)
			{
				return enumItem switch
				{
					LevelEnum.Weight_0x25_Higher_than_BT_Scan_lower_than_A2dp => "Weight (0x25) Higher than BT Scan lower than A2dp", 
					LevelEnum.Weight_0x5B_Higher_than_A2dp_but_lower_than_SCO => "Weight (0x5B) Higher than A2dp but lower than SCO", 
					LevelEnum.Weight_0x67_Higher_than_Hid_but_lower_than_SCO => "Weight (0x67) Higher than Hid but lower than SCO", 
					LevelEnum.Weight_0x79_Higher_than_SCO_but_lower_than_Broadcast_Audio => "Weight (0x79) Higher than SCO but lower than Broadcast Audio", 
					_ => "Invalid Level Value", 
				};
			}

			public static string LevelToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Weight (0x25) Higher than BT Scan lower than A2dp");
				stringBuilder.AppendLine("0x1 = Weight (0x5B) Higher than A2dp but lower than SCO");
				stringBuilder.AppendLine("0x2 = Weight (0x67) Higher than Hid but lower than SCO");
				stringBuilder.AppendLine("0x3 = Weight (0x79) Higher than SCO but lower than Broadcast Audio");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 0u;

		private uint _OpType;

		private uint _Level;

		public uint SubOpcode => 0u;

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

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

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

		public OpTypeEnum GetOpTypeAsOpTypeEnum()
		{
			return OpTypeInfo.ValueToOpType(_OpType);
		}

		public void SetOpTypeAsOpTypeEnum(OpTypeEnum enumItem)
		{
			OpType = OpTypeInfo.OpTypeToValue(enumItem);
		}

		public LevelEnum GetLevelAsLevelEnum()
		{
			return LevelInfo.ValueToLevel(_Level);
		}

		public void SetLevelAsLevelEnum(LevelEnum enumItem)
		{
			Level = LevelInfo.LevelToValue(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 SetMptaPriorityWeightInfo(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.OpType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Level = 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.OpType, 8);
		bitBuilder.AddBitsAsUint(UserFields.Level, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
