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

namespace QCRPG.TSL.BT.Packets;

public class LeTransmitPowerReportingEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ReasonEnum
		{
			local,
			remote,
			hci,
			InvalidReasonValue
		}

		public static class ReasonInfo
		{
			public static ReasonEnum ValueToReason(uint value)
			{
				return value switch
				{
					0u => ReasonEnum.local, 
					1u => ReasonEnum.remote, 
					2u => ReasonEnum.hci, 
					_ => ReasonEnum.InvalidReasonValue, 
				};
			}

			public static uint ReasonToValue(ReasonEnum enumItem)
			{
				return enumItem switch
				{
					ReasonEnum.local => 0u, 
					ReasonEnum.remote => 1u, 
					ReasonEnum.hci => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ReasonToString(ReasonEnum enumItem)
			{
				return enumItem switch
				{
					ReasonEnum.local => "Local transmit power changed", 
					ReasonEnum.remote => "Remote transmit power changed", 
					ReasonEnum.hci => "HCI_LE_Read_Remote_Transmit_Power_Level command completed", 
					_ => "Invalid Reason Value", 
				};
			}

			public static string ReasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Local transmit power changed");
				stringBuilder.AppendLine("0x1 = Remote transmit power changed");
				stringBuilder.AppendLine("0x2 = HCI_LE_Read_Remote_Transmit_Power_Level command completed");
				return stringBuilder.ToString();
			}
		}

		public enum PhyEnum
		{
			LE_PHY_1M,
			LE_PHY_2M,
			LE_PHY_Coded_S8,
			LE_PHY_Coded_S2,
			InvalidPhyValue
		}

		public static class PhyInfo
		{
			public static PhyEnum ValueToPhy(uint value)
			{
				return value switch
				{
					1u => PhyEnum.LE_PHY_1M, 
					2u => PhyEnum.LE_PHY_2M, 
					3u => PhyEnum.LE_PHY_Coded_S8, 
					4u => PhyEnum.LE_PHY_Coded_S2, 
					_ => PhyEnum.InvalidPhyValue, 
				};
			}

			public static uint PhyToValue(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_PHY_1M => 1u, 
					PhyEnum.LE_PHY_2M => 2u, 
					PhyEnum.LE_PHY_Coded_S8 => 3u, 
					PhyEnum.LE_PHY_Coded_S2 => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PhyToString(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_PHY_1M => "LE 1M PHY", 
					PhyEnum.LE_PHY_2M => "LE 2M PHY", 
					PhyEnum.LE_PHY_Coded_S8 => "LE Coded PHY with S=8 data coding", 
					PhyEnum.LE_PHY_Coded_S2 => "LE Coded PHY with S=2 data coding", 
					_ => "Invalid Phy Value", 
				};
			}

			public static string PhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = LE 1M PHY");
				stringBuilder.AppendLine("0x2 = LE 2M PHY");
				stringBuilder.AppendLine("0x3 = LE Coded PHY with S=8 data coding");
				stringBuilder.AppendLine("0x4 = LE Coded PHY with S=2 data coding");
				return stringBuilder.ToString();
			}
		}

		public enum TransmitPowerLevelEnum
		{
			not_managed,
			not_available,
			InvalidTransmitPowerLevelValue
		}

		public static class TransmitPowerLevelInfo
		{
			public static TransmitPowerLevelEnum ValueToTransmitPowerLevel(uint value)
			{
				return value switch
				{
					126u => TransmitPowerLevelEnum.not_managed, 
					127u => TransmitPowerLevelEnum.not_available, 
					_ => TransmitPowerLevelEnum.InvalidTransmitPowerLevelValue, 
				};
			}

			public static uint TransmitPowerLevelToValue(TransmitPowerLevelEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelEnum.not_managed => 126u, 
					TransmitPowerLevelEnum.not_available => 127u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TransmitPowerLevelToString(TransmitPowerLevelEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelEnum.not_managed => "Remote device is not managing power levels on this PHY", 
					TransmitPowerLevelEnum.not_available => "Transmit power level is not available", 
					_ => "Invalid TransmitPowerLevel Value", 
				};
			}

			public static string TransmitPowerLevelToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x7E = Remote device is not managing power levels on this PHY");
				stringBuilder.AppendLine("0x7F = Transmit power level is not available");
				return stringBuilder.ToString();
			}
		}

		public enum TransmitPowerLevelFlagEnum
		{
			min_level,
			max_level,
			InvalidTransmitPowerLevelFlagValue
		}

		public static class TransmitPowerLevelFlagInfo
		{
			public static TransmitPowerLevelFlagEnum ValueToTransmitPowerLevelFlag(uint value)
			{
				return value switch
				{
					0u => TransmitPowerLevelFlagEnum.min_level, 
					1u => TransmitPowerLevelFlagEnum.max_level, 
					_ => TransmitPowerLevelFlagEnum.InvalidTransmitPowerLevelFlagValue, 
				};
			}

			public static uint TransmitPowerLevelFlagToValue(TransmitPowerLevelFlagEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelFlagEnum.min_level => 0u, 
					TransmitPowerLevelFlagEnum.max_level => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TransmitPowerLevelFlagToString(TransmitPowerLevelFlagEnum enumItem)
			{
				return enumItem switch
				{
					TransmitPowerLevelFlagEnum.min_level => "Transmit power level is at minimum level", 
					TransmitPowerLevelFlagEnum.max_level => "Transmit power level is at maximum level", 
					_ => "Invalid TransmitPowerLevelFlag Value", 
				};
			}

			public static string TransmitPowerLevelFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Transmit power level is at minimum level");
				stringBuilder.AppendLine("0x1 = Transmit power level is at maximum level");
				return stringBuilder.ToString();
			}
		}

		public enum DeltaEnum
		{
			not_available_or_out_of_range,
			InvalidDeltaValue
		}

		public static class DeltaInfo
		{
			public static DeltaEnum ValueToDelta(uint value)
			{
				if (value == 127)
				{
					return DeltaEnum.not_available_or_out_of_range;
				}
				return DeltaEnum.InvalidDeltaValue;
			}

			public static uint DeltaToValue(DeltaEnum enumItem)
			{
				if (enumItem == DeltaEnum.not_available_or_out_of_range)
				{
					return 127u;
				}
				return uint.MaxValue;
			}

			public static string DeltaToString(DeltaEnum enumItem)
			{
				if (enumItem == DeltaEnum.not_available_or_out_of_range)
				{
					return "Change is not available or is out of range";
				}
				return "Invalid Delta Value";
			}

			public static string DeltaToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x7F = Change is not available or is out of range");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 33u;

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _Reason;

		private uint _Phy;

		private uint _TransmitPowerLevel;

		private uint _TransmitPowerLevelFlag;

		private uint _Delta;

		public uint SubeventCode => 33u;

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

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

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

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

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

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

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

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public ReasonEnum GetReasonAsReasonEnum()
		{
			return ReasonInfo.ValueToReason(_Reason);
		}

		public void SetReasonAsReasonEnum(ReasonEnum enumItem)
		{
			Reason = ReasonInfo.ReasonToValue(enumItem);
		}

		public PhyEnum GetPhyAsPhyEnum()
		{
			return PhyInfo.ValueToPhy(_Phy);
		}

		public void SetPhyAsPhyEnum(PhyEnum enumItem)
		{
			Phy = PhyInfo.PhyToValue(enumItem);
		}

		public TransmitPowerLevelEnum GetTransmitPowerLevelAsTransmitPowerLevelEnum()
		{
			return TransmitPowerLevelInfo.ValueToTransmitPowerLevel(_TransmitPowerLevel);
		}

		public void SetTransmitPowerLevelAsTransmitPowerLevelEnum(TransmitPowerLevelEnum enumItem)
		{
			TransmitPowerLevel = TransmitPowerLevelInfo.TransmitPowerLevelToValue(enumItem);
		}

		public TransmitPowerLevelFlagEnum GetTransmitPowerLevelFlagAsTransmitPowerLevelFlagEnum()
		{
			return TransmitPowerLevelFlagInfo.ValueToTransmitPowerLevelFlag(_TransmitPowerLevelFlag);
		}

		public void SetTransmitPowerLevelFlagAsTransmitPowerLevelFlagEnum(TransmitPowerLevelFlagEnum enumItem)
		{
			TransmitPowerLevelFlag = TransmitPowerLevelFlagInfo.TransmitPowerLevelFlagToValue(enumItem);
		}

		public DeltaEnum GetDeltaAsDeltaEnum()
		{
			return DeltaInfo.ValueToDelta(_Delta);
		}

		public void SetDeltaAsDeltaEnum(DeltaEnum enumItem)
		{
			Delta = DeltaInfo.DeltaToValue(enumItem);
		}
	}

	private const uint _EventCode = 62u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new LeTransmitPowerReportingEventInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.TransmitPowerLevel = 0u;
		UserFields.Delta = 0u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			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.SubeventCode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Reason = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TransmitPowerLevel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TransmitPowerLevelFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Delta = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubeventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Reason, 8);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.TransmitPowerLevel, 8);
		bitBuilder.AddBitsAsUint(UserFields.TransmitPowerLevelFlag, 8);
		bitBuilder.AddBitsAsUint(UserFields.Delta, 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.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection Handle is 0xEFF");
		}
		if (UserFields.TransmitPowerLevel < 0)
		{
			list.Add("Minimum allowed value for Transmit Power Level is 0x0");
		}
		if (UserFields.TransmitPowerLevel > 127)
		{
			list.Add("Maximum allowed value for Transmit Power Level is 0x7F");
		}
		if (UserFields.Delta < 0)
		{
			list.Add("Minimum allowed value for Delta is 0x0");
		}
		if (UserFields.Delta > 127)
		{
			list.Add("Maximum allowed value for Delta is 0x7F");
		}
		return list;
	}
}
