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

namespace QCRPG.TSL.BT.Packets;

public class LePathLossThresholdEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum CurrentPathLossEnum
		{
			Unavailable,
			InvalidCurrentPathLossValue
		}

		public static class CurrentPathLossInfo
		{
			public static CurrentPathLossEnum ValueToCurrentPathLoss(uint value)
			{
				if (value == 255)
				{
					return CurrentPathLossEnum.Unavailable;
				}
				return CurrentPathLossEnum.InvalidCurrentPathLossValue;
			}

			public static uint CurrentPathLossToValue(CurrentPathLossEnum enumItem)
			{
				if (enumItem == CurrentPathLossEnum.Unavailable)
				{
					return 255u;
				}
				return uint.MaxValue;
			}

			public static string CurrentPathLossToString(CurrentPathLossEnum enumItem)
			{
				if (enumItem == CurrentPathLossEnum.Unavailable)
				{
					return "Path loss is Unavailable";
				}
				return "Invalid CurrentPathLoss Value";
			}

			public static string CurrentPathLossToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0xFF = Path loss is Unavailable");
				return stringBuilder.ToString();
			}
		}

		public enum ZoneEnteredEnum
		{
			low_zone,
			middle_zone,
			high_zone,
			InvalidZoneEnteredValue
		}

		public static class ZoneEnteredInfo
		{
			public static ZoneEnteredEnum ValueToZoneEntered(uint value)
			{
				return value switch
				{
					0u => ZoneEnteredEnum.low_zone, 
					1u => ZoneEnteredEnum.middle_zone, 
					2u => ZoneEnteredEnum.high_zone, 
					_ => ZoneEnteredEnum.InvalidZoneEnteredValue, 
				};
			}

			public static uint ZoneEnteredToValue(ZoneEnteredEnum enumItem)
			{
				return enumItem switch
				{
					ZoneEnteredEnum.low_zone => 0u, 
					ZoneEnteredEnum.middle_zone => 1u, 
					ZoneEnteredEnum.high_zone => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ZoneEnteredToString(ZoneEnteredEnum enumItem)
			{
				return enumItem switch
				{
					ZoneEnteredEnum.low_zone => "Entered low zone", 
					ZoneEnteredEnum.middle_zone => "Entered middle zone", 
					ZoneEnteredEnum.high_zone => "Entered high zone", 
					_ => "Invalid ZoneEntered Value", 
				};
			}

			public static string ZoneEnteredToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Entered low zone");
				stringBuilder.AppendLine("0x1 = Entered middle zone");
				stringBuilder.AppendLine("0x2 = Entered high zone");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 32u;

		private uint _ConnectionHandle;

		private uint _CurrentPathLoss;

		private uint _ZoneEntered;

		public uint SubeventCode => 32u;

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

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

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

		public CurrentPathLossEnum GetCurrentPathLossAsCurrentPathLossEnum()
		{
			return CurrentPathLossInfo.ValueToCurrentPathLoss(_CurrentPathLoss);
		}

		public void SetCurrentPathLossAsCurrentPathLossEnum(CurrentPathLossEnum enumItem)
		{
			CurrentPathLoss = CurrentPathLossInfo.CurrentPathLossToValue(enumItem);
		}

		public ZoneEnteredEnum GetZoneEnteredAsZoneEnteredEnum()
		{
			return ZoneEnteredInfo.ValueToZoneEntered(_ZoneEntered);
		}

		public void SetZoneEnteredAsZoneEnteredEnum(ZoneEnteredEnum enumItem)
		{
			ZoneEntered = ZoneEnteredInfo.ZoneEnteredToValue(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 LePathLossThresholdEventInfo(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.CurrentPathLoss = 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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.CurrentPathLoss = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ZoneEntered = 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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.CurrentPathLoss, 8);
		bitBuilder.AddBitsAsUint(UserFields.ZoneEntered, 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.CurrentPathLoss < 0)
		{
			list.Add("Minimum allowed value for Current Path Loss is 0x0");
		}
		if (UserFields.CurrentPathLoss > 255)
		{
			list.Add("Maximum allowed value for Current Path Loss is 0xFF");
		}
		return list;
	}
}
