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

namespace QCRPG.TSL.BT.Packets;

public class LeExtendedSetScanParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LeExScanTypeEnum
		{
			Passive_Scanning_No_SCAN_REQ_packets_shall_be_sent,
			Active_scanning_SCAN_REQ_packets_may_be_sent,
			InvalidLeExScanTypeValue
		}

		public static class LeExScanTypeInfo
		{
			public static LeExScanTypeEnum ValueToLeExScanType(uint value)
			{
				return value switch
				{
					0u => LeExScanTypeEnum.Passive_Scanning_No_SCAN_REQ_packets_shall_be_sent, 
					1u => LeExScanTypeEnum.Active_scanning_SCAN_REQ_packets_may_be_sent, 
					_ => LeExScanTypeEnum.InvalidLeExScanTypeValue, 
				};
			}

			public static uint LeExScanTypeToValue(LeExScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					LeExScanTypeEnum.Passive_Scanning_No_SCAN_REQ_packets_shall_be_sent => 0u, 
					LeExScanTypeEnum.Active_scanning_SCAN_REQ_packets_may_be_sent => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LeExScanTypeToString(LeExScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					LeExScanTypeEnum.Passive_Scanning_No_SCAN_REQ_packets_shall_be_sent => "Passive Scanning. No SCAN_REQ packets shall be sent. (default)", 
					LeExScanTypeEnum.Active_scanning_SCAN_REQ_packets_may_be_sent => "Active scanning. SCAN_REQ packets may be sent.", 
					_ => "Invalid LeExScanType Value", 
				};
			}

			public static string LeExScanTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Passive Scanning. No SCAN_REQ packets shall be sent. (default)");
				stringBuilder.AppendLine("0x1 = Active scanning. SCAN_REQ packets may be sent.");
				return stringBuilder.ToString();
			}
		}

		public enum LeExScanFilterPolicyEnum
		{
			Accept_all_except_directed,
			Accept_only,
			InvalidLeExScanFilterPolicyValue
		}

		public static class LeExScanFilterPolicyInfo
		{
			public static LeExScanFilterPolicyEnum ValueToLeExScanFilterPolicy(uint value)
			{
				return value switch
				{
					0u => LeExScanFilterPolicyEnum.Accept_all_except_directed, 
					1u => LeExScanFilterPolicyEnum.Accept_only, 
					_ => LeExScanFilterPolicyEnum.InvalidLeExScanFilterPolicyValue, 
				};
			}

			public static uint LeExScanFilterPolicyToValue(LeExScanFilterPolicyEnum enumItem)
			{
				return enumItem switch
				{
					LeExScanFilterPolicyEnum.Accept_all_except_directed => 0u, 
					LeExScanFilterPolicyEnum.Accept_only => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LeExScanFilterPolicyToString(LeExScanFilterPolicyEnum enumItem)
			{
				return enumItem switch
				{
					LeExScanFilterPolicyEnum.Accept_all_except_directed => "Accept all advertisement packets except directed advertising packets not addressed to this device (default).", 
					LeExScanFilterPolicyEnum.Accept_only => "Accept only advertisement packets from devices where the advertiser’s address is in the White list. Directed advertising packets which are not addressed for this device shall be ignored.", 
					_ => "Invalid LeExScanFilterPolicy Value", 
				};
			}

			public static string LeExScanFilterPolicyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Accept all advertisement packets except directed advertising packets not addressed to this device (default).");
				stringBuilder.AppendLine("0x1 = Accept only advertisement packets from devices where the advertiser’s address is in the White list. Directed advertising packets which are not addressed for this device shall be ignored.");
				return stringBuilder.ToString();
			}
		}

		private uint _LeExScanType;

		private uint _LeExScanInterval = 4u;

		private uint _LeExScanWindow = 4u;

		private uint _OwnAddressType;

		private uint _LeExScanFilterPolicy;

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

		public uint LeExScanInterval
		{
			get
			{
				return _LeExScanInterval;
			}
			set
			{
				_LeExScanInterval = value;
			}
		}

		public uint LeExScanWindow
		{
			get
			{
				return _LeExScanWindow;
			}
			set
			{
				_LeExScanWindow = 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 LeExScanFilterPolicy
		{
			get
			{
				return _LeExScanFilterPolicy;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for LeExScanFilterPolicy is 0xFF");
				}
				_LeExScanFilterPolicy = value;
			}
		}

		public LeExScanTypeEnum GetLeExScanTypeAsLeExScanTypeEnum()
		{
			return LeExScanTypeInfo.ValueToLeExScanType(_LeExScanType);
		}

		public void SetLeExScanTypeAsLeExScanTypeEnum(LeExScanTypeEnum enumItem)
		{
			LeExScanType = LeExScanTypeInfo.LeExScanTypeToValue(enumItem);
		}

		public DeviceAddressTypeEnum GetOwnAddressTypeAsDeviceAddressTypeEnum()
		{
			return DeviceAddressTypeInfo.ValueToDeviceAddressType(_OwnAddressType);
		}

		public void SetOwnAddressTypeAsDeviceAddressTypeEnum(DeviceAddressTypeEnum enumItem)
		{
			OwnAddressType = DeviceAddressTypeInfo.DeviceAddressTypeToValue(enumItem);
		}

		public LeExScanFilterPolicyEnum GetLeExScanFilterPolicyAsLeExScanFilterPolicyEnum()
		{
			return LeExScanFilterPolicyInfo.ValueToLeExScanFilterPolicy(_LeExScanFilterPolicy);
		}

		public void SetLeExScanFilterPolicyAsLeExScanFilterPolicyEnum(LeExScanFilterPolicyEnum enumItem)
		{
			LeExScanFilterPolicy = LeExScanFilterPolicyInfo.LeExScanFilterPolicyToValue(enumItem);
		}
	}

	private const uint _Opcode = 64858u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeExtendedSetScanParameters),
		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 LeExtendedSetScanParametersInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64858u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.LeExScanInterval = 16u;
		UserFields.LeExScanWindow = 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.LeExScanType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LeExScanInterval = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.LeExScanWindow = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.OwnAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LeExScanFilterPolicy = 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.LeExScanType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.LeExScanInterval, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.LeExScanWindow, 32);
		bitBuilder.AddBitsAsUint(UserFields.OwnAddressType, 8);
		bitBuilder.AddBitsAsUint(UserFields.LeExScanFilterPolicy, 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.LeExScanInterval < 4)
		{
			list.Add("Minimum allowed value for LE_Ex_Scan_Interval is 0x4");
		}
		if (UserFields.LeExScanInterval > 16777215)
		{
			list.Add("Maximum allowed value for LE_Ex_Scan_Interval is 0xFFFFFF");
		}
		if (UserFields.LeExScanWindow < 4)
		{
			list.Add("Minimum allowed value for LE_Ex_Scan_Window is 0x4");
		}
		if (UserFields.LeExScanWindow > 65535)
		{
			list.Add("Maximum allowed value for LE_Ex_Scan_Window is 0xFFFF");
		}
		return list;
	}
}
