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

namespace QCRPG.TSL.BT.Packets;

public class PersistAccessOtpSet : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubOpcode = 1u;

		private uint _CfgType;

		private uint _CfgLen;

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

		public uint SubOpcode => 1u;

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

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

		public List<uint> CfgDataList => _CfgDataList;

		public CfgTypeOtpAccessEnum GetCfgTypeAsCfgTypeOtpAccessEnum()
		{
			return CfgTypeOtpAccessInfo.ValueToCfgTypeOtpAccess(_CfgType);
		}

		public void SetCfgTypeAsCfgTypeOtpAccessEnum(CfgTypeOtpAccessEnum enumItem)
		{
			CfgType = CfgTypeOtpAccessInfo.CfgTypeOtpAccessToValue(enumItem);
		}
	}

	private const uint _Opcode = 64521u;

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

	public uint Opcode => 64521u;

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