using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class SetMptaGrantModeTimeout : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			SET_MPTA_GRANT_MODE_TIMEOUT,
			InvalidSubOpcodeValue
		}

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

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

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

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

		public enum OpTypeEnum
		{
			Configuring_Maximum_Request_Time_in_32usec_resolution,
			Configuring_No_Nack_Detection_Time_in_16MHz_clock_period_resolution,
			Configuring_WLAN_Contention_Reset_Timeout_in_32usec_resolution,
			InvalidOpTypeValue
		}

		public static class OpTypeInfo
		{
			public static OpTypeEnum ValueToOpType(uint value)
			{
				return value switch
				{
					0u => OpTypeEnum.Configuring_Maximum_Request_Time_in_32usec_resolution, 
					1u => OpTypeEnum.Configuring_No_Nack_Detection_Time_in_16MHz_clock_period_resolution, 
					2u => OpTypeEnum.Configuring_WLAN_Contention_Reset_Timeout_in_32usec_resolution, 
					_ => OpTypeEnum.InvalidOpTypeValue, 
				};
			}

			public static uint OpTypeToValue(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Maximum_Request_Time_in_32usec_resolution => 0u, 
					OpTypeEnum.Configuring_No_Nack_Detection_Time_in_16MHz_clock_period_resolution => 1u, 
					OpTypeEnum.Configuring_WLAN_Contention_Reset_Timeout_in_32usec_resolution => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OpTypeToString(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Maximum_Request_Time_in_32usec_resolution => "Configuring Maximum Request Time in 32usec resolution", 
					OpTypeEnum.Configuring_No_Nack_Detection_Time_in_16MHz_clock_period_resolution => "Configuring No Nack Detection Time in 16MHz clock period resolution", 
					OpTypeEnum.Configuring_WLAN_Contention_Reset_Timeout_in_32usec_resolution => "Configuring WLAN Contention Reset Timeout in 32usec resolution", 
					_ => "Invalid OpType Value", 
				};
			}

			public static string OpTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Configuring Maximum Request Time in 32usec resolution");
				stringBuilder.AppendLine("0x1 = Configuring No Nack Detection Time in 16MHz clock period resolution");
				stringBuilder.AppendLine("0x2 = Configuring WLAN Contention Reset Timeout in 32usec resolution");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 11u;

		private uint _OpType;

		private uint _Time = 1u;

		public uint SubOpcode => 11u;

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

		public uint Time
		{
			get
			{
				return _Time;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Time is 0xFFFF");
				}
				_Time = value;
			}
		}

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

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

	public uint Opcode => 64608u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Time = 1u;
	}

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

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Time < 1)
		{
			list.Add("Minimum allowed value for Time is 0x1");
		}
		if (UserFields.Time > 640)
		{
			list.Add("Maximum allowed value for Time is 0x280");
		}
		return list;
	}
}
