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

namespace QCRPG.TSL.FM.Packets;

public class RdsConfig : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _RegisterAddress = 31u;

		private const uint _XfrMode = 143u;

		private uint _RdsGroupFlushTimer;

		private uint _RdsGroupBufferDepth = 1u;

		private uint _BlockBMatch;

		private uint _BlockBMask;

		private uint _FastPiCheck;

		private uint _RdsGroupFilter;

		private uint _ProgramServiceConfig;

		public uint RegisterAddress => 31u;

		public uint XfrMode => 143u;

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

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

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

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

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

		public uint RdsGroupFilter
		{
			get
			{
				return _RdsGroupFilter;
			}
			set
			{
				_RdsGroupFilter = value;
			}
		}

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

		public RegisterMapEnum GetRegisterAddressAsRegisterMapEnum()
		{
			return RegisterMapInfo.ValueToRegisterMap(31u);
		}

		public XfrModeMapEnum GetXfrModeAsXfrModeMapEnum()
		{
			return XfrModeMapInfo.ValueToXfrModeMap(143u);
		}
	}

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.RegisterAddress)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.XfrMode)
		{
			return false;
		}
		UserFields.RdsGroupFlushTimer = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RdsGroupBufferDepth = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BlockBMatch = bitBuilder.RemoveBitsAsUint(16);
		UserFields.BlockBMask = bitBuilder.RemoveBitsAsUint(16);
		UserFields.FastPiCheck = bitBuilder.RemoveBitsAsUint(16);
		UserFields.RdsGroupFilter = bitBuilder.RemoveBitsAsUint(32);
		UserFields.ProgramServiceConfig = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.RegisterAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.XfrMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.RdsGroupFlushTimer, 8);
		bitBuilder.AddBitsAsUint(UserFields.RdsGroupBufferDepth, 8);
		bitBuilder.AddBitsAsUint(UserFields.BlockBMatch, 16);
		bitBuilder.AddBitsAsUint(UserFields.BlockBMask, 16);
		bitBuilder.AddBitsAsUint(UserFields.FastPiCheck, 16);
		bitBuilder.AddBitsAsUint(UserFields.RdsGroupFilter, 32);
		bitBuilder.AddBitsAsUint(UserFields.ProgramServiceConfig, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.RdsGroupBufferDepth < 1)
		{
			list.Add("Minimum allowed value for RDS Group Buffer Depth is 0x1");
		}
		if (UserFields.RdsGroupBufferDepth > 22)
		{
			list.Add("Maximum allowed value for RDS Group Buffer Depth is 0x16");
		}
		return list;
	}
}
