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

namespace QCRPG.TSL.BT.Packets;

public class LeSetScanEnabled : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FilterDuplicatesEnum
		{
			Duplicate_filtering_disabled,
			Duplicate_filtering_enabled,
			Reserved_for_future_use,
			InvalidFilterDuplicatesValue
		}

		public static class FilterDuplicatesInfo
		{
			public static FilterDuplicatesEnum ValueToFilterDuplicates(uint value)
			{
				switch (value)
				{
				case 0u:
					return FilterDuplicatesEnum.Duplicate_filtering_disabled;
				case 1u:
					return FilterDuplicatesEnum.Duplicate_filtering_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return FilterDuplicatesEnum.Reserved_for_future_use;
					}
					return FilterDuplicatesEnum.InvalidFilterDuplicatesValue;
				}
			}

			public static uint FilterDuplicatesToValue(FilterDuplicatesEnum enumItem)
			{
				return enumItem switch
				{
					FilterDuplicatesEnum.Duplicate_filtering_disabled => 0u, 
					FilterDuplicatesEnum.Duplicate_filtering_enabled => 1u, 
					FilterDuplicatesEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FilterDuplicatesToString(FilterDuplicatesEnum enumItem)
			{
				return enumItem switch
				{
					FilterDuplicatesEnum.Duplicate_filtering_disabled => "Duplicate filtering disabled", 
					FilterDuplicatesEnum.Duplicate_filtering_enabled => "Duplicate filtering enabled", 
					FilterDuplicatesEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid FilterDuplicates Value", 
				};
			}

			public static string FilterDuplicatesToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Duplicate filtering disabled");
				stringBuilder.AppendLine("0x1 = Duplicate filtering enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _LeScanEnable;

		private uint _FilterDuplicates;

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

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

		public LeScanEnableEnum GetLeScanEnableAsLeScanEnableEnum()
		{
			return LeScanEnableInfo.ValueToLeScanEnable(_LeScanEnable);
		}

		public void SetLeScanEnableAsLeScanEnableEnum(LeScanEnableEnum enumItem)
		{
			LeScanEnable = LeScanEnableInfo.LeScanEnableToValue(enumItem);
		}

		public FilterDuplicatesEnum GetFilterDuplicatesAsFilterDuplicatesEnum()
		{
			return FilterDuplicatesInfo.ValueToFilterDuplicates(_FilterDuplicates);
		}

		public void SetFilterDuplicatesAsFilterDuplicatesEnum(FilterDuplicatesEnum enumItem)
		{
			FilterDuplicates = FilterDuplicatesInfo.FilterDuplicatesToValue(enumItem);
		}
	}

	private const uint _Opcode = 8204u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeSetScanEnabled),
		typeof(LeAdvertisingReport)
	};

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

	public uint Opcode => 8204u;

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