using System.IO;

namespace Crysome.Common.Network.Packets.Client;

public class GetDirectoryResponsePacket : IPacket
{
	public string Name { get; set; }

	public string Path { get; set; }

	public string[] Folders { get; set; }

	public string[] Files { get; set; }

	public long[] FileSizes { get; set; }

	public long[] FolderLastWriteUtcTicks { get; set; }

	public long[] FileLastWriteUtcTicks { get; set; }

	public byte[][] FolderIconPng { get; set; }

	public byte[][] FileIconPng { get; set; }

	public long RequestId { get; set; }

	public void Write(BinaryWriter w)
	{
		w.Write(Name ?? "");
		w.Write(Path ?? "");
		int num = ((Folders != null) ? Folders.Length : 0);
		w.Write(num);
		if (Folders != null)
		{
			for (int i = 0; i < num; i++)
			{
				w.Write(Folders[i] ?? "");
			}
		}
		int num2 = ((Files != null) ? Files.Length : 0);
		w.Write(num2);
		if (Files != null)
		{
			for (int j = 0; j < num2; j++)
			{
				w.Write(Files[j] ?? "");
			}
		}
		w.Write((FileSizes != null) ? FileSizes.Length : 0);
		if (FileSizes != null)
		{
			for (int k = 0; k < FileSizes.Length; k++)
			{
				w.Write(FileSizes[k]);
			}
		}
		for (int l = 0; l < num; l++)
		{
			long value = ((FolderLastWriteUtcTicks != null && l < FolderLastWriteUtcTicks.Length) ? FolderLastWriteUtcTicks[l] : 0);
			w.Write(value);
		}
		for (int m = 0; m < num2; m++)
		{
			long value2 = ((FileLastWriteUtcTicks != null && m < FileLastWriteUtcTicks.Length) ? FileLastWriteUtcTicks[m] : 0);
			w.Write(value2);
		}
		WritePngArray(w, FolderIconPng, num);
		WritePngArray(w, FileIconPng, num2);
		w.Write(RequestId);
	}

	private static void WritePngArray(BinaryWriter w, byte[][] arr, int count)
	{
		for (int i = 0; i < count; i++)
		{
			byte[] array = ((arr != null && i < arr.Length) ? arr[i] : null);
			int num = ((array != null) ? array.Length : 0);
			w.Write(num);
			if (num > 0)
			{
				w.Write(array);
			}
		}
	}

	private static byte[][] ReadPngArray(BinaryReader r, int count)
	{
		byte[][] array = new byte[count][];
		for (int i = 0; i < count; i++)
		{
			int num = r.ReadInt32();
			array[i] = ((num > 0) ? r.ReadBytes(num) : null);
		}
		return array;
	}

	public void Read(BinaryReader r)
	{
		Name = r.ReadString();
		Path = r.ReadString();
		int num = r.ReadInt32();
		Folders = new string[num];
		for (int i = 0; i < num; i++)
		{
			Folders[i] = r.ReadString();
		}
		int num2 = r.ReadInt32();
		Files = new string[num2];
		for (int j = 0; j < num2; j++)
		{
			Files[j] = r.ReadString();
		}
		int num3 = r.ReadInt32();
		FileSizes = new long[num3];
		for (int k = 0; k < num3; k++)
		{
			FileSizes[k] = r.ReadInt64();
		}
		FolderLastWriteUtcTicks = null;
		FileLastWriteUtcTicks = null;
		FolderIconPng = null;
		FileIconPng = null;
		RequestId = 0L;
		if (r.BaseStream.Position >= r.BaseStream.Length)
		{
			return;
		}
		FolderLastWriteUtcTicks = new long[num];
		for (int l = 0; l < num; l++)
		{
			FolderLastWriteUtcTicks[l] = r.ReadInt64();
		}
		if (r.BaseStream.Position >= r.BaseStream.Length)
		{
			return;
		}
		FileLastWriteUtcTicks = new long[num2];
		for (int m = 0; m < num2; m++)
		{
			FileLastWriteUtcTicks[m] = r.ReadInt64();
		}
		if (r.BaseStream.Position < r.BaseStream.Length)
		{
			FolderIconPng = ReadPngArray(r, num);
			FileIconPng = ReadPngArray(r, num2);
			if (r.BaseStream.Position < r.BaseStream.Length)
			{
				RequestId = r.ReadInt64();
			}
		}
	}
}
