using System;

namespace QCRPG.Framework;

public class PacketData : IDeepCopyable<PacketData>
{
	public PacketDataType type;

	public bool suppressRawByteLogging;

	public bool skipReceiveEnqueue;

	public byte[] bytes;

	public PacketData(byte b)
	{
		bytes = new byte[1];
		bytes[0] = b;
	}

	public PacketData(byte[] bytes, bool performHexAsciiConversion = false)
	{
		if (bytes == null)
		{
			this.bytes = new byte[0];
			return;
		}
		if (performHexAsciiConversion)
		{
			FromHexAsciiByteArray(bytes);
			return;
		}
		this.bytes = new byte[bytes.Length];
		Array.Copy(bytes, this.bytes, bytes.Length);
	}

	public PacketData(byte[] bytes, int length, bool performHexAsciiConversion = false)
	{
		if (bytes == null)
		{
			this.bytes = new byte[0];
			return;
		}
		if (performHexAsciiConversion)
		{
			FromHexAsciiByteArray(bytes, length);
			return;
		}
		this.bytes = new byte[length];
		Array.Copy(bytes, this.bytes, length);
	}

	public PacketData(PacketDataType type, byte b)
	{
		this.type = type;
		bytes = new byte[1];
		bytes[0] = b;
	}

	public PacketData(PacketDataType type, byte[] bytes)
	{
		this.type = type;
		if (bytes == null)
		{
			this.bytes = new byte[0];
			return;
		}
		this.bytes = new byte[bytes.Length];
		Array.Copy(bytes, this.bytes, bytes.Length);
	}

	public PacketData(PacketDataType type, string s)
	{
		this.type = type;
		bytes = Conversions.StringToByteArray(s);
	}

	public PacketData(Exception ex)
	{
		type = PacketDataType.Exception;
		bytes = Conversions.ObjectToByteArray(ex);
	}

	public PacketData DeepCopy()
	{
		PacketData packetData = new PacketData(type, bytes);
		packetData.suppressRawByteLogging = suppressRawByteLogging;
		packetData.skipReceiveEnqueue = skipReceiveEnqueue;
		return packetData;
	}

	public byte[] ToHexAsciiByteArray()
	{
		return Conversions.ByteArrayToHexAsciiByteArray(bytes);
	}

	public void FromHexAsciiByteArray(byte[] hexAsciiByteArray)
	{
		if (hexAsciiByteArray == null)
		{
			bytes = new byte[0];
		}
		else
		{
			FromHexAsciiByteArray(hexAsciiByteArray, hexAsciiByteArray.Length);
		}
	}

	public void FromHexAsciiByteArray(byte[] hexAsciiByteArray, int length)
	{
		if (hexAsciiByteArray == null)
		{
			bytes = new byte[0];
		}
		else
		{
			bytes = Conversions.HexAsciiByteArrayToByteArray(hexAsciiByteArray, length);
		}
	}
}
