using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using XKRS.Common.Model.Authority;
using XKRS.Common.Model.Helper;

namespace XKRS.UI.Main
{
    static class Program
    {
        static MainFrm mainFrm = null;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            AutoStart(true);
            if (AuthorityCheck.AuthorityCheckOperation())
            {
                MessageBox.Show("加密狗本地检测和远程检测失败！请本地插入加密狗或确认远程加密服务就绪。");
                //Environment:提供有关当前环境和平台的信息以及操作他们的方法
                //Exit:终止此进程，并将退出代码返回到操作系统
                Environment.Exit(1);
            }
            //3f0d870f-0fcc-4f0b-9836-123a679d8bc
            if (!HslCommunication.Authorization.SetAuthorizationCode("f562cc4c-4772-4b32-bdcd-f3e122c534e3"))
            {
                MessageBox.Show("软件授权失败，系统关闭！");
                Environment.Exit(1);
            }
            bool isAppRunning = false;
            //Mutex:可用于进程间同步的同步基元
            Mutex mutex = new Mutex(true, Process.GetCurrentProcess().ProcessName, out isAppRunning);
            if (!isAppRunning)
            {
                MessageBox.Show("程序已运行，不能再次打开！");
                Environment.Exit(1);
            }

            
            Application.SetCompatibleTextRenderingDefault(false);
            //如果没有配置文件，则初始化示例数据
            if (!SettingHelper.IsSettingFileValid())
            {
                SampleHelper.Init();
            }
            Application.EnableVisualStyles();
            //PriorityClass:获取或设置关联进程的总体优先级类别
            //ProcessPriorityClass：指定系统将与进程关联的优先级
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
            //UnhandledException：当某个异常未被捕获时出现
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += Application_ThreadException;

            mainFrm = new MainFrm();
            Application.Run(mainFrm);

            AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
            Application.ThreadException -= Application_ThreadException;
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }

        private static void AutoStart(bool isAuto = true)
        {
            if (isAuto == true)
            {
                RegistryKey R_local = Registry.CurrentUser;//包含有关当前用户首选项的信息
                //创建一个新子项或打开一个现有子项进行写访问
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.SetValue("ProjectName", Application.ExecutablePath);//获取启动应用程序的可执行文件的路径
                R_run.Close();
                R_local.Close();
            }
            else
            {
                RegistryKey R_local = Registry.CurrentUser;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.DeleteValue("ProjectName", false);
                R_run.Close();
                R_local.Close();
            }
        }
    }
}
