using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.NFC.Packets;

public class CoreResetCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ResetTypeEnum
		{
			KeepConfiguration,
			ResetConfiguration,
			RFU,
			InvalidResetTypeValue
		}

		public static class ResetTypeInfo
		{
			public static ResetTypeEnum ValueToResetType(uint value)
			{
				switch (value)
				{
				case 0u:
					return ResetTypeEnum.KeepConfiguration;
				case 1u:
					return ResetTypeEnum.ResetConfiguration;
				default:
					if (value >= 2 && value <= 255)
					{
						return ResetTypeEnum.RFU;
					}
					return ResetTypeEnum.InvalidResetTypeValue;
				}
			}

			public static uint ResetTypeToValue(ResetTypeEnum enumItem)
			{
				return enumItem switch
				{
					ResetTypeEnum.KeepConfiguration => 0u, 
					ResetTypeEnum.ResetConfiguration => 1u, 
					ResetTypeEnum.RFU => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ResetTypeToString(ResetTypeEnum enumItem)
			{
				return enumItem switch
				{
					ResetTypeEnum.KeepConfiguration => "Reset the NFCC and keep the NCI RF Configuration (if supported).", 
					ResetTypeEnum.ResetConfiguration => "Reset the NFCC including the NCI RF Configuration.", 
					ResetTypeEnum.RFU => "RFU", 
					_ => "Invalid ResetType Value", 
				};
			}

			public static string ResetTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Reset the NFCC and keep the NCI RF Configuration (if supported).");
				stringBuilder.AppendLine("0x1 = Reset the NFCC including the NCI RF Configuration.");
				stringBuilder.AppendLine("0x2-0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _ResetType;

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

		public ResetTypeEnum GetResetTypeAsResetTypeEnum()
		{
			return ResetTypeInfo.ValueToResetType(_ResetType);
		}

		public void SetResetTypeAsResetTypeEnum(ResetTypeEnum enumItem)
		{
			ResetType = ResetTypeInfo.ResetTypeToValue(enumItem);
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 0u;

	private const uint _Oid = 0u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 0u;

	public uint Rfu => _Rfu;

	public uint Oid => 0u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(3) != Mt)
		{
			return false;
		}
		_Pbf = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(4) != Gid)
		{
			return false;
		}
		_Rfu = bitBuilder.RemoveBitsAsUint(2);
		if (bitBuilder.RemoveBitsAsUint(6) != Oid)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.ResetType = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(Mt, 3);
		bitBuilder.AddBitsAsUint(Pbf, 1);
		bitBuilder.AddBitsAsUint(Gid, 4);
		bitBuilder.AddBitsAsUint(Rfu, 2);
		bitBuilder.AddBitsAsUint(Oid, 6);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.ResetType, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
