using MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Work29.ViewModels
{
    public class RegWindowViewModel : BaseViewModel
    {
        private User _user;

        private RelayCommand _createUser;

        public RelayCommand CreateUser
        {
            get
            {
                return _createUser ??
                    (_createUser = new RelayCommand(x =>
                    {
                        string stroka = "";
                        User user = User;
                        if ((User.Surname != stroka) && (User.Name != stroka)
                            && (User.Patronimyc != stroka) && (User.Login != stroka)
                            && (User.Password != stroka) && (User.Phone != stroka))
                        {
                            Work29Context context = new Work29Context();
                            context.Users.Add(user);
                            context.SaveChanges();
                            user = new User();
                            MainWindow mainWindow = new MainWindow();
                            mainWindow.Show();
                            foreach (var window in App.Current.Windows)
                            {
                                if (window is RegWindow regWindow)
                                {
                                    regWindow.Close();
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Все поля должны быть заполнены!");
                        }
                    }
                    ));
            }
        }

        public User User
        {
            get => _user;
            set
            {
                _user = value;
                OnPropertyChanged();
            }
        }

        public RegWindowViewModel()
        {
            Work29Context context = new Work29Context();
            _user = new User();
        }
    }
}
