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

namespace QCRPG.TSL.FMHCI.Packets;

public class RdsGroups : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableRdsChangeFilterEnum
		{
			DISABLED,
			ENABLED,
			InvalidEnableRdsChangeFilterValue
		}

		public static class EnableRdsChangeFilterInfo
		{
			public static EnableRdsChangeFilterEnum ValueToEnableRdsChangeFilter(uint value)
			{
				return value switch
				{
					0u => EnableRdsChangeFilterEnum.DISABLED, 
					1u => EnableRdsChangeFilterEnum.ENABLED, 
					_ => EnableRdsChangeFilterEnum.InvalidEnableRdsChangeFilterValue, 
				};
			}

			public static uint EnableRdsChangeFilterToValue(EnableRdsChangeFilterEnum enumItem)
			{
				return enumItem switch
				{
					EnableRdsChangeFilterEnum.DISABLED => 0u, 
					EnableRdsChangeFilterEnum.ENABLED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableRdsChangeFilterToString(EnableRdsChangeFilterEnum enumItem)
			{
				return enumItem switch
				{
					EnableRdsChangeFilterEnum.DISABLED => "The RDS/RBDS change filter should not be enabled", 
					EnableRdsChangeFilterEnum.ENABLED => "The RDS/RBDS change filter should be enabled", 
					_ => "Invalid EnableRdsChangeFilter Value", 
				};
			}

			public static string EnableRdsChangeFilterToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS change filter should not be enabled");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS change filter should be enabled");
				return stringBuilder.ToString();
			}
		}

		private uint _RdsGroupEnableMask;

		private uint _RdsBufferSize;

		private uint _EnableRdsChangeFilter;

		public uint RdsGroupEnableMask
		{
			get
			{
				return _RdsGroupEnableMask;
			}
			set
			{
				_RdsGroupEnableMask = value;
			}
		}

		public uint RdsBufferSize
		{
			get
			{
				return _RdsBufferSize;
			}
			set
			{
				_RdsBufferSize = value;
			}
		}

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

		public EnableRdsChangeFilterEnum GetEnableRdsChangeFilterAsEnableRdsChangeFilterEnum()
		{
			return EnableRdsChangeFilterInfo.ValueToEnableRdsChangeFilter(_EnableRdsChangeFilter);
		}

		public void SetEnableRdsChangeFilterAsEnableRdsChangeFilterEnum(EnableRdsChangeFilterEnum enumItem)
		{
			EnableRdsChangeFilter = EnableRdsChangeFilterInfo.EnableRdsChangeFilterToValue(enumItem);
		}
	}

	private const uint _Opcode = 19474u;

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

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

	public uint Opcode => 19474u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.RdsBufferSize = 1u;
	}

	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.RdsGroupEnableMask = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.RdsBufferSize = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.EnableRdsChangeFilter = 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.AddBitsAsUintLittleEndian(UserFields.RdsGroupEnableMask, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.RdsBufferSize, 32);
		bitBuilder.AddBitsAsUint(UserFields.EnableRdsChangeFilter, 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.EnableRdsChangeFilter > 1)
		{
			list.Add("Maximum allowed value for Enable RDS Change Filter is 0x1");
		}
		return list;
	}
}
