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

namespace QCRPG.TSL.BT.Packets;

public class NvmAccessSet : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _NvmCommand = 1u;

		private uint _TagNumber;

		private uint _Length;

		private List<uint> _TagValueList = new List<uint>();

		public uint NvmCommand => 1u;

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

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

		public List<uint> TagValueList => _TagValueList;
	}

	private const uint _Opcode = 64523u;

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

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

	public uint Opcode => 64523u;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.NvmCommand)
		{
			return false;
		}
		UserFields.TagNumber = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Length = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.TagValueList.Count; i++)
		{
			UserFields.TagValueList[i] = 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.NvmCommand, 8);
		bitBuilder.AddBitsAsUint(UserFields.TagNumber, 8);
		bitBuilder.AddBitsAsUint(UserFields.Length, 8);
		for (int i = 0; i < UserFields.TagValueList.Count; i++)
		{
			if (UserFields.TagValueList[i] > 255)
			{
				throw new Exception("UserFields.TagValueList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.TagValueList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
