using Khd.Core.Domain.Auth;
using Khd.Core.EntityFramework;
using Khd.Core.Wpf.Form;
using Khd.Core.Wpf.TaskForm;
using Masuit.Tools.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using MessageBox = HandyControl.Controls.MessageBox;
namespace Khd.Core.Wpf
{
    /// <summary>
    /// LoginPage.xaml 的交互逻辑
    /// </summary>
    public partial class LoginPage : Window
    {
        public static IHost _host;
        public LoginPage()
        {
            InitializeComponent();
         
            // 通过进程名称查找进程  
            bool ret;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, $"Khd.Core.Wpf", out ret);
            if (ret)
            {
                IHost host = CreateHostBuilder(null).Build();
                _host = host;
                FormBoardT.CheckVersion();
                if (!SystemData.NeedLogin)
                {
                    this.Hide();
                    FormBoardT mainWindow = new FormBoardT(_host);
                    mainWindow.Show();
                }
            }
            else
            {
                MessageBox.Show("系统重复开启,请先关闭之前窗口!");
                mutex.ReleaseMutex(); // 释放 Mutex
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!SystemData.NeedLogin)
                {
                    FormBoardT mainWindow = new FormBoardT(_host);
                    mainWindow.Show();
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
                LogManager.Error(ex);
            }
        }
        private void ShuBiaoShuangJi(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                txtUserNm.Text = "admin";
                txtPwd.Password = "123456";
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
                LogManager.Error(ex);
            }
        }
        static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
          .ConfigureServices((_, services) =>
          {
              services.AddDbContext<DefaultDbContext>(options =>
                  options.UseMySql(ConnectionStrings.ConnectionString, new MySqlServerVersion(new Version(8, 0, 31))));
          });

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var user = new
                {
                    username = txtUserNm.Text,
                    password = txtPwd.Password
                };
                string msg = HttpHelper.SendHttpsPostMessage("172.16.12.100", "prod-api/auth/pdaLogin", JsonConvert.SerializeObject(user));
                var message = JsonConvert.DeserializeObject<ReponseMessage<ToeknMessage>>(msg);
                if (message.code == "200")
                {
                    this.Hide();
                    string menuMsg = HttpHelper.HttpsGet("https://172.16.12.100/prod-api/system/menu/getWcsRouters", message.data.access_token);
                    var menu = JsonConvert.DeserializeObject<ReponseMessage<List<SysMenu>>>(menuMsg);
                    if (menu.code == "200")
                    {
                        List<string> list = menu.data.Select(t => t.MenuName).ToList();
                        //对list和SystemData.MenuList取交集
                        SystemData.MenuList = list.Intersect(SystemData.MenuList).ToList();
                    }
                    FormBoardT mainWindow = new FormBoardT(_host);
                    mainWindow.Show();
                }
                else
                {
                    MessageBox.Show(message.msg);
                }
            }
            catch
            {
                this.Hide();
                FormBoardT mainWindow = new FormBoardT(_host);
                mainWindow.Show();
            }
        }
        private class ReponseMessage<T>
        {
            public string code { get; set; }
            public string msg { get; set; }
            public T data { get; set; }
        }

        private class ToeknMessage
        {
            public string access_token { get; set; }
            public long expires_in { get; set; }
        }
    }
}
