using AccompanyingAssistant;
using AudioWallpaper.IPC;
using AudioWallpaper.OtherWallpaper;
using AudioWallpaper.WebWallpaper;
using System.Runtime.InteropServices;

namespace AudioWallpaper {
    internal static class Program {
        [DllImport("user32.dll")]
        private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);

        [DllImport("user32.dll")]
        private static extern bool FlashWindowEx(int pfwi);
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        /// 
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();//释放关联的控制台窗口
        private static SSOManager _ssoManager = new SSOManager();
        //public static SSOManager SSOM { get; private set; };
        public static SSOManager SSOM {
            get {
                return _ssoManager;
            }
        }
        public static WindowManager WindowManager { get; private set; } = new WindowManager();

        [STAThread]
        static void Main(String[] args) {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            bool createdNew;
            bool DoesTheGuardianExist = false;
            // 创建一个唯一的互斥体名称，确保它是唯一的
            using (Mutex mutex = new Mutex(true, "{C8F8E5D9-6F05-4F92-BE10-DF7A4F5F97FB}", out createdNew)) {
#if DEBUG
                createdNew = true;
#endif
                if (createdNew) {
                    //判断启动参数是否满足条件
                    //if (args.Length < 1) {
                    //    mutex.ReleaseMutex();
                    //    return;
                    //}
                    //if (!args[0].Equals("ManagerStart")) {
                    //    mutex.ReleaseMutex();
                    //    return;
                    //}
                    //if (!args[1].Equals("1")) {
                    //    mutex.ReleaseMutex();
                    //    return;
                    //}
#if !DEBUG
                   
                    Mutex Dmutex = new Mutex(true, "{C9F9E5D9-6F05-4F92-BE10-DF7A4F5F97FB}", out DoesTheGuardianExist);
                    if (DoesTheGuardianExist) {
                        Dmutex.ReleaseMutex();
                        Dmutex.Dispose();
                        mutex.ReleaseMutex();
                        return;
                    }
                    Dmutex.Dispose();
#endif
                    // 开启控制台
                    AllocConsole();
                    // 如果互斥体创建成功，说明当前没有其他实例在运行
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    ApplicationConfiguration.Initialize();
                    Task.Run(() => SSOM.Initialize()); // Run SSOManager initialization in a separate task
                    //创建窗体管理器
                    FormManager formManager = new FormManager();
                    //创建陪伴助手管理器
                    AccAssManage accAssManage = new AccAssManage();

                    IPCClientExample.RunAsync(); // 启动IPC客户端示例


                    //WebWallpaperManager webWallpaperManager = new WebWallpaperManager("https://blog.ysit.top");
                    //OtherWallpaperManager otherWallpaperManager = new OtherWallpaperManager();

                    Application.Run();
                    //释放控制台
                    FreeConsole();
                    mutex.ReleaseMutex(); // 释放互斥体
                }
            }
        }

    }
}