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

namespace QCRPG.TSL.BT.Packets;

public class LeBatchScanReadBatchScanResults : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BatchScanDataReadEnum
		{
			Truncated_mode_data,
			Full_mode_data,
			InvalidBatchScanDataReadValue
		}

		public static class BatchScanDataReadInfo
		{
			public static BatchScanDataReadEnum ValueToBatchScanDataRead(uint value)
			{
				return value switch
				{
					1u => BatchScanDataReadEnum.Truncated_mode_data, 
					2u => BatchScanDataReadEnum.Full_mode_data, 
					_ => BatchScanDataReadEnum.InvalidBatchScanDataReadValue, 
				};
			}

			public static uint BatchScanDataReadToValue(BatchScanDataReadEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanDataReadEnum.Truncated_mode_data => 1u, 
					BatchScanDataReadEnum.Full_mode_data => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string BatchScanDataReadToString(BatchScanDataReadEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanDataReadEnum.Truncated_mode_data => "Truncated mode data", 
					BatchScanDataReadEnum.Full_mode_data => "Full mode data", 
					_ => "Invalid BatchScanDataRead Value", 
				};
			}

			public static string BatchScanDataReadToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = Truncated mode data");
				stringBuilder.AppendLine("0x2 = Full mode data");
				return stringBuilder.ToString();
			}
		}

		private const uint _BatchScanOpcode = 4u;

		private uint _BatchScanDataRead;

		public uint BatchScanOpcode => 4u;

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

		public BatchScanDataReadEnum GetBatchScanDataReadAsBatchScanDataReadEnum()
		{
			return BatchScanDataReadInfo.ValueToBatchScanDataRead(_BatchScanDataRead);
		}

		public void SetBatchScanDataReadAsBatchScanDataReadEnum(BatchScanDataReadEnum enumItem)
		{
			BatchScanDataRead = BatchScanDataReadInfo.BatchScanDataReadToValue(enumItem);
		}
	}

	private const uint _Opcode = 64854u;

	private Type[] expectedResponseTypes = new Type[3]
	{
		typeof(CommandCompleteForLeBatchScanReadBatchScanResultsTruncated),
		typeof(CommandCompleteForLeBatchScanReadBatchScanResultsFull),
		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 LeBatchScanReadBatchScanResultsInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64854u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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