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

namespace QCRPG.TSL.BT.Packets;

public class SetMptaGrantModeEdgeDelay : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			SET_MPTA_GRANT_MODE_EDGE_DELAY,
			InvalidSubOpcodeValue
		}

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

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

			public static string SubOpcodeToString(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.SET_MPTA_GRANT_MODE_EDGE_DELAY)
				{
					return "Set MPTA Grant Mode Edge Delay";
				}
				return "Invalid SubOpcode Value";
			}

			public static string SubOpcodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x9 = Set MPTA Grant Mode Edge Delay");
				return stringBuilder.ToString();
			}
		}

		public enum OpTypeEnum
		{
			Configuring_PP0_Rx_Falling_Edge_Delay,
			Configuring_PP0_Rx_Rising_Edge_Delay,
			Configuring_PP0_Tx_Falling_Edge_Delay,
			Configuring_PP0_Tx_Rising_Edge_Delay,
			Configuring_PP1_Rx_Falling_Edge_Delay,
			Configuring_PP1_Rx_Rising_Edge_Delay,
			Configuring_PP1_Tx_Falling_Edge_Delay,
			Configuring_PP1_Tx_Rising_Edge_Delay,
			Configuring_WLAN_Rx_Falling_Edge_Delay,
			Configuring_WLAN_Rx_Rising_Edge_Delay,
			Configuring_WLAN_Tx_Falling_Edge_Delay,
			Configuring_WLAN_Tx_Rising_Edge_Delay,
			InvalidOpTypeValue
		}

		public static class OpTypeInfo
		{
			public static OpTypeEnum ValueToOpType(uint value)
			{
				return value switch
				{
					0u => OpTypeEnum.Configuring_PP0_Rx_Falling_Edge_Delay, 
					1u => OpTypeEnum.Configuring_PP0_Rx_Rising_Edge_Delay, 
					2u => OpTypeEnum.Configuring_PP0_Tx_Falling_Edge_Delay, 
					3u => OpTypeEnum.Configuring_PP0_Tx_Rising_Edge_Delay, 
					4u => OpTypeEnum.Configuring_PP1_Rx_Falling_Edge_Delay, 
					5u => OpTypeEnum.Configuring_PP1_Rx_Rising_Edge_Delay, 
					6u => OpTypeEnum.Configuring_PP1_Tx_Falling_Edge_Delay, 
					7u => OpTypeEnum.Configuring_PP1_Tx_Rising_Edge_Delay, 
					8u => OpTypeEnum.Configuring_WLAN_Rx_Falling_Edge_Delay, 
					9u => OpTypeEnum.Configuring_WLAN_Rx_Rising_Edge_Delay, 
					10u => OpTypeEnum.Configuring_WLAN_Tx_Falling_Edge_Delay, 
					11u => OpTypeEnum.Configuring_WLAN_Tx_Rising_Edge_Delay, 
					_ => OpTypeEnum.InvalidOpTypeValue, 
				};
			}

			public static uint OpTypeToValue(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_PP0_Rx_Falling_Edge_Delay => 0u, 
					OpTypeEnum.Configuring_PP0_Rx_Rising_Edge_Delay => 1u, 
					OpTypeEnum.Configuring_PP0_Tx_Falling_Edge_Delay => 2u, 
					OpTypeEnum.Configuring_PP0_Tx_Rising_Edge_Delay => 3u, 
					OpTypeEnum.Configuring_PP1_Rx_Falling_Edge_Delay => 4u, 
					OpTypeEnum.Configuring_PP1_Rx_Rising_Edge_Delay => 5u, 
					OpTypeEnum.Configuring_PP1_Tx_Falling_Edge_Delay => 6u, 
					OpTypeEnum.Configuring_PP1_Tx_Rising_Edge_Delay => 7u, 
					OpTypeEnum.Configuring_WLAN_Rx_Falling_Edge_Delay => 8u, 
					OpTypeEnum.Configuring_WLAN_Rx_Rising_Edge_Delay => 9u, 
					OpTypeEnum.Configuring_WLAN_Tx_Falling_Edge_Delay => 10u, 
					OpTypeEnum.Configuring_WLAN_Tx_Rising_Edge_Delay => 11u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OpTypeToString(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_PP0_Rx_Falling_Edge_Delay => "Configuring PP0 Rx Falling Edge Delay", 
					OpTypeEnum.Configuring_PP0_Rx_Rising_Edge_Delay => "Configuring PP0 Rx Rising Edge Delay", 
					OpTypeEnum.Configuring_PP0_Tx_Falling_Edge_Delay => "Configuring PP0 Tx Falling Edge Delay", 
					OpTypeEnum.Configuring_PP0_Tx_Rising_Edge_Delay => "Configuring PP0 Tx Rising Edge Delay", 
					OpTypeEnum.Configuring_PP1_Rx_Falling_Edge_Delay => "Configuring PP1 Rx Falling Edge Delay", 
					OpTypeEnum.Configuring_PP1_Rx_Rising_Edge_Delay => "Configuring PP1 Rx Rising Edge Delay", 
					OpTypeEnum.Configuring_PP1_Tx_Falling_Edge_Delay => "Configuring PP1 Tx Falling Edge Delay", 
					OpTypeEnum.Configuring_PP1_Tx_Rising_Edge_Delay => "Configuring PP1 Tx Rising Edge Delay", 
					OpTypeEnum.Configuring_WLAN_Rx_Falling_Edge_Delay => "Configuring WLAN Rx Falling Edge Delay", 
					OpTypeEnum.Configuring_WLAN_Rx_Rising_Edge_Delay => "Configuring WLAN Rx Rising Edge Delay", 
					OpTypeEnum.Configuring_WLAN_Tx_Falling_Edge_Delay => "Configuring WLAN Tx Falling Edge Delay", 
					OpTypeEnum.Configuring_WLAN_Tx_Rising_Edge_Delay => "Configuring WLAN Tx Rising Edge Delay", 
					_ => "Invalid OpType Value", 
				};
			}

			public static string OpTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Configuring PP0 Rx Falling Edge Delay");
				stringBuilder.AppendLine("0x1 = Configuring PP0 Rx Rising Edge Delay");
				stringBuilder.AppendLine("0x2 = Configuring PP0 Tx Falling Edge Delay");
				stringBuilder.AppendLine("0x3 = Configuring PP0 Tx Rising Edge Delay");
				stringBuilder.AppendLine("0x4 = Configuring PP1 Rx Falling Edge Delay");
				stringBuilder.AppendLine("0x5 = Configuring PP1 Rx Rising Edge Delay");
				stringBuilder.AppendLine("0x6 = Configuring PP1 Tx Falling Edge Delay");
				stringBuilder.AppendLine("0x7 = Configuring PP1 Tx Rising Edge Delay");
				stringBuilder.AppendLine("0x8 = Configuring WLAN Rx Falling Edge Delay");
				stringBuilder.AppendLine("0x9 = Configuring WLAN Rx Rising Edge Delay");
				stringBuilder.AppendLine("0xA = Configuring WLAN Tx Falling Edge Delay");
				stringBuilder.AppendLine("0xB = Configuring WLAN Tx Rising Edge Delay");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 9u;

		private uint _OpType;

		private uint _Delay;

		public uint SubOpcode => 9u;

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

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

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

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

		public void SetOpTypeAsOpTypeEnum(OpTypeEnum enumItem)
		{
			OpType = OpTypeInfo.OpTypeToValue(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 SetMptaGrantModeEdgeDelayInfo(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.Delay = 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.Delay, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
