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

namespace QCRPG.TSL.BT.Packets;

public class SetEventFilterConnectionSetup : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _FilterType = 2u;

		private uint _ConditionType;

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

		public uint FilterType => 2u;

		public uint ConditionType
		{
			get
			{
				return _ConditionType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ConditionType is 0xFF");
				}
				_ConditionType = value;
				int num = 0;
				if (ConditionType == 0)
				{
					num = 1;
				}
				else if (ConditionType == 1)
				{
					num = 7;
				}
				else if (ConditionType == 2)
				{
					num = 7;
				}
				while (_ConditionList.Count < num)
				{
					_ConditionList.Add(0u);
				}
				while (_ConditionList.Count > num)
				{
					_ConditionList.RemoveAt(_ConditionList.Count - 1);
				}
			}
		}

		public List<uint> ConditionList => _ConditionList;

		public FilterConditionTypeEnum GetConditionTypeAsFilterConditionTypeEnum()
		{
			return FilterConditionTypeInfo.ValueToFilterConditionType(_ConditionType);
		}

		public void SetConditionTypeAsFilterConditionTypeEnum(FilterConditionTypeEnum enumItem)
		{
			ConditionType = FilterConditionTypeInfo.FilterConditionTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 3077u;

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

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

	public uint Opcode => 3077u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConditionType = 0u;
	}

	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.FilterType)
		{
			return false;
		}
		UserFields.ConditionType = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.ConditionList.Count - 1; num >= 0; num--)
		{
			UserFields.ConditionList[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.FilterType, 8);
		bitBuilder.AddBitsAsUint(UserFields.ConditionType, 8);
		for (int num = UserFields.ConditionList.Count - 1; num >= 0; num--)
		{
			if (UserFields.ConditionList[num] > 255)
			{
				throw new Exception("UserFields.ConditionList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.ConditionList[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();
	}
}
