#if true
using System.Diagnostics;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.Desktop;
using SixLabors.ImageSharp.PixelFormats;
using System.Reflection;
using System.Text.Json;
using GraphicsManager;
using SixLabors.ImageSharp;
using Image = OpenTK.Windowing.Common.Input.Image;

namespace Updater;

public class StartGUI
{
    //internal static int Width = 694;
    //internal static int Height = 216;
    public static SixLabors.ImageSharp.Image<Rgba32>? Logo { get; private set; }
    static readonly NativeWindowSettings Settings = new()
    {
        Title = "Updater",
        WindowBorder = WindowBorder.Fixed,
        APIVersion = new Version(3, 2),
        StartFocused = true,
        Size = new(694, 105)
    };

    private static readonly GameWindowSettings GameWindowSettings = new()
    {
        RenderFrequency = 24,
        UpdateFrequency = 30
    };
    

    internal static NewUpdater? Up;

    public static bool close = false;

    /// <summary>
    /// <para>args[0] = Process</para>
    /// <para>args[1] = Remote Program dir</para>
    /// <para>args[2] = Remote Program Branch</para>
    /// <para>args[3] = Local Program Dir</para>
    /// <para>args[4] = exe file</para>
    /// </summary>
    /// <param name="args"></param>
    internal static int Main(Argumets args)
    {
        /*
        var mmm = Monitors.GetMonitors();
        if (mmm is not null && mmm.Any())
        {
            var mi = mmm.First();
            int tempw = (int)Math.Round(mi.HorizontalResolution * 0.2127604166666667);
            int temph = (int)Math.Round(mi.VerticalResolution * 0.1175925925925926);
            if (tempw < Width) tempw = Width;
            Width = tempw;
            if (temph < Height) temph = Height;
            Height = temph - 30;
           // Settings.CurrentMonitor = mi.Handle;
        }
        //Settings.Size = new Vector2i(Width, Height);
*/
        Image<Rgba32> Logo = SixLabors.ImageSharp.Image.Load<Rgba32>(Tools.GetResourceStream(Assembly.GetExecutingAssembly(), "Updater.Resource.Logo.png"));
        Logo.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> mm);
        byte[] pixels = new byte[4 * Logo.Width * Logo.Height];
        Logo.CopyPixelDataTo(pixels);
   
        Settings.Icon = new WindowIcon(new Image(Logo.Width, Logo.Height, pixels));
        
        (Up = new NewUpdater(Settings, GameWindowSettings, args)).Run();
        Up.Close();
        Up.Dispose();
        if (close)
        {
            if (args.Updater is not null)
            {
                Process p = new();
                p.StartInfo.FileName = Path.Combine(args.Updater, AppDomain.CurrentDomain.FriendlyName);
                if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsFreeBSD())
                {
                    Process m = Process.Start("/usr/bin/chmod", $"+x {Path.Combine(args.Updater, AppDomain.CurrentDomain.FriendlyName)}");
                    m.WaitForExit();
                }

                p.StartInfo.WorkingDirectory = args.Updater;
                p.StartInfo.CreateNoWindow = true;
                Program.rawargs[1] = AppDomain.CurrentDomain.BaseDirectory;
                p.StartInfo.Arguments = $"\"{string.Join("\" \"", Program.rawargs)}\"";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            
                p.Start();
                Console.WriteLine("Program done");
                return 0;
            }
            else
            {
                if (args.Loc is not null)
                {
                    FileInfo fi = new(args.Loc);
                    if (!fi.Directory!.Exists) Directory.CreateDirectory(fi.Directory.FullName);
                    UpdaterSettings temp;
                    if (!File.Exists(args.Loc))
                    {
                        File.WriteAllText(args.Loc, JsonSerializer.Serialize(new UpdaterSettings(), UpdaterSettingsContext.Default.UpdaterSettings));
                        temp = new();
                    }

                    try
                    {
                        UpdaterSettings? ss = JsonSerializer.Deserialize(File.ReadAllText(args.Loc), UpdaterSettingsContext.Default.UpdaterSettings);
                        if (ss is null)
                        {
                            ss = new();
                        }

                        temp = ss;
                    }
                    catch
                    {
                        temp = new();
                    }

                    temp.Updater = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName);
                    if (args.Platform is not null) temp.Platform = args.Platform!.ToLower();
                    temp.SelfContained = args.SeflContaind;
                    temp.Branch = args.Branch!.ToLower() switch
                    {
                        "dev" => Branch.Dev,
                        "beta" => Branch.Beta,
                        "master" or _ => Branch.Master
                    };
                    File.WriteAllText(args.Loc, JsonSerializer.Serialize(temp, UpdaterSettingsContext.Default.UpdaterSettings));
                }

                if (string.IsNullOrEmpty(args.DLL) || string.IsNullOrWhiteSpace(args.DLL))
                {
                    return 0;
                }
                if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsFreeBSD())
                {
                    Process m = Process.Start("/usr/bin/chmod", $"+x \"{Path.Combine(args.LocalDirectory, args.DLL)}\"");
                    m.WaitForExit();
                }

                string a = "";
                foreach (string arg in args.args!)
                {
                    a += arg + " ";
                }

                Process.Start(new ProcessStartInfo()
                {
                    FileName = Path.Combine(args.LocalDirectory, args.DLL),
                    Arguments = a,
                    WorkingDirectory = args.LocalDirectory,
                    CreateNoWindow = true,
                    UseShellExecute = true,
                    
                })!.WaitForExit();
            }
        }
        return 0;
    }
}
#endif