using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Config;
using SlnMesnac.WPF.Attribute;
using System;
using System.Windows;

namespace SlnMesnac.WPF.ViewModel.Login
{
    [RegisterAsSingleton]
    public partial class LoginViewModel: ObservableObject
    {
        private readonly AppConfig _appConfig;
        [ObservableProperty]
        public string systemTitle = string.Empty;

        [ObservableProperty]
        public string userName = string.Empty;

        public LoginViewModel(AppConfig appConfig)
        {
            _appConfig = appConfig;
            SystemTitle = "系统测试";
        }


        [RequirePermission("Login")]
        public bool Login(string userName,string password)
        {
            bool res = true;
            try
            {
                //res = _usersBusiness.Verify(userName, password);
                if (res)
                {
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        MainWindow mainWindow = App.ServiceProvider.GetService<MainWindow>();
                        App.Current.MainWindow = mainWindow;
                        mainWindow.Show();
                        
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"登录失败:{ex.Message}");
            }
            return res;
        }
    }
}
