using ITNexusAgent;
using ITNexusAgent.Models;
using ITNexusAgent.UI;
using Newtonsoft.Json;
using System.ServiceProcess;

internal class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule!.FileName;

        if (args.Length > 0)
        {
            switch (args[0])
            {
                case "--notify":
                    RunNotification(args.Length > 1 ? args[1] : "");
                    return;

                case "--dashboard":
                    RunDashboard();
                    return;

                case "--install":
                    AgentService.MigrateFromOldAgent();
                    AgentService.Install(exePath);
                    return;

                case "--uninstall":
                    AgentService.Uninstall();
                    return;
            }
        }

        ServiceBase.Run(new AgentService());
    }

    static void RunNotification(string base64Json)
    {
        try
        {
            var json = Encoding.UTF8.GetString(Convert.FromBase64String(base64Json));
            var ann = JsonConvert.DeserializeObject<Announcement>(json);
            if (ann == null) return;
            var app = new System.Windows.Application();
            app.Run(new NotificationWindow(ann));
        }
        catch { }
    }

    static void RunDashboard()
    {
        var app = new System.Windows.Application();
        app.Run(new DashboardWindow());
    }
}
