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

namespace QCRPG.TSL.BT.Packets;

public class DebugCxmLog : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _VsEventType = 12u;

		private const uint _SubOpcode = 53u;

		private uint _MciMwsMsgPayloadLength;

		private uint _MciMwsMsgHeader;

		private uint _PaddingAndTimestamp;

		private uint _Byte9;

		private uint _Byte10;

		private uint _Byte11;

		private uint _Byte12;

		private uint _MciMwsGpmType;

		private uint _MciMwsAgentType;

		private List<RemainingPayloadSubField> _RemainingPayloadList = new List<RemainingPayloadSubField>();

		public uint VsEventType => 12u;

		public uint SubOpcode => 53u;

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

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

		public uint PaddingAndTimestamp
		{
			get
			{
				return _PaddingAndTimestamp;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for PaddingAndTimestamp is 0xFFFFFF");
				}
				_PaddingAndTimestamp = value;
			}
		}

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

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

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

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

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

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

		public List<RemainingPayloadSubField> RemainingPayloadList => _RemainingPayloadList;
	}

	public class RemainingPayloadSubField
	{
		private uint _PayloadByte;

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			PayloadByte = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(PayloadByte, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 255u;

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

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new DebugCxmLogInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 255u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			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.VsEventType)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.MciMwsMsgPayloadLength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MciMwsMsgHeader = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PaddingAndTimestamp = bitBuilder.RemoveBitsAsUint(24);
		UserFields.Byte9 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Byte10 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Byte11 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Byte12 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MciMwsGpmType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MciMwsAgentType = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.RemainingPayloadList.Count; i++)
		{
			UserFields.RemainingPayloadList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.VsEventType, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.MciMwsMsgPayloadLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.MciMwsMsgHeader, 8);
		bitBuilder.AddBitsAsUint(UserFields.PaddingAndTimestamp, 24);
		bitBuilder.AddBitsAsUint(UserFields.Byte9, 8);
		bitBuilder.AddBitsAsUint(UserFields.Byte10, 8);
		bitBuilder.AddBitsAsUint(UserFields.Byte11, 8);
		bitBuilder.AddBitsAsUint(UserFields.Byte12, 8);
		bitBuilder.AddBitsAsUint(UserFields.MciMwsGpmType, 8);
		bitBuilder.AddBitsAsUint(UserFields.MciMwsAgentType, 8);
		for (int i = 0; i < UserFields.RemainingPayloadList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.RemainingPayloadList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
