using System;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Hotel_Course_Project
{
    /// <summary>
    /// Логика взаимодействия для AuthorizationWindow.xaml
    /// </summary>
    public partial class AuthorizationWindow : Window
    {
        public AuthorizationWindow()
        {
            InitializeComponent();
        }

        private void AuthBtn_Click(object sender, RoutedEventArgs e)
        {

            if (Auth(Login.Text, Password.Password) == false)
            {
                MessageBox.Show("Ошибка авторизации");
                Password.Password = "";
            }
            else
            {
                string Pass = SomeMethods.Hash(Password.Password);
                var user = DataBase.db.Staff.SingleOrDefault(x => x.Login == Login.Text && x.Password == Pass);
                if (user != null && user.Id_PersStatus == 2)
                {
                    MessageBox.Show("Данный пользователь удалён из базы данных");
                }
                else
                {
                    SomeMethods.pass = Password.Password;
                    MainWindow mainWindow = new MainWindow(user);
                    mainWindow.Show();
                    Close();
                }
            }
        }

        public bool Auth(string login, string password)
        {
            string pass = SomeMethods.Hash(password);
            var user = DataBase.db.Staff.SingleOrDefault(x => x.Login == login && x.Password == pass);
            if (user != null && user.Id_PersStatus == 2)
            {
                return true;
            }
            else if (user != null && user.Id_PersStatus == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Login.Focus();
        }

        private void LostPass_Click(object sender, RoutedEventArgs e)
        {
            LoginRememberWindow LoginRemember = new LoginRememberWindow();
            LoginRemember.ShowDialog();
        }
    }
}
