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

namespace QCRPG.TSL.FMHCI.Packets;

public class ServiceAvailable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ServiceAvailableEnum
		{
			NO_STATIONS_FOUND,
			AT_LEAST_ONE_STATION_FOUND,
			InvalidServiceAvailableValue
		}

		public static class ServiceAvailableInfo
		{
			public static ServiceAvailableEnum ValueToServiceAvailable(uint value)
			{
				return value switch
				{
					0u => ServiceAvailableEnum.NO_STATIONS_FOUND, 
					1u => ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND, 
					_ => ServiceAvailableEnum.InvalidServiceAvailableValue, 
				};
			}

			public static uint ServiceAvailableToValue(ServiceAvailableEnum enumItem)
			{
				return enumItem switch
				{
					ServiceAvailableEnum.NO_STATIONS_FOUND => 0u, 
					ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ServiceAvailableToString(ServiceAvailableEnum enumItem)
			{
				return enumItem switch
				{
					ServiceAvailableEnum.NO_STATIONS_FOUND => "No stations were found in the FM band", 
					ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND => "At least one station was found in the FM band", 
					_ => "Invalid ServiceAvailable Value", 
				};
			}

			public static string ServiceAvailableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No stations were found in the FM band");
				stringBuilder.AppendLine("0x1 = At least one station was found in the FM band");
				return stringBuilder.ToString();
			}
		}

		private uint _ServiceAvailable;

		private uint _Sinr;

		private uint _InterferenceDetectorThreshold;

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

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

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

		public ServiceAvailableEnum GetServiceAvailableAsServiceAvailableEnum()
		{
			return ServiceAvailableInfo.ValueToServiceAvailable(_ServiceAvailable);
		}

		public void SetServiceAvailableAsServiceAvailableEnum(ServiceAvailableEnum enumItem)
		{
			ServiceAvailable = ServiceAvailableInfo.ServiceAvailableToValue(enumItem);
		}
	}

	private const uint _EventCode = 4u;

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

	public uint EventCode => 4u;

	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.ServiceAvailable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Sinr = bitBuilder.RemoveBitsAsUint(8);
		UserFields.InterferenceDetectorThreshold = 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.ServiceAvailable, 8);
		bitBuilder.AddBitsAsUint(UserFields.Sinr, 8);
		bitBuilder.AddBitsAsUint(UserFields.InterferenceDetectorThreshold, 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.ServiceAvailable > 1)
		{
			list.Add("Maximum allowed value for Service Available is 0x1");
		}
		return list;
	}
}
