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

namespace QCRPG.TSL.BT.Packets;

public class WriteScanEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ScanEnableEnum
		{
			NO_SCANS_ENABLED,
			INQUIRY_SCAN_ENABLED,
			PAGE_SCAN_ENABLED,
			BOTH_SCANS_ENABLED,
			RESERVED,
			InvalidScanEnableValue
		}

		public static class ScanEnableInfo
		{
			public static ScanEnableEnum ValueToScanEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return ScanEnableEnum.NO_SCANS_ENABLED;
				case 1u:
					return ScanEnableEnum.INQUIRY_SCAN_ENABLED;
				case 2u:
					return ScanEnableEnum.PAGE_SCAN_ENABLED;
				case 3u:
					return ScanEnableEnum.BOTH_SCANS_ENABLED;
				default:
					if (value >= 4 && value <= 255)
					{
						return ScanEnableEnum.RESERVED;
					}
					return ScanEnableEnum.InvalidScanEnableValue;
				}
			}

			public static uint ScanEnableToValue(ScanEnableEnum enumItem)
			{
				return enumItem switch
				{
					ScanEnableEnum.NO_SCANS_ENABLED => 0u, 
					ScanEnableEnum.INQUIRY_SCAN_ENABLED => 1u, 
					ScanEnableEnum.PAGE_SCAN_ENABLED => 2u, 
					ScanEnableEnum.BOTH_SCANS_ENABLED => 3u, 
					ScanEnableEnum.RESERVED => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ScanEnableToString(ScanEnableEnum enumItem)
			{
				return enumItem switch
				{
					ScanEnableEnum.NO_SCANS_ENABLED => "NO_SCANS_ENABLED", 
					ScanEnableEnum.INQUIRY_SCAN_ENABLED => "INQUIRY_SCAN_ENABLED", 
					ScanEnableEnum.PAGE_SCAN_ENABLED => "PAGE_SCAN_ENABLED", 
					ScanEnableEnum.BOTH_SCANS_ENABLED => "BOTH_SCANS_ENABLED", 
					ScanEnableEnum.RESERVED => "RESERVED", 
					_ => "Invalid ScanEnable Value", 
				};
			}

			public static string ScanEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = NO_SCANS_ENABLED");
				stringBuilder.AppendLine("0x1 = INQUIRY_SCAN_ENABLED");
				stringBuilder.AppendLine("0x2 = PAGE_SCAN_ENABLED");
				stringBuilder.AppendLine("0x3 = BOTH_SCANS_ENABLED");
				stringBuilder.AppendLine("0x4-0xFF = RESERVED");
				return stringBuilder.ToString();
			}
		}

		private uint _ScanEnable;

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

		public ScanEnableEnum GetScanEnableAsScanEnableEnum()
		{
			return ScanEnableInfo.ValueToScanEnable(_ScanEnable);
		}

		public void SetScanEnableAsScanEnableEnum(ScanEnableEnum enumItem)
		{
			ScanEnable = ScanEnableInfo.ScanEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 3098u;

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

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

	public uint Opcode => 3098u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ScanEnable = 3u;
	}

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