using AutoUpdaterDotNET;
using DeviceRepair.Models;
using DeviceRepairAndOptimization;
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //Application.Run(new page_DriveMaintenance());

            #region 开个等待窗口,做初始化 主要功能为检测程序配置config中的数据库连接字符串 & 测试连接数据库
            if (args != null && args.Length > 0)
            {
                //UserInfoModel us1 = JsonConvert.DeserializeObject<UserInfoModel>("{\"AutoID\": 1,\"LoginCode\": \"myl\",\"PassWord\": \"Kanghui2\",\"RealName\": \"管理员\",\"Gender\": 1,\"Birthday\": \"/Date(1693471604770)/\",\"Phone\": \"13800000000\",\"RuleGroup\": 0}");
                //string aa = JWT.JsonWebToken.Encode(us1, GlobalInfo.SecureKey, JWT.JwtHashAlgorithm.HS256);
                AutoUpdater.AppTitle = "Device Repair And Optimization";
                var vUpdateDir = Path.Combine(Application.StartupPath, "Update");
                if (!Directory.Exists(vUpdateDir))
                {
                    Directory.CreateDirectory(vUpdateDir);
                }

                // 指定下载文件的存储目录
                AutoUpdater.DownloadPath = vUpdateDir;
                AutoUpdater.UpdateFormSize = new Size(800, 600);

                AutoUpdater.Synchronous = true;
                AutoUpdater.ShowSkipButton = false;
                AutoUpdater.ShowRemindLaterButton = false;
                AutoUpdater.Start($"http://{DeviceRepair.Utils.Config.Configurations.Properties.ServiceIP}/{DeviceRepair.Utils.Config.Configurations.Properties.ServiceName}/Update/DOAutoUpdater.xml");

                try
                {
                    string argsValue = string.Join("|", args);
                    string usValue = JWT.JsonWebToken.Decode(argsValue, DeviceRepair.Utils.Config.Configurations.Properties.SecureKey, true);
                    UserInfoModel us = JsonConvert.DeserializeObject<UserInfoModel>(usValue);


                    if (us != null)
                    {
                        GlobalInfo.token = argsValue;
                        GlobalInfo.CurrentUser = us;
                        Application.Run(new frm_Launch());
                        return;
                    }
                }
                catch (Exception ex)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误");
                    return;
                }
            }


            //// 创建并显示等待窗口
            //SplashScreenManager.ShowDefaultWaitForm("程序初始化", "请稍等...");
            //if (string.IsNullOrEmpty(GlobalInfo.connectionString))
            //{
            //    SplashScreenManager.Default.SetWaitFormDescription("出错:连接字符串错误...");
            //    return;
            //}

            ////测试数据库连接 - 判断字符串是否正确
            //SqlSugarClient db = GlobalInfo.dbClient;
            //try
            //{
            //    //通过获取表信息的方式测试连接
            //    db.DbMaintenance.GetTableInfoList();
            //    SplashScreenManager.Default.SetWaitFormDescription("成功");
            //}
            //catch (Exception ex)
            //{
            //    SplashScreenManager.Default.SetWaitFormDescription($"出错:{ex.Message}...");
            //    return;
            //}

            //// 关闭等待窗口
            //SplashScreenManager.CloseDefaultWaitForm();

            #endregion

            //Application.Run(new Pages.Maintain.pageMaintainEdit());

            frmLogin login = new frmLogin();
            if (login.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new frm_Launch());
            }
        }
    }
}