using System.Diagnostics;
using RobloxLegacy.Utilities;

namespace RobloxLegacy;

public class Installer
{
    private static readonly string InstallPath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        "Roblox", "Versions"
    );
    
    private static void SetShortcuts(string filePath)
    {
        // create a shortcut on the desktop
        Shortcut.Create(
            "Launch Roblox",
            filePath,
            "Updater and patcher for roblox on legacy operating systems",
            Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        );

        // create a shortcut in the start menu
        Shortcut.Create(
            "Roblox Legacy",
            filePath,
            "Updater and patcher for roblox on legacy operating systems",
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs")
        );
    }
    
    public static void DeployBootstrapper()
    {
        var bootstrapperPath = Process.GetCurrentProcess()?.MainModule?.FileName ?? throw new Exception("Could not find bootstrapper");
        var newExePath = Path.Combine(InstallPath, "RobloxLegacy.exe");
        if(bootstrapperPath == newExePath) // already installed
            return;
        Directory.CreateDirectory(InstallPath);
        File.Copy(bootstrapperPath, newExePath, true);
        SetShortcuts(newExePath);
    }
}