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

namespace QCRPG.TSL.FMHCI.Packets;

public class TxRdsRawGroups : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumberOfRdsGroups;

		private List<RdsGroupListSubField> _RdsGroupListList = new List<RdsGroupListSubField>();

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

		public List<RdsGroupListSubField> RdsGroupListList => _RdsGroupListList;
	}

	public class RdsGroupListSubField
	{
		private uint _RdsGroupBlockInfo1;

		private uint _RdsGroupBlockInfo2;

		private uint _RdsGroupBlockInfo3;

		private uint _RdsGroupBlockInfo4;

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

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

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			RdsGroupBlockInfo1 = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			RdsGroupBlockInfo2 = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			RdsGroupBlockInfo3 = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			RdsGroupBlockInfo4 = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(RdsGroupBlockInfo1, 16);
			bitBuilder.AddBitsAsUintLittleEndian(RdsGroupBlockInfo2, 16);
			bitBuilder.AddBitsAsUintLittleEndian(RdsGroupBlockInfo3, 16);
			bitBuilder.AddBitsAsUintLittleEndian(RdsGroupBlockInfo4, 16);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 20485u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForTxRdsRawGroups) };

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new TxRdsRawGroupsInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 20485u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.NumberOfRdsGroups = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.RdsGroupListList.Count; i++)
		{
			UserFields.RdsGroupListList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfRdsGroups, 8);
		for (int i = 0; i < UserFields.RdsGroupListList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.RdsGroupListList[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.NumberOfRdsGroups > 31)
		{
			list.Add("Maximum allowed value for Number Of RDS Groups is 0x1F");
		}
		return list;
	}
}
