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

namespace QCRPG.TSL.FMHCI.Packets;

public class ReadExtGroupCounters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ResetCountersEnum
		{
			DO_NOT_RESET,
			RESET,
			InvalidResetCountersValue
		}

		public static class ResetCountersInfo
		{
			public static ResetCountersEnum ValueToResetCounters(uint value)
			{
				return value switch
				{
					0u => ResetCountersEnum.DO_NOT_RESET, 
					1u => ResetCountersEnum.RESET, 
					_ => ResetCountersEnum.InvalidResetCountersValue, 
				};
			}

			public static uint ResetCountersToValue(ResetCountersEnum enumItem)
			{
				return enumItem switch
				{
					ResetCountersEnum.DO_NOT_RESET => 0u, 
					ResetCountersEnum.RESET => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ResetCountersToString(ResetCountersEnum enumItem)
			{
				return enumItem switch
				{
					ResetCountersEnum.DO_NOT_RESET => "Do not reset the RDS group counters", 
					ResetCountersEnum.RESET => "Reset the RDS group counters after transmitting event response", 
					_ => "Invalid ResetCounters Value", 
				};
			}

			public static string ResetCountersToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Do not reset the RDS group counters");
				stringBuilder.AppendLine("0x1 = Reset the RDS group counters after transmitting event response");
				return stringBuilder.ToString();
			}
		}

		private uint _ResetCounters;

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

		public ResetCountersEnum GetResetCountersAsResetCountersEnum()
		{
			return ResetCountersInfo.ValueToResetCounters(_ResetCounters);
		}

		public void SetResetCountersAsResetCountersEnum(ResetCountersEnum enumItem)
		{
			ResetCounters = ResetCountersInfo.ResetCountersToValue(enumItem);
		}
	}

	private const uint _Opcode = 22530u;

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

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

	public uint Opcode => 22530u;

	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.ResetCounters = bitBuilder.RemoveBitsAsUint(8);
		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.ResetCounters, 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.ResetCounters > 1)
		{
			list.Add("Maximum allowed value for Reset Counters is 0x1");
		}
		return list;
	}
}
