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

namespace QCRPG.TSL.FMHCI.Packets;

public class SearchListComplete : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Status;

		private uint _NumberOfStationsFound;

		private List<StationListSubField> _StationListList = new List<StationListSubField>();

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

		public uint NumberOfStationsFound
		{
			get
			{
				return _NumberOfStationsFound;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfStationsFound is 0xFF");
				}
				_NumberOfStationsFound = value;
				while (_StationListList.Count < _NumberOfStationsFound)
				{
					_StationListList.Add(new StationListSubField());
				}
				while (_StationListList.Count > _NumberOfStationsFound)
				{
					_StationListList.RemoveAt(_StationListList.Count - 1);
				}
			}
		}

		public List<StationListSubField> StationListList => _StationListList;

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

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

	public class StationListSubField
	{
		public enum RdsCapabilityEnum
		{
			NO_RDS_DATA_RECEIVED,
			RDS_DATA_RECEIVED,
			InvalidRdsCapabilityValue
		}

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

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

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

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

		private uint _StationFrequency;

		private uint _RdsCapability;

		private uint _ProgramType;

		private uint _ProgramIdentificationCode;

		public uint StationFrequency
		{
			get
			{
				return _StationFrequency;
			}
			set
			{
				_StationFrequency = value;
			}
		}

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

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

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

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			StationFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			RdsCapability = bitBuilder.RemoveBitsAsUint(8);
			ProgramType = bitBuilder.RemoveBitsAsUint(8);
			ProgramIdentificationCode = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(StationFrequency, 32);
			bitBuilder.AddBitsAsUint(RdsCapability, 8);
			bitBuilder.AddBitsAsUint(ProgramType, 8);
			bitBuilder.AddBitsAsUintLittleEndian(ProgramIdentificationCode, 16);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (RdsCapability > 1)
			{
				list.Add("Maximum allowed value for RDS Capability is 0x1");
			}
			return list;
		}
	}

	private const uint _EventCode = 20u;

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

	public uint EventCode => 20u;

	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.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfStationsFound = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.StationListList.Count; i++)
		{
			UserFields.StationListList[i].FromBitBuilder(ref bitBuilder);
		}
		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.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfStationsFound, 8);
		for (int i = 0; i < UserFields.StationListList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.StationListList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
