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

namespace QCRPG.TSL.BT.Packets;

public class PastDebugEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubOpcode = 154u;

		private const uint _Opcode = 28u;

		private const uint _Length = 35u;

		private uint _SeviceData;

		private uint _Sid;

		private uint _AddrType;

		private uint _Phy;

		private uint _Pad;

		private List<uint> _SyncinfoList = new List<uint>(new uint[18]);

		private uint _Conneventcounter;

		public uint SubOpcode => 154u;

		public uint Opcode => 28u;

		public uint Length => 35u;

		public uint SeviceData
		{
			get
			{
				return _SeviceData;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for SeviceData is 0xFFFF");
				}
				_SeviceData = value;
			}
		}

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

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

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

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

		public List<uint> SyncinfoList => _SyncinfoList;

		public uint Conneventcounter
		{
			get
			{
				return _Conneventcounter;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Conneventcounter is 0xFFFF");
				}
				_Conneventcounter = value;
			}
		}
	}

	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 PastDebugEventInfo(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.SubOpcode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Opcode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Length)
		{
			return false;
		}
		UserFields.SeviceData = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Sid = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AddrType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Phy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pad = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SyncinfoList.Count; i++)
		{
			UserFields.SyncinfoList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.Conneventcounter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Opcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Length, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SeviceData, 16);
		bitBuilder.AddBitsAsUint(UserFields.Sid, 8);
		bitBuilder.AddBitsAsUint(UserFields.AddrType, 8);
		bitBuilder.AddBitsAsUint(UserFields.Phy, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pad, 8);
		for (int i = 0; i < UserFields.SyncinfoList.Count; i++)
		{
			if (UserFields.SyncinfoList[i] > 255)
			{
				throw new Exception("UserFields.SyncinfoList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.SyncinfoList[i], 8);
		}
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Conneventcounter, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
