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

namespace QCRPG.TSL.BT.Packets;

public class WriteScoFlowControlEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SynchronousFlowControlEnableEnum
		{
			Synchronous_Flow_Control_is_disabled,
			Synchronous_Flow_Control_is_enabled,
			InvalidSynchronousFlowControlEnableValue
		}

		public static class SynchronousFlowControlEnableInfo
		{
			public static SynchronousFlowControlEnableEnum ValueToSynchronousFlowControlEnable(uint value)
			{
				return value switch
				{
					0u => SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_disabled, 
					1u => SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_enabled, 
					_ => SynchronousFlowControlEnableEnum.InvalidSynchronousFlowControlEnableValue, 
				};
			}

			public static uint SynchronousFlowControlEnableToValue(SynchronousFlowControlEnableEnum enumItem)
			{
				return enumItem switch
				{
					SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_disabled => 0u, 
					SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_enabled => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SynchronousFlowControlEnableToString(SynchronousFlowControlEnableEnum enumItem)
			{
				return enumItem switch
				{
					SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_disabled => "Synchronous_Flow_Control_is_disabled", 
					SynchronousFlowControlEnableEnum.Synchronous_Flow_Control_is_enabled => "Synchronous_Flow_Control_is_enabled", 
					_ => "Invalid SynchronousFlowControlEnable Value", 
				};
			}

			public static string SynchronousFlowControlEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Synchronous_Flow_Control_is_disabled");
				stringBuilder.AppendLine("0x1 = Synchronous_Flow_Control_is_enabled");
				return stringBuilder.ToString();
			}
		}

		private uint _SynchronousFlowControlEnable;

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

		public SynchronousFlowControlEnableEnum GetSynchronousFlowControlEnableAsSynchronousFlowControlEnableEnum()
		{
			return SynchronousFlowControlEnableInfo.ValueToSynchronousFlowControlEnable(_SynchronousFlowControlEnable);
		}

		public void SetSynchronousFlowControlEnableAsSynchronousFlowControlEnableEnum(SynchronousFlowControlEnableEnum enumItem)
		{
			SynchronousFlowControlEnable = SynchronousFlowControlEnableInfo.SynchronousFlowControlEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 3119u;

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

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

	public uint Opcode => 3119u;

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