using System;
using System.Collections.Generic;

namespace QCRPG.Framework;

public abstract class PacketBase : IDeepCopyable<PacketBase>
{
	private uint flags;

	public uint Flags
	{
		get
		{
			return flags;
		}
		set
		{
			flags = value;
		}
	}

	public abstract PacketType Type { get; }

	public abstract Type[] ExpectedResponseTypes { get; }

	public virtual bool SuppressRawByteLogging => false;

	public abstract PacketInfoBase Info { get; }

	public PacketBase()
	{
		SetDefaultValues();
	}

	public virtual void SetDefaultValues()
	{
	}

	public bool FromTextString(string textString)
	{
		return PacketStringTranslator.StringToPacket(textString, this);
	}

	public bool FromByteArray(byte[] byteArray)
	{
		BitBuilder bitBuilder = new BitBuilder();
		foreach (byte b in byteArray)
		{
			bitBuilder.AddBitsAsByte(b);
		}
		return FromBitBuilder(ref bitBuilder);
	}

	public bool FromBitString(string bitString)
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsString(bitString);
		return FromBitBuilder(ref bitBuilder);
	}

	public bool FromBitBuilder(BitBuilder bitBuilder)
	{
		return FromBitBuilder(ref bitBuilder);
	}

	protected abstract bool FromBitBuilder(ref BitBuilder bitBuilder);

	public byte[] ToByteArray()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsString(ToBitString());
		return bitBuilder.ToByteArray();
	}

	public abstract string ToBitString();

	protected BitBuilder ToBitBuilder()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsString(ToBitString());
		return bitBuilder;
	}

	public virtual List<string> GetWarnings()
	{
		return new List<string>();
	}

	public PacketBase DeepCopy()
	{
		PacketBase packetBase = (PacketBase)Activator.CreateInstance(GetType());
		packetBase.FromBitString(ToBitString());
		packetBase.Flags = flags;
		return packetBase;
	}
}
