using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeSetScanParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LeScanTypeEnum
		{
			Passive_Scanning_No_scanning_PDUs_shall_be_sent,
			Active_scanning_Scanning_PDUs_may_be_sent,
			Reserved_for_future_use,
			InvalidLeScanTypeValue
		}

		public static class LeScanTypeInfo
		{
			public static LeScanTypeEnum ValueToLeScanType(uint value)
			{
				switch (value)
				{
				case 0u:
					return LeScanTypeEnum.Passive_Scanning_No_scanning_PDUs_shall_be_sent;
				case 1u:
					return LeScanTypeEnum.Active_scanning_Scanning_PDUs_may_be_sent;
				default:
					if (value >= 2 && value <= 255)
					{
						return LeScanTypeEnum.Reserved_for_future_use;
					}
					return LeScanTypeEnum.InvalidLeScanTypeValue;
				}
			}

			public static uint LeScanTypeToValue(LeScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					LeScanTypeEnum.Passive_Scanning_No_scanning_PDUs_shall_be_sent => 0u, 
					LeScanTypeEnum.Active_scanning_Scanning_PDUs_may_be_sent => 1u, 
					LeScanTypeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LeScanTypeToString(LeScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					LeScanTypeEnum.Passive_Scanning_No_scanning_PDUs_shall_be_sent => "Passive Scanning.", 
					LeScanTypeEnum.Active_scanning_Scanning_PDUs_may_be_sent => "Active scanning.", 
					LeScanTypeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid LeScanType Value", 
				};
			}

			public static string LeScanTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Passive Scanning.");
				stringBuilder.AppendLine("0x1 = Active scanning.");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _LeScanType;

		private uint _LeScanInterval = 4u;

		private uint _LeScanWindow = 4u;

		private uint _OwnAddressType;

		private uint _ScanningFilterPolicy;

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

		public uint LeScanInterval
		{
			get
			{
				return _LeScanInterval;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for LeScanInterval is 0xFFFF");
				}
				_LeScanInterval = value;
			}
		}

		public uint LeScanWindow
		{
			get
			{
				return _LeScanWindow;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for LeScanWindow is 0xFFFF");
				}
				_LeScanWindow = value;
			}
		}

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

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

		public LeScanTypeEnum GetLeScanTypeAsLeScanTypeEnum()
		{
			return LeScanTypeInfo.ValueToLeScanType(_LeScanType);
		}

		public void SetLeScanTypeAsLeScanTypeEnum(LeScanTypeEnum enumItem)
		{
			LeScanType = LeScanTypeInfo.LeScanTypeToValue(enumItem);
		}

		public OwnAddressTypeEnum GetOwnAddressTypeAsOwnAddressTypeEnum()
		{
			return OwnAddressTypeInfo.ValueToOwnAddressType(_OwnAddressType);
		}

		public void SetOwnAddressTypeAsOwnAddressTypeEnum(OwnAddressTypeEnum enumItem)
		{
			OwnAddressType = OwnAddressTypeInfo.OwnAddressTypeToValue(enumItem);
		}

		public ScanningFilterPolicyEnum GetScanningFilterPolicyAsScanningFilterPolicyEnum()
		{
			return ScanningFilterPolicyInfo.ValueToScanningFilterPolicy(_ScanningFilterPolicy);
		}

		public void SetScanningFilterPolicyAsScanningFilterPolicyEnum(ScanningFilterPolicyEnum enumItem)
		{
			ScanningFilterPolicy = ScanningFilterPolicyInfo.ScanningFilterPolicyToValue(enumItem);
		}
	}

	private const uint _Opcode = 8203u;

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

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

	public uint Opcode => 8203u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.LeScanInterval = 16u;
		UserFields.LeScanWindow = 16u;
	}

	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.LeScanType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LeScanInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.LeScanWindow = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.OwnAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ScanningFilterPolicy = 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.LeScanType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.LeScanInterval, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.LeScanWindow, 16);
		bitBuilder.AddBitsAsUint(UserFields.OwnAddressType, 8);
		bitBuilder.AddBitsAsUint(UserFields.ScanningFilterPolicy, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.LeScanInterval < 4)
		{
			list.Add("Minimum allowed value for LE_Scan_Interval is 0x4");
		}
		if (UserFields.LeScanInterval > 16384)
		{
			list.Add("Maximum allowed value for LE_Scan_Interval is 0x4000");
		}
		if (UserFields.LeScanWindow < 4)
		{
			list.Add("Minimum allowed value for LE_Scan_Window is 0x4");
		}
		if (UserFields.LeScanWindow > 16384)
		{
			list.Add("Maximum allowed value for LE_Scan_Window is 0x4000");
		}
		return list;
	}
}
