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

namespace QCRPG.TSL.BT.Packets;

public class LeBatchScanSetBatchScanParam : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BatchScanModeEnum
		{
			Batch_scan_is_disabled,
			Truncated_mode_enabled,
			Full_mode_enabled,
			Truncated_and_Full_mode_enabled,
			InvalidBatchScanModeValue
		}

		public static class BatchScanModeInfo
		{
			public static BatchScanModeEnum ValueToBatchScanMode(uint value)
			{
				return value switch
				{
					0u => BatchScanModeEnum.Batch_scan_is_disabled, 
					1u => BatchScanModeEnum.Truncated_mode_enabled, 
					2u => BatchScanModeEnum.Full_mode_enabled, 
					3u => BatchScanModeEnum.Truncated_and_Full_mode_enabled, 
					_ => BatchScanModeEnum.InvalidBatchScanModeValue, 
				};
			}

			public static uint BatchScanModeToValue(BatchScanModeEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanModeEnum.Batch_scan_is_disabled => 0u, 
					BatchScanModeEnum.Truncated_mode_enabled => 1u, 
					BatchScanModeEnum.Full_mode_enabled => 2u, 
					BatchScanModeEnum.Truncated_and_Full_mode_enabled => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string BatchScanModeToString(BatchScanModeEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanModeEnum.Batch_scan_is_disabled => "Batch scan is disabled", 
					BatchScanModeEnum.Truncated_mode_enabled => "Truncated mode enabled", 
					BatchScanModeEnum.Full_mode_enabled => "Full mode enabled", 
					BatchScanModeEnum.Truncated_and_Full_mode_enabled => "Truncated & Full mode enabled", 
					_ => "Invalid BatchScanMode Value", 
				};
			}

			public static string BatchScanModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Batch scan is disabled");
				stringBuilder.AppendLine("0x1 = Truncated mode enabled");
				stringBuilder.AppendLine("0x2 = Full mode enabled");
				stringBuilder.AppendLine("0x3 = Truncated & Full mode enabled");
				return stringBuilder.ToString();
			}
		}

		public enum BatchScanDiscardRuleEnum
		{
			Discard_oldest_advertisement,
			Discard_advertisement_with_weakest_RSSI,
			InvalidBatchScanDiscardRuleValue
		}

		public static class BatchScanDiscardRuleInfo
		{
			public static BatchScanDiscardRuleEnum ValueToBatchScanDiscardRule(uint value)
			{
				return value switch
				{
					0u => BatchScanDiscardRuleEnum.Discard_oldest_advertisement, 
					1u => BatchScanDiscardRuleEnum.Discard_advertisement_with_weakest_RSSI, 
					_ => BatchScanDiscardRuleEnum.InvalidBatchScanDiscardRuleValue, 
				};
			}

			public static uint BatchScanDiscardRuleToValue(BatchScanDiscardRuleEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanDiscardRuleEnum.Discard_oldest_advertisement => 0u, 
					BatchScanDiscardRuleEnum.Discard_advertisement_with_weakest_RSSI => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string BatchScanDiscardRuleToString(BatchScanDiscardRuleEnum enumItem)
			{
				return enumItem switch
				{
					BatchScanDiscardRuleEnum.Discard_oldest_advertisement => "Discard oldest advertisement", 
					BatchScanDiscardRuleEnum.Discard_advertisement_with_weakest_RSSI => "Discard advertisement with weakest RSSI", 
					_ => "Invalid BatchScanDiscardRule Value", 
				};
			}

			public static string BatchScanDiscardRuleToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Discard oldest advertisement");
				stringBuilder.AppendLine("0x1 = Discard advertisement with weakest RSSI");
				return stringBuilder.ToString();
			}
		}

		private const uint _BatchScanOpcode = 3u;

		private uint _BatchScanMode;

		private uint _DutyCycleScanWindow;

		private uint _DutyCyleScanInterval;

		private uint _OwnAddressType;

		private uint _BatchScanDiscardRule;

		public uint BatchScanOpcode => 3u;

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

		public uint DutyCycleScanWindow
		{
			get
			{
				return _DutyCycleScanWindow;
			}
			set
			{
				_DutyCycleScanWindow = value;
			}
		}

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

		public BatchScanModeEnum GetBatchScanModeAsBatchScanModeEnum()
		{
			return BatchScanModeInfo.ValueToBatchScanMode(_BatchScanMode);
		}

		public void SetBatchScanModeAsBatchScanModeEnum(BatchScanModeEnum enumItem)
		{
			BatchScanMode = BatchScanModeInfo.BatchScanModeToValue(enumItem);
		}

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

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

		public BatchScanDiscardRuleEnum GetBatchScanDiscardRuleAsBatchScanDiscardRuleEnum()
		{
			return BatchScanDiscardRuleInfo.ValueToBatchScanDiscardRule(_BatchScanDiscardRule);
		}

		public void SetBatchScanDiscardRuleAsBatchScanDiscardRuleEnum(BatchScanDiscardRuleEnum enumItem)
		{
			BatchScanDiscardRule = BatchScanDiscardRuleInfo.BatchScanDiscardRuleToValue(enumItem);
		}
	}

	private const uint _Opcode = 64854u;

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

	public uint Opcode => 64854u;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.BatchScanOpcode)
		{
			return false;
		}
		UserFields.BatchScanMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DutyCycleScanWindow = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.DutyCyleScanInterval = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.OwnAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BatchScanDiscardRule = 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.BatchScanMode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.DutyCycleScanWindow, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.DutyCyleScanInterval, 32);
		bitBuilder.AddBitsAsUint(UserFields.OwnAddressType, 8);
		bitBuilder.AddBitsAsUint(UserFields.BatchScanDiscardRule, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
