using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
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 Penis
{
    /// <summary>
    /// Логика взаимодействия для Reg.xaml
    /// </summary>
    public partial class Reg : Page
    {
        public Reg()
        {
            InitializeComponent();
        }
        public void Go1(object sender, RoutedEventArgs e)
        {
            MainFrame.Name1.Navigate(new Page1());
        }
        public void GoReg(object sender, RoutedEventArgs e)
        {
            if (!NaPerekrestkeNeGonit(Login.Text))
            { MessageBox.Show("Логин занят"); return; }
            if (!Tamoshna(Pass.Password))
            { MessageBox.Show("Пароль не соотвествует требованиям "); return; }



            string RegPass = Pass.Password;
            using (SHA256 hash = SHA256.Create())
            {
                RegPass = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(RegPass))).Replace("-", "");
            }
            int Pol;
            if (M.IsChecked == true) { Pol = 1; } else { Pol = 2; }
            Client client = new Client()
            {
                Login = Login.Text,
                Password = RegPass,
                Name = Name.Text,
                Surname = Surname.Text,
                Patronymic = Patronymic.Text,
                BirthDate = DataR.SelectedDate,
                id_Gender = Pol,
                id_Role = 2
            };
            using (Entities SSS = new Entities())
            {
                SSS.Client.Add(client);
                SSS.SaveChanges();
            }
            MessageBox.Show("Сохранено");
        }
        public bool NaPerekrestkeNeGonit(string Login)
        {
            using (Entities SSS = new Entities())
            {
                foreach (Client client in SSS.Client)
                {
                    if (client.Login == Login)
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        public bool Tamoshna(string Password)
        {
            Regex Proverka = new Regex("(?=.*[A-Z])(?=.*[a-z]){3,}(?=.*[0-9]){2,}(?=.*[!@#$%^&&*()_])[A-Za-z0-9!@#$%^&*()_]{8,}");
            if (Proverka.IsMatch(Password)) { return true; }else { return false; }
        }

    }
}
