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

namespace QCRPG.TSL.BT.Packets;

public class LeBatchScanEnableCustomerSpecificFeature : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableCustomerSpecificFeatureSetEnum
		{
			Disable_Batch_Scan_feature,
			Enable_Batch_Scan_feature,
			InvalidEnableCustomerSpecificFeatureSetValue
		}

		public static class EnableCustomerSpecificFeatureSetInfo
		{
			public static EnableCustomerSpecificFeatureSetEnum ValueToEnableCustomerSpecificFeatureSet(uint value)
			{
				return value switch
				{
					0u => EnableCustomerSpecificFeatureSetEnum.Disable_Batch_Scan_feature, 
					1u => EnableCustomerSpecificFeatureSetEnum.Enable_Batch_Scan_feature, 
					_ => EnableCustomerSpecificFeatureSetEnum.InvalidEnableCustomerSpecificFeatureSetValue, 
				};
			}

			public static uint EnableCustomerSpecificFeatureSetToValue(EnableCustomerSpecificFeatureSetEnum enumItem)
			{
				return enumItem switch
				{
					EnableCustomerSpecificFeatureSetEnum.Disable_Batch_Scan_feature => 0u, 
					EnableCustomerSpecificFeatureSetEnum.Enable_Batch_Scan_feature => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableCustomerSpecificFeatureSetToString(EnableCustomerSpecificFeatureSetEnum enumItem)
			{
				return enumItem switch
				{
					EnableCustomerSpecificFeatureSetEnum.Disable_Batch_Scan_feature => "Disable Batch Scan feature", 
					EnableCustomerSpecificFeatureSetEnum.Enable_Batch_Scan_feature => "Enable Batch Scan feature", 
					_ => "Invalid EnableCustomerSpecificFeatureSet Value", 
				};
			}

			public static string EnableCustomerSpecificFeatureSetToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable Batch Scan feature");
				stringBuilder.AppendLine("0x1 = Enable Batch Scan feature");
				return stringBuilder.ToString();
			}
		}

		private const uint _BatchScanOpcode = 1u;

		private uint _EnableCustomerSpecificFeatureSet;

		public uint BatchScanOpcode => 1u;

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

		public EnableCustomerSpecificFeatureSetEnum GetEnableCustomerSpecificFeatureSetAsEnableCustomerSpecificFeatureSetEnum()
		{
			return EnableCustomerSpecificFeatureSetInfo.ValueToEnableCustomerSpecificFeatureSet(_EnableCustomerSpecificFeatureSet);
		}

		public void SetEnableCustomerSpecificFeatureSetAsEnableCustomerSpecificFeatureSetEnum(EnableCustomerSpecificFeatureSetEnum enumItem)
		{
			EnableCustomerSpecificFeatureSet = EnableCustomerSpecificFeatureSetInfo.EnableCustomerSpecificFeatureSetToValue(enumItem);
		}
	}

	private const uint _Opcode = 64854u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeBatchScanEnableCustomerSpecificFeature),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64854u;

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