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

namespace QCRPG.TSL.BT.Packets;

public class SetMptaGrantModePriority : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			SET_MPTA_GRANT_MODE_PRIORITY,
			InvalidSubOpcodeValue
		}

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

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

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

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

		public enum OpTypeEnum
		{
			Configuring_Low_Base_Priority,
			Configuring_High_Base_Priority,
			Configuring_Low_Offset_Priority,
			Configuring_High_Offset_Priority,
			InvalidOpTypeValue
		}

		public static class OpTypeInfo
		{
			public static OpTypeEnum ValueToOpType(uint value)
			{
				return value switch
				{
					0u => OpTypeEnum.Configuring_Low_Base_Priority, 
					1u => OpTypeEnum.Configuring_High_Base_Priority, 
					2u => OpTypeEnum.Configuring_Low_Offset_Priority, 
					3u => OpTypeEnum.Configuring_High_Offset_Priority, 
					_ => OpTypeEnum.InvalidOpTypeValue, 
				};
			}

			public static uint OpTypeToValue(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Low_Base_Priority => 0u, 
					OpTypeEnum.Configuring_High_Base_Priority => 1u, 
					OpTypeEnum.Configuring_Low_Offset_Priority => 2u, 
					OpTypeEnum.Configuring_High_Offset_Priority => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OpTypeToString(OpTypeEnum enumItem)
			{
				return enumItem switch
				{
					OpTypeEnum.Configuring_Low_Base_Priority => "Configuring Low Base Priority", 
					OpTypeEnum.Configuring_High_Base_Priority => "Configuring High Base Priority", 
					OpTypeEnum.Configuring_Low_Offset_Priority => "Configuring Low Offset Priority", 
					OpTypeEnum.Configuring_High_Offset_Priority => "Configuring High Offset Priority", 
					_ => "Invalid OpType Value", 
				};
			}

			public static string OpTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Configuring Low Base Priority");
				stringBuilder.AppendLine("0x1 = Configuring High Base Priority");
				stringBuilder.AppendLine("0x2 = Configuring Low Offset Priority");
				stringBuilder.AppendLine("0x3 = Configuring High Offset Priority");
				return stringBuilder.ToString();
			}
		}

		public enum DirectionEnum
		{
			Rx,
			Tx,
			InvalidDirectionValue
		}

		public static class DirectionInfo
		{
			public static DirectionEnum ValueToDirection(uint value)
			{
				return value switch
				{
					0u => DirectionEnum.Rx, 
					1u => DirectionEnum.Tx, 
					_ => DirectionEnum.InvalidDirectionValue, 
				};
			}

			public static uint DirectionToValue(DirectionEnum enumItem)
			{
				return enumItem switch
				{
					DirectionEnum.Rx => 0u, 
					DirectionEnum.Tx => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string DirectionToString(DirectionEnum enumItem)
			{
				return enumItem switch
				{
					DirectionEnum.Rx => "Rx", 
					DirectionEnum.Tx => "Tx", 
					_ => "Invalid Direction Value", 
				};
			}

			public static string DirectionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Rx");
				stringBuilder.AppendLine("0x1 = Tx");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 12u;

		private uint _OpType;

		private uint _Priority;

		private uint _Direction;

		public uint SubOpcode => 12u;

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

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

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

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

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

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

		public DirectionEnum GetDirectionAsDirectionEnum()
		{
			return DirectionInfo.ValueToDirection(_Direction);
		}

		public void SetDirectionAsDirectionEnum(DirectionEnum enumItem)
		{
			Direction = DirectionInfo.DirectionToValue(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 SetMptaGrantModePriorityInfo(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.Priority = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Direction = 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.Priority, 8);
		bitBuilder.AddBitsAsUint(UserFields.Direction, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
