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

namespace QCRPG.TSL.FMHCI.Packets;

public class AfList : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _TuneFrequency;

		private uint _ProgramIdentificationCode;

		private uint _NumberOfAlternativeFrequencies;

		private List<AlternativeFrequencyListSubField> _AlternativeFrequencyListList = new List<AlternativeFrequencyListSubField>();

		public uint TuneFrequency
		{
			get
			{
				return _TuneFrequency;
			}
			set
			{
				_TuneFrequency = value;
			}
		}

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

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

		public List<AlternativeFrequencyListSubField> AlternativeFrequencyListList => _AlternativeFrequencyListList;
	}

	public class AlternativeFrequencyListSubField
	{
		private uint _AlternativeFrequency;

		public uint AlternativeFrequency
		{
			get
			{
				return _AlternativeFrequency;
			}
			set
			{
				_AlternativeFrequency = value;
			}
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			AlternativeFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(AlternativeFrequency, 32);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 11u;

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

	public uint EventCode => 11u;

	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.TuneFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.ProgramIdentificationCode = bitBuilder.RemoveBitsAsUint(16);
		UserFields.NumberOfAlternativeFrequencies = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.AlternativeFrequencyListList.Count; i++)
		{
			UserFields.AlternativeFrequencyListList[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.AddBitsAsUintLittleEndian(UserFields.TuneFrequency, 32);
		bitBuilder.AddBitsAsUint(UserFields.ProgramIdentificationCode, 16);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfAlternativeFrequencies, 8);
		for (int i = 0; i < UserFields.AlternativeFrequencyListList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.AlternativeFrequencyListList[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();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.NumberOfAlternativeFrequencies > 25)
		{
			list.Add("Maximum allowed value for Number Of Alternative Frequencies is 0x19");
		}
		return list;
	}
}
