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

namespace QCRPG.TSL.BT.Packets;

public class WriteLoopbackMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LoopbackModeEnum
		{
			No_Loopback_mode_enabled,
			Enable_Local_Loopback,
			Enable_Remote_Loopback,
			Reserved,
			InvalidLoopbackModeValue
		}

		public static class LoopbackModeInfo
		{
			public static LoopbackModeEnum ValueToLoopbackMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return LoopbackModeEnum.No_Loopback_mode_enabled;
				case 1u:
					return LoopbackModeEnum.Enable_Local_Loopback;
				case 2u:
					return LoopbackModeEnum.Enable_Remote_Loopback;
				default:
					if (value >= 3 && value <= 255)
					{
						return LoopbackModeEnum.Reserved;
					}
					return LoopbackModeEnum.InvalidLoopbackModeValue;
				}
			}

			public static uint LoopbackModeToValue(LoopbackModeEnum enumItem)
			{
				return enumItem switch
				{
					LoopbackModeEnum.No_Loopback_mode_enabled => 0u, 
					LoopbackModeEnum.Enable_Local_Loopback => 1u, 
					LoopbackModeEnum.Enable_Remote_Loopback => 2u, 
					LoopbackModeEnum.Reserved => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LoopbackModeToString(LoopbackModeEnum enumItem)
			{
				return enumItem switch
				{
					LoopbackModeEnum.No_Loopback_mode_enabled => "No_Loopback_mode_enabled", 
					LoopbackModeEnum.Enable_Local_Loopback => "Enable_Local_Loopback", 
					LoopbackModeEnum.Enable_Remote_Loopback => "Enable_Remote_Loopback", 
					LoopbackModeEnum.Reserved => "Reserved", 
					_ => "Invalid LoopbackMode Value", 
				};
			}

			public static string LoopbackModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No_Loopback_mode_enabled");
				stringBuilder.AppendLine("0x1 = Enable_Local_Loopback");
				stringBuilder.AppendLine("0x2 = Enable_Remote_Loopback");
				stringBuilder.AppendLine("0x3-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _LoopbackMode;

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

		public LoopbackModeEnum GetLoopbackModeAsLoopbackModeEnum()
		{
			return LoopbackModeInfo.ValueToLoopbackMode(_LoopbackMode);
		}

		public void SetLoopbackModeAsLoopbackModeEnum(LoopbackModeEnum enumItem)
		{
			LoopbackMode = LoopbackModeInfo.LoopbackModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 6146u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForWriteLoopbackMode) };

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

	public uint Opcode => 6146u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.LoopbackMode = 0u;
	}

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