using System;
using System.Collections.Generic;
using System.Linq;
using pr13.Models;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
using ReactiveUI;

namespace pr13.ViewModels
{
    public class AuthViewModel : ReactiveObject
    {
        public static PostgresContext DB = new PostgresContext();

        private string _login;
        private string _pass;

        public string Login
        {
            get => _login; set => this.RaiseAndSetIfChanged(ref _login, value);
        }

        public string Pass
        {
            get => _pass; set => this.RaiseAndSetIfChanged(ref _pass, value);
        }

        public int Auth()
        {
            if (Login != null && Pass != null)
            {
                User user = DB.Users.Where(x => x.Login == Login && x.Password == Pass).FirstOrDefault();
                if (user != null)
                {
                    return user.IdUser;
                }
                else
                {
                    var box = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Такого аккаунта не существует");
                    box.ShowAsync();
                    return 0;
                }
            }
            else
            {
                var box = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Поля не заполнены");
                box.ShowAsync();
                return 0;
            }
        }
    }
}