using System;
using System.Collections.Generic;
using System.Text;

namespace QCRPG.Framework;

public abstract class PacketInfoBase
{
	protected PacketBase packet;

	protected List<PacketFieldInfo> fieldInfoList = new List<PacketFieldInfo>();

	public List<PacketFieldInfo> FieldInfoList
	{
		get
		{
			List<PacketFieldInfo> list = new List<PacketFieldInfo>();
			foreach (PacketFieldInfo fieldInfo in fieldInfoList)
			{
				list.Add(fieldInfo.DeepCopy());
			}
			return list;
		}
	}

	public abstract string Name { get; }

	public abstract string DisplayGroup { get; }

	public PacketInfoBase(PacketBase packet)
	{
		this.packet = packet;
	}

	public abstract void UpdateFieldInfoFromPacket();

	public abstract void UpdatePacketFromFieldInfo(int index, string value);

	public virtual string ConvertFieldHexStringToDisplayedText(string fieldName, string hexString)
	{
		throw new Exception("No hex string to displayed text conversion for field " + fieldName);
	}

	public virtual string ConvertFieldDisplayedTextToHexString(string fieldName, string displayedText)
	{
		throw new Exception("No displayed text to hex string conversion for field " + fieldName);
	}

	protected string FormatValue(uint value, int lengthBits, bool includeSpaces = false)
	{
		int num = ((lengthBits % 8 == 0) ? (lengthBits / 8) : (lengthBits / 8 + 1));
		string text = value.ToString("X" + Convert.ToString(num * 2));
		if (text.Length > num * 2)
		{
			throw new Exception("Value exceeds number of bytes calculated");
		}
		if (includeSpaces)
		{
			for (int i = 1; i < num; i++)
			{
				text = text.Insert(text.Length - (i - 1) * 3 - 2, " ");
			}
		}
		return text;
	}

	protected string FormatValue(ulong value, int lengthBits, bool includeSpaces = false)
	{
		int num = ((lengthBits % 8 == 0) ? (lengthBits / 8) : (lengthBits / 8 + 1));
		string text = value.ToString("X" + Convert.ToString(num * 2));
		if (text.Length > num * 2)
		{
			throw new Exception("Value exceeds number of bytes calculated");
		}
		if (includeSpaces)
		{
			for (int i = 1; i < num; i++)
			{
				text = text.Insert(text.Length - (i - 1) * 3 - 2, " ");
			}
		}
		return text;
	}

	protected string FormatValue(List<uint> valueList, int lengthBits, bool includeSpaces)
	{
		StringBuilder stringBuilder = new StringBuilder();
		int num = 0;
		int num2 = ((lengthBits % 8 == 0) ? (lengthBits / 8) : (lengthBits / 8 + 1));
		for (int i = 0; i < num2 - valueList.Count; i++)
		{
			stringBuilder.Append("00");
		}
		foreach (uint value in valueList)
		{
			if (value > 255)
			{
				throw new Exception("Invalid value in valueList: 0x" + value.ToString("X"));
			}
			stringBuilder.Append(value.ToString("X2"));
			if (includeSpaces)
			{
				stringBuilder.Append(" ");
				num++;
			}
		}
		if (stringBuilder.Length != num2 * 2 + num)
		{
			throw new Exception("Value exceeds number of bytes calculated");
		}
		return stringBuilder.ToString().TrimEnd();
	}

	protected string FormatValue(List<uint> valueList, int startIndex, int lengthBits, bool includeSpaces)
	{
		StringBuilder stringBuilder = new StringBuilder();
		int num = 0;
		int num2 = ((lengthBits % 8 == 0) ? (lengthBits / 8) : (lengthBits / 8 + 1));
		int num3 = 0;
		int num4 = 0;
		foreach (uint value in valueList)
		{
			if (num3 < startIndex)
			{
				num3++;
				continue;
			}
			if (value > 255)
			{
				throw new Exception("Invalid value in valueList: 0x" + value.ToString("X"));
			}
			stringBuilder.Append(value.ToString("X2"));
			num4++;
			if (includeSpaces)
			{
				stringBuilder.Append(" ");
				num++;
			}
			num3++;
			if (num4 != num2)
			{
				continue;
			}
			break;
		}
		if (stringBuilder.Length != num2 * 2 + num)
		{
			throw new Exception("Value exceeds number of bytes calculated");
		}
		return stringBuilder.ToString().TrimEnd();
	}

	protected void FormatValueList(List<uint> valueList, int startIndex, int lengthBits, string valueString, bool littleEndian)
	{
		byte[] array = Conversions.HexStringToByteArray(valueString);
		int num = ((lengthBits % 8 == 0) ? (lengthBits / 8) : (lengthBits / 8 + 1));
		if (littleEndian)
		{
			int num2 = startIndex + num - 1;
			int num3 = 0;
			while (num3 < num)
			{
				valueList[num2] = array[num3];
				num3++;
				num2--;
			}
		}
		else
		{
			int num4 = startIndex;
			int num5 = 0;
			while (num5 < num)
			{
				valueList[num4] = array[num5];
				num5++;
				num4++;
			}
		}
	}

	protected string AddLeadingZerosAndCheckValueStringLength(string valueString, int lengthBytes)
	{
		while (valueString.StartsWith("0"))
		{
			valueString = valueString.Remove(0, 1);
		}
		if (valueString.Length % 2 == 1)
		{
			valueString = "0" + valueString;
		}
		if (valueString.Length > lengthBytes * 2)
		{
			throw new Exception("Value exceeds number of bytes specified");
		}
		while (valueString.Length < lengthBytes * 2)
		{
			valueString = "00" + valueString;
		}
		return valueString;
	}
}
