using Khd.Core.EntityFramework;
using Khd.Core.Wpf.Form;
using Masuit.Tools.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Input;

namespace Khd.Core.Wpf
{
    /// <summary>
    /// LoginPage.xaml 的交互逻辑
    /// </summary>
    public partial class LoginPage : Window
    {
        public static IHost _host;
        public LoginPage()
        {
            InitializeComponent();
            this.Hide();
            // 通过进程名称查找进程  
            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 mainWindow = new FormBoardT(_host);
                mainWindow.Show();
            }
            else
            {
                MessageBox.Show("系统重复开启,请先关闭之前窗口!");
                mutex.ReleaseMutex(); // 释放 Mutex
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //FormBoard mainWindow = new FormBoard();
                //mainWindow.Show();
            }
            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)
        {
            //using var s
        }
    }
}
