using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace mateo
{
    public partial class First : Window
    {
        public First()
        {
            InitializeComponent();
            vvod_login.Focus();
        }

        #region Функции для тестов
        public bool Vhod(string login, string password)
        {
            bool tf = false;

            vvod_login.Text = login;
            vvod_pass.Password = password;
            if (vvod_login.Text == "" || vvod_pass.Password == "") MessageBox.Show("Пустые поля", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
            else
            {
                try
                {
                    //пользователь
                    if (DB.GetContext().Users.FirstOrDefault(x => x.Login == vvod_login.Text && x.Password == vvod_pass.Password) != null)
                    {
                        DB.Iduser = DB.GetContext().Users.FirstOrDefault(x => x.Login == vvod_login.Text && x.Password == vvod_pass.Password).IDUsers;

                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        this.Close();

                        tf = true;
                    }

                    //администратор
                    else if (DB.GetContext().Employees.FirstOrDefault(x => x.Login == vvod_login.Text && x.Password == vvod_pass.Password) != null)
                    {
                        DB.Idemployees = DB.GetContext().Employees.FirstOrDefault(x => x.Login == vvod_login.Text && x.Password == vvod_pass.Password).IDEmployees;
                        if (DB.GetContext().Employees.Where(x => x.IDEmployees == DB.Idemployees).FirstOrDefault().FKStatusEmployees != 2)
                        {
                            SysAdmin sysAdmin = new SysAdmin();
                            sysAdmin.Show();
                            this.Close();
                            tf = true;
                        }
                        else
                        {
                            MessageBox.Show("Вы были уволены", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                            vvod_login.Text = "";
                            vvod_pass.Password = "";
                        }
                    }

                    else
                    {
                        MessageBox.Show("Такого пользователя не сущ-ет", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                        vvod_login.Text = "";
                        vvod_pass.Password = "";
                        vvod_login.Focus();
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return tf;
        }

        public bool Registr(string lastname, string firstname, string middlename, string login, string password)
        {
            bool tf = false;

            vvod_LNAME.Text = lastname;
            vvod_FNAME.Text = firstname;
            vvod_MNAME.Text = middlename;
            vvod_login_reg.Text = login;
            vvod_pass_reg.Password = password;

            if (vvod_LNAME.Text == "" || vvod_FNAME.Text == "" || vvod_login_reg.Text == "" || vvod_pass_reg.Password == "") MessageBox.Show("Пустые поля!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
            else
            {
                try
                {
                    //проверяем есть ли пользователь с указанным логином в системе //проверяем в пользователях и админах
                    if (DB.GetContext().Users.FirstOrDefault(x => x.Login == vvod_login_reg.Text) == null && DB.GetContext().Employees.FirstOrDefault(x => x.Login == vvod_login_reg.Text) == null)
                    {

                        //подбираем номер карты которого нет в бд
                        string numberCard;
                        do
                        {
                            Random random = new Random();
                            int card1 = random.Next(1000, 9999);
                            int card2 = random.Next(1000, 9999);
                            int card3 = random.Next(1000, 9999);
                            int card4 = random.Next(1000, 9999);
                            numberCard = card1.ToString() + " " + card2.ToString() + " " + card3.ToString() + " " + card4.ToString();
                        } while (DB.GetContext().Card.FirstOrDefault(x => x.NumberCard == numberCard) != null);


                        DB.GetContext().Card.Add(new Card
                        {
                            NumberCard = numberCard
                        });
                        DB.GetContext().SaveChanges();

                        DB.Idcard = DB.GetContext().Card.OrderByDescending(x => x.IDCard).FirstOrDefault().IDCard;

                        if (vvod_MNAME.Text == "")
                        {
                            vvod_MNAME.Text = null;
                        }

                        var card = DB.GetContext().Card.OrderByDescending(x => x.IDCard).FirstOrDefault();
                        DB.GetContext().Users.Add(new Users
                        {
                            LastName = vvod_LNAME.Text,
                            FirstName = vvod_FNAME.Text,
                            MiddleName = vvod_MNAME.Text,
                            Login = vvod_login_reg.Text,
                            Password = vvod_pass_reg.Password,
                            FKCard = card.IDCard
                        });
                        DB.GetContext().SaveChanges();
                        DB.Iduser = DB.GetContext().Users.OrderByDescending(x => x.IDUsers).FirstOrDefault().IDUsers;

                        MessageBox.Show("Пользователь " + vvod_LNAME.Text + " " + vvod_FNAME.Text + " " + " был добавлен\nДобро пожаловать! ", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        this.Close();
                        tf = true;
                    }
                    else
                    {
                        MessageBox.Show("Пользователь с таким логином уже есть!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                        vvod_login_reg.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return tf;
        }
        #endregion


        private void btn_vhod(object sender, RoutedEventArgs e)
        {
            Vhod(vvod_login.Text, vvod_pass.Password);
        }

        private void btn_registr(object sender, RoutedEventArgs e)
        {
            grid_registr.Visibility = Visibility.Visible;
            grid_vhod.Visibility = Visibility.Hidden;
            vvod_login_reg.Focus();
        }

        private void btn_regist2(object sender, RoutedEventArgs e)
        {
            Registr(vvod_LNAME.Text, vvod_FNAME.Text, vvod_MNAME.Text, vvod_login_reg.Text, vvod_pass_reg.Password);
        }

        #region логинпароль
        private void txtchangedLogin(object sender, TextChangedEventArgs e)
        {
            if (vvod_login.Text != "") txtlogin.Visibility = Visibility.Hidden;
            else if (vvod_login.Text == "") txtlogin.Visibility = Visibility.Visible;


            if (vvod_LNAME.Text != "") txtlastreg.Visibility = Visibility.Hidden;
            else if (vvod_LNAME.Text == "") txtlastreg.Visibility = Visibility.Visible;

            if (vvod_FNAME.Text != "") txtfirstreg.Visibility = Visibility.Hidden;
            else if (vvod_FNAME.Text == "") txtfirstreg.Visibility = Visibility.Visible;

            if (vvod_MNAME.Text != "") txtmiddlereg.Visibility = Visibility.Hidden;
            else if (vvod_MNAME.Text == "") txtmiddlereg.Visibility = Visibility.Visible;

            if (vvod_login_reg.Text != "") txtloginreg.Visibility = Visibility.Hidden;
            else if (vvod_login_reg.Text == "") txtloginreg.Visibility = Visibility.Visible;
        }

        private void passchangedPass(object sender, RoutedEventArgs e)
        {
            if (vvod_pass.Password != "") txtpass.Visibility = Visibility.Hidden;
            else if (vvod_pass.Password == "") txtpass.Visibility = Visibility.Visible;

            if (vvod_pass_reg.Password != "") txtpassreg.Visibility = Visibility.Hidden;
            else if (vvod_pass_reg.Password == "") txtpassreg.Visibility = Visibility.Visible;
        }

        private void txtPasswordLogin_PreviewTextInput(object sender, TextCompositionEventArgs e)//опред символы в пароле и логине
        {
            bool a = new Regex("[^A-Z]+").IsMatch(e.Text);
            bool b = new Regex("[^a-z]+").IsMatch(e.Text);
            bool c = new Regex("[^0-9]+").IsMatch(e.Text);
            bool d = new Regex("[^.]").IsMatch(e.Text);

            e.Handled = a && b && c && d;

        }

        private void txtFIO_PreviewTextInput(object sender, TextCompositionEventArgs r)
        {
            bool a = new Regex("[^А-Я]+").IsMatch(r.Text);
            bool b = new Regex("[^а-я]+").IsMatch(r.Text);
            r.Handled = a && b;
        }
        #endregion

        private void btnCancel(object sender, RoutedEventArgs e)
        {
            grid_registr.Visibility = Visibility.Hidden;
            vvod_login.Text = "";
            vvod_pass.Password = "";
            vvod_LNAME.Text = "";
            vvod_FNAME.Text = "";
            vvod_MNAME.Text = "";
            vvod_login_reg.Text = "";
            vvod_pass_reg.Password = "";
            grid_vhod.Visibility = Visibility.Visible;
        }

        private void btn_exit(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

    }
}
