using Quasar.Server.Forms;
using Quasar.Server.Utilities;
using System;
using System.Net;
using System.Windows.Forms;

namespace Quasar.Server
{
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            try
            {
                DebugLogger.Log("Application starting");
                
                // enable TLS 1.2
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                DebugLogger.Log("TLS 1.2 enabled");

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                DebugLogger.Log("Visual styles enabled");

                // Initialize dark mode globally
                DarkModeHelper.Initialize();
                DebugLogger.Log("Dark mode helper initialized");
                
                // Apply dark mode to all forms automatically
                if (Models.Settings.DarkMode)
                {
                    DebugLogger.Log("Dark mode is enabled, starting timer");
                    // Use a timer to periodically check for new forms and apply dark mode
                    var darkModeTimer = new System.Windows.Forms.Timer();
                    darkModeTimer.Interval = 100;
                    darkModeTimer.Tick += (s, e) =>
                    {
                        foreach (Form form in Application.OpenForms)
                        {
                            if (form.Tag == null || !form.Tag.Equals("DarkModeApplied"))
                            {
                                Utilities.ThemeManager.ApplyTheme(form);
                                form.Tag = "DarkModeApplied";
                            }
                        }
                    };
                    darkModeTimer.Start();
                }

                // Check license before starting
                DebugLogger.Log("Checking license");
                if (!FrmLicense.CheckLicense())
                {
                    DebugLogger.Log("License check failed, showing license form");
                    using (var licenseForm = new FrmLicense())
                    {
                        if (licenseForm.ShowDialog() != DialogResult.OK)
                        {
                            DebugLogger.Log("License not activated, exiting");
                            return; // Exit if license not activated
                        }
                    }
                }

                DebugLogger.Log("Starting main form");
                Application.Run(new FrmMain());
                DebugLogger.Log("Application exited normally");
            }
            catch (Exception ex)
            {
                DebugLogger.LogException("Main", ex);
                MessageBox.Show($"Fatal error: {ex.Message}\n\nCheck QuasarDebug.txt on Desktop for details.", 
                    "Quasar Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
