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

namespace QCRPG.TSL.FMHCI.Packets;

public class RdsLockStatus : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum RdsCapabilityEnum
		{
			RDS_DECODER_NOT_SYNCHRONIZED,
			RDS_DECODER_SYNCHRONIZED,
			InvalidRdsCapabilityValue
		}

		public static class RdsCapabilityInfo
		{
			public static RdsCapabilityEnum ValueToRdsCapability(uint value)
			{
				return value switch
				{
					0u => RdsCapabilityEnum.RDS_DECODER_NOT_SYNCHRONIZED, 
					1u => RdsCapabilityEnum.RDS_DECODER_SYNCHRONIZED, 
					_ => RdsCapabilityEnum.InvalidRdsCapabilityValue, 
				};
			}

			public static uint RdsCapabilityToValue(RdsCapabilityEnum enumItem)
			{
				return enumItem switch
				{
					RdsCapabilityEnum.RDS_DECODER_NOT_SYNCHRONIZED => 0u, 
					RdsCapabilityEnum.RDS_DECODER_SYNCHRONIZED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RdsCapabilityToString(RdsCapabilityEnum enumItem)
			{
				return enumItem switch
				{
					RdsCapabilityEnum.RDS_DECODER_NOT_SYNCHRONIZED => "RDS_DECODER_NOT_SYNCHRONIZED", 
					RdsCapabilityEnum.RDS_DECODER_SYNCHRONIZED => "RDS_DECODER_SYNCHRONIZED", 
					_ => "Invalid RdsCapability Value", 
				};
			}

			public static string RdsCapabilityToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = RDS_DECODER_NOT_SYNCHRONIZED");
				stringBuilder.AppendLine("0x1 = RDS_DECODER_SYNCHRONIZED");
				return stringBuilder.ToString();
			}
		}

		private uint _RdsCapability;

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

		public RdsCapabilityEnum GetRdsCapabilityAsRdsCapabilityEnum()
		{
			return RdsCapabilityInfo.ValueToRdsCapability(_RdsCapability);
		}

		public void SetRdsCapabilityAsRdsCapabilityEnum(RdsCapabilityEnum enumItem)
		{
			RdsCapability = RdsCapabilityInfo.RdsCapabilityToValue(enumItem);
		}
	}

	private const uint _EventCode = 2u;

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

	public uint EventCode => 2u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		UserFields.RdsCapability = 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.RdsCapability, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
