using System.Diagnostics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.Desktop;

namespace Updater;

public static class Handler
{
    public static string JT
    {
        get
        {
            string tmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JacobTech");
            if (OperatingSystem.IsLinux())
            {
                tmp = Path.Combine(Environment.GetEnvironmentVariable("HOME")!, ".config/");
                tmp += "JacobTech";
            }
            return tmp;
        }
    }
    public static string Slash
    {
        get
        {
            if (OperatingSystem.IsWindows()) return "\\";
            else return "/";
        }
    }
    
    
    public const string Domain = "www.JacobTech.com";
    
    internal static Argumets Argumets { get; } = new();
    
    internal static readonly NativeWindowSettings Settings = new()
    {
        Title = "Updater",
        WindowBorder = WindowBorder.Fixed,
        APIVersion = new Version(3, 2),
        StartFocused = true,
        Size = new(694, 105)
    };

    internal static readonly GameWindowSettings GameWindowSettings = new()
    {
        UpdateFrequency = 30
    };

    public const string UpdateArg = "--StartUpdate",
        PostUpdateArg = "--PostUpdate";
    
    public static void PreUpdate(string app)
    {
        string p = Path.Join(Path.GetTempPath(), Path.GetRandomFileName());
        if (!Directory.Exists(p)) Directory.CreateDirectory(p);
        
        void CoppyDir(string path, string dir)
        {
            DirectoryInfo DI = new(path);
            foreach (FileInfo file in DI.GetFiles())
            {
                file.CopyTo(Path.Join(dir, file.Name), true);
            }
            foreach (DirectoryInfo file in DI.GetDirectories())
            {
                Directory.CreateDirectory(Path.Join(dir, file.Name));
                CoppyDir(file.FullName, Path.Join(dir, file.Name));
            }
        }
        
        CoppyDir(Directory.GetCurrentDirectory(), p);
        if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsFreeBSD())
        {
            Process m = Process.Start("/usr/bin/chmod", $"+x \"{Path.Combine(p, app)}\"");
            m.WaitForExit();
        }

        Process pp = new Process()
        {
            StartInfo = new()
            {
                FileName = Path.Join(p, app),
                WorkingDirectory = p,
                Arguments = UpdateArg + $" '{Directory.GetCurrentDirectory()}'"
            }
        };
        pp.Start();
        pp.WaitForExit();
    }
    
    public static void StartUpdate(WindowIcon Icon, string App, string RemoteDirectory, string LocalDirectory, string ConfigPath, string Platform, string Branch = "main", bool SelfContained = false, string? ProgramVersion = null)
    {
        Argumets.Loc = ConfigPath;
        Argumets.RemoteDirectory = RemoteDirectory;
        Argumets.LocalDirectory = LocalDirectory;
        Argumets.DLL = App;
        Argumets.Branch = Branch;
        Argumets.Version = ProgramVersion;
        Argumets.SeflContaind = SelfContained;
        Argumets.Platform = Platform;

        //Settings.Icon = Icon;
        
        NewUpdater NU = new(Settings, GameWindowSettings, Argumets);
        NU.Run();
        NU.Dispose();
        if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsFreeBSD())
        {
            Process m = Process.Start("/usr/bin/chmod", $"+x \"{Path.Combine(Argumets.LocalDirectory, Argumets.DLL)}\"");
            m.WaitForExit();
        }
        Process.Start(new ProcessStartInfo()
        {
            FileName = Path.Combine(Argumets.LocalDirectory, Argumets.DLL),
            Arguments = PostUpdateArg + $" '{Directory.GetCurrentDirectory()}'",
            WorkingDirectory = Argumets.LocalDirectory,
            CreateNoWindow = true,
            UseShellExecute = true,
                    
        })!.WaitForExit();
    }
    
    public static void PostUpdate(string LocalDirectory)
    {
        if (Directory.Exists(LocalDirectory)) Directory.Delete(LocalDirectory, true);
    }
}