using System.Diagnostics;
using Luski;
using Luski.Classes;
using Luski.GUI;
using OpenTK.Windowing.Common.Input;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Updater;
using Image = OpenTK.Windowing.Common.Input.Image;

try
{
    Globals.Settings = Globals.GetSettings(Path.Combine(Globals.LuskiPath, "Settings.json"), SettingsContext.Default.Settings);
    foreach (ExperimentInfo le in LuskiExperiments.LuskiExperimentsList)
    {
        Globals.RegisterExperiment(le);
    }
    
    ServerList serverlist = Globals.GetSettings(Path.Combine(Globals.LuskiPath, "Servers.json"), ServerListContext.Default.ServerList);
    Globals.ServerList = serverlist;
    string themes = Path.Combine(Globals.LuskiPath, "Themes");
    if (!Directory.Exists(themes)) Directory.CreateDirectory(themes);
    DirectoryInfo di = new DirectoryInfo(themes);

    foreach (DirectoryInfo Theme in di.GetDirectories())
    {
        try
        {
            LuskiThemes.LoadThemeFromDir(Theme.FullName);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
    foreach (FileInfo Theme in di.GetFiles())
    {
        try
        {
            if (Theme.FullName.ToLower().EndsWith(".zip")) LuskiThemes.LoadThemeFromDir(Theme.FullName);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
    if (serverlist.Servers.Length > 0)
    {
        foreach (ServerInfo server in serverlist.Servers)
        {
            switch (server.Main)
            {
                case false:
                   // if (server.Domain == "jt.luskiserver.jacobtech.com") continue;
                    Globals.ServersLoading.Add(Task.Run(async () =>
                    {
                        return await Globals.Luski.TryGetPublicServer(out _, server.Domain, server.Version,
                            server.Secure, server.PreGenEncryption, server.ShowMessage);
                    }));
                    break;
                case true:
                    if (LuskiExperiments.MainServers.EnableMainServers.IsEnabled()) Globals.Luski.GetMainServer(server.Domain, server.Version);
                    break;
            }
        }
    }
    
    Globals.UpdaterSettings = Globals.GetSettings(Path.Combine(Globals.LuskiPath, "UpdaterSettings.json"), UpdaterSettingsContext.Default.UpdaterSettings);
    Image<Rgba32> Logo = SixLabors.ImageSharp.Image.Load<Rgba32>(Globals.GetResource("Textures.Luski.png"));
    Logo.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> m);
    byte[] pixels = new byte[4 * Logo.Width * Logo.Height];
    Logo.CopyPixelDataTo(pixels);
    Logo.Dispose();
   
    Globals.Icon = new WindowIcon(new Image(Logo.Width, Logo.Height, pixels));

    if (args is not null && args.Length > 0)
    {
        if (args.Contains(Handler.UpdateArg))
        {
            string tm = args[Array.IndexOf(args, Handler.UpdateArg) + 1].Remove(0, 1);
            tm = tm.Remove(tm.Length - 1, 1);
            Console.WriteLine(tm);
            Handler.StartUpdate(Globals.Icon,
                AppDomain.CurrentDomain.FriendlyName,
                "Luski",
                tm,
                Path.Combine(Globals.LuskiPath, "UpdaterSettings.json"),
                Globals.UpdaterSettings.Platform, SelfContained: Globals.UpdaterSettings.SelfContained);
            return;
        }
        if (args.Contains(Handler.PostUpdateArg))
        {
            string tm = args[Array.IndexOf(args, Handler.PostUpdateArg) + 1].Remove(0, 1);
            tm = tm.Remove(tm.Length - 1, 1);
            Handler.PostUpdate(tm);
            return;
        }
    }
    
   
    
    MainScreenWindow.Settings.Icon = Globals.Icon;
    Globals.ms = new MainScreenWindow();
    Globals.ms.CustomF11 = false;
    //Globals.ms.DrawFrame();
    pixels = Array.Empty<byte>();
    Globals.ms.StartRender();
    Globals.ms.Dispose();
    
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

if (Globals.Download)
{
    Console.WriteLine("Update Starting");
    Handler.PreUpdate(AppDomain.CurrentDomain.FriendlyName);
    
}