using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Rkis29.Model;
using Rkis29.View;
using System.Windows.Controls;
using System.Windows;

namespace Rkis29.ViewModel
{
    public class VWRegistr : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }

        private string _tbSupname; //текст бокс Фамилии
        public string TbSurname
        {
            get { return _tbSupname; }
            set { _tbSupname = value; OnPropertyChanged(); }
        }

        private string _tbName; //текс бокс Имени
        public string TbName
        {
            get { return _tbName; }
            set { _tbName = value; OnPropertyChanged(); }
        }

        private string _tbPatronomic; //текст бокс Отчества
        public string TbPatronomic
        {
            get { return _tbPatronomic; }
            set { _tbPatronomic = value; OnPropertyChanged(); }
        }

        private string _tbNumber; //текст бокс Номер телефона
        public string TbPhoneName
        {
            get { return _tbNumber; }
            set { _tbNumber = value; OnPropertyChanged(); }
        }

        private string _tbLogin; //текст бокс логина
        public string TbLogin
        {
            get { return _tbLogin; }
            set { _tbLogin = value; OnPropertyChanged(); }
        }

        private string _tbPassword; //текст бокс пароля
        public string TbPassword
        {
            get { return _tbPassword; }
            set { _tbPassword = value; OnPropertyChanged(); }
        }

        private RelayCommand _go_RegistrBt;
        public RelayCommand Go_RegistrBt
        {
            get
            {
                return _go_RegistrBt ?? new RelayCommand(obj =>
                {
                    if (_tbSupname != null && _tbName != null && TbPatronomic != null && _tbNumber != null && _tbLogin != null && _tbPassword != null)
                    {
                        var _d = ModelPublic.GetContext().Person.Where(w => w.Logins == _tbLogin).FirstOrDefault(); //сравнение логинов для авторизации
                        if (_d != null)
                        {
                            MessageBox.Show("Придумайте другой логин");
                        }
                        else
                        {
                            //сохранение результатов
                            ModelPublic.GetContext().Person.Add(new Person() { SurnameP = _tbSupname, NameP = _tbName, PatronomycP = TbPatronomic, PhoneNumber = _tbNumber, Logins = _tbLogin, Passwords = _tbPassword });
                            ModelPublic.GetContext().SaveChanges();
                            OnPropertyChanged();

                            MainWindow mainWindow = new MainWindow();
                            mainWindow.Show();
                            Window window = (Window)obj;
                            window.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Заполните все поля");
                    }
                });
            }
        }
    }
}
