using Kafe.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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



        private void ConfirmBtn_Click(object sender, RoutedEventArgs e)
        {
            var login = LoginTB.Text.Trim();
            var password = PasswordTB.Password.Trim();
            var abc = authorization(login,password);

            MessageBox.Show($"{abc.Item1}, {abc.Item2}");
        }


        private (int, int) authorization(string login, string password)
        {
            var user = Helper.GetContext().Employee;
            var role = 0;
            var id = 0;

            if (user.Select(i => i.Login).Contains(login)
                && user.Select(i => i.Password).Contains(password))
            {
                role = user.Where(i => i.Login == login && i.Password == password).Select(i => i.PositionID).FirstOrDefault();
                id = user.Where(i => i.Login == login && i.Password == password).Select(i => i.ID).FirstOrDefault();
            
                if (role == 1)
                {
                    (new AdminWindow()).Show();
                    this.Close();
                }
                else if (role == 2)
                {
                    (new WaiterWindow()).Show();
                    this.Close();
                }
                else if (role == 3)
                {
                    (new CookerWindow()).Show();
                    this.Close();
                }
            }
        
            else
            {
                MessageBox.Show("Пользователя с таким логином или паролем не существует");
            }
           return (id, role);
            }
        }

    }
