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 ViolationNOT
{
    /// <summary>
    /// Interaction logic for RegistrationPage.xaml
    /// </summary>
    public partial class RegistrationPage : Page
    {
        public RegistrationPage()
        {
            InitializeComponent();
            DataContext = new User();
        }

        public bool Registration(string Password, string RepeatPassword)
        {
            User NewUser = DataContext as User;

            if (string.IsNullOrEmpty(NewUser.Login) || 
                string.IsNullOrEmpty(Password) ||
                string.IsNullOrEmpty(RepeatPassword) ||
                string.IsNullOrEmpty(NewUser.LastName) ||
                string.IsNullOrEmpty(NewUser.Name) ||
                string.IsNullOrEmpty(NewUser.Phone) ||
                string.IsNullOrEmpty(NewUser.Email) )
            {
                Helper.ErroreMessage("Поля не заполнены");
                return false;
            }
            if (Password.Length <4)
            {
                Helper.ErroreMessage("Пароль слишком короткий");
                return false;
            }
            if (Password != RepeatPassword)
            {
                Helper.ErroreMessage("Пароли не совпадают");
                return false;
            }
            User FindUser = DB.db.User.FirstOrDefault(x => x.Login == NewUser.Login);
            if (FindUser != null)
            {
                Helper.ErroreMessage("Логин занят");
                return false;
            }
            if (NewUser.LastName.All(char.IsLetter) == false ||
               NewUser.Name.All(char.IsLetter) == false ||
               NewUser.MiddleName.All(char.IsLetter) == false)
            {
                Helper.ErroreMessage("Не корректное ФИО");
                return false;
            }
            NewUser.Password = Password;
            NewUser.IDRole = 1;
            DB.db.User.Add(NewUser);
            DB.TrySave();
            Helper.InformMessage("Вы зарегестрированы");
            ManagerFrame.MainFrame.GoBack();
            return true;
        }

        private void BtnRegistration_Click(object sender, RoutedEventArgs e)
        {
            Registration(TxtPassword.Password, TxtRepeatPassword.Password);
        }
    }
}
