using System.Collections.Generic;
using System.IO;

namespace Crysome.Common.Network.Packets.Client;

public class GetProcessListResponsePacket : IPacket
{
	public List<ProcessListEntry> Processes { get; set; } = new List<ProcessListEntry>();

	public void Write(BinaryWriter w)
	{
		w.Write(Processes?.Count ?? 0);
		if (Processes == null)
		{
			return;
		}
		foreach (ProcessListEntry process in Processes)
		{
			w.Write(process.Name ?? "");
			w.Write(process.Pid);
			w.Write((process.IconData != null) ? process.IconData.Length : 0);
			if (process.IconData != null && process.IconData.Length != 0)
			{
				w.Write(process.IconData);
			}
		}
	}

	public void Read(BinaryReader r)
	{
		Processes = new List<ProcessListEntry>();
		int num = r.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			string name = r.ReadString();
			int pid = r.ReadInt32();
			int num2 = r.ReadInt32();
			byte[] iconData = ((num2 > 0) ? r.ReadBytes(num2) : new byte[0]);
			Processes.Add(new ProcessListEntry(name, pid, iconData));
		}
	}
}
