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.Data.SqlClient;
using System.Data;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

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

        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Inventory;Integrated Security=True");

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();
            }
            catch
            {
            }
        }

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

        public void vhod()
        {
            if (logintxt.Text == "" || passwordpsw.Password == "")
            {
                MessageBox.Show("Пустые поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE Login = '" + logintxt.Text + "' and Password = '" + passwordpsw.Password + "'", con);
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet);
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        string idrole = dataSet.Tables[0].Rows[0]["ID_Role"].ToString();
                        if (idrole.ToString() == "1")
                        {
                            con.Close();
                            Osnova osnova = new Osnova();
                            osnova.Show();
                            this.Close();
                        }
                        else
                        {
                            con.Close();
                            SystemAdminWindow system = new SystemAdminWindow();
                            system.Show();
                            this.Close();
                        }
                    }
                    else
                    {
                        con.Close();
                        logintxt.Text = "";
                        passwordpsw.Password = "";
                        MessageBox.Show("Неправильный логин/пароль!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    con.Close();
                }
            }
        }

        private void Vhod_Click(object sender, RoutedEventArgs e)
        {
            vhod();
        }

        private void Logintxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is TextBox textBox)
            {
                textBox.Text = new string
                    (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
            }
        }

        private void Passwordpsw_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void Passwordpsw_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);
            e.Handled = a && b && c;
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                vhod();
            }
        }
    }
}
