using MVVM;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Work29
{
    public class MainWindowViewModel : BaseViewModel
    {
        private ObservableCollection<User> _users;

        private User _user;

        private RelayCommand _autoReady;

        private RelayCommand _regNew;

        public RelayCommand AutoReady
        {
            get
            {
                return _autoReady ??
                    (_autoReady = new RelayCommand(x =>
                    {
                        if (x is PasswordBox password)
                        {
                            Work29Context wpfContext = new Work29Context();
                            User user = Users.FirstOrDefault(p => p.Login == User.Login && p.Password == password.Password);
                            if (user != null)
                            {
                                User.a_user = user;
                                UserWindow userwindow = new UserWindow();
                                userwindow.Show();
                                foreach (var window in App.Current.Windows)
                                {
                                    if (window is MainWindow mainWindow)
                                    {
                                        mainWindow.Close();
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Ошибка авторизации!!!");
                            }
                        }
                    }
                    ));
            }
        }

        public RelayCommand RegNew
        {
            get
            {
                return _regNew ??
                    (_regNew = new RelayCommand(x =>
                    {
                        RegWindow userWindow = new RegWindow();
                        userWindow.Show();
                        foreach (var window in App.Current.Windows)
                        {
                            if (window is MainWindow mainWindow)
                            {
                                mainWindow.Close();
                            }
                        }
                    }
                    ));
            }
        }

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

        public ObservableCollection<User> Users
        {
            get => _users;
            set
            {
                _users = value;
                OnPropertyChanged();
            }
        }

        public MainWindowViewModel()
        {
            Work29Context context = new Work29Context();
            _user = new User();
            _users = new ObservableCollection<User>(context.Users);
        }
    }
}
