using Avalonia.Controls;
using pr13.Models;
using ReactiveUI;
using System.Linq;
using System.Net;

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

        UserControl us = new Auth();

        public UserControl US
        {
            get => us;
            set => this.RaiseAndSetIfChanged(ref us, value);
        }

        AuthViewModel authorizationvm = new AuthViewModel();
        PersonalCabinetViewModel personalpagevm;
        CoursesViewModel coursesvm = new CoursesViewModel();

        public AuthViewModel AuthorizationVM
        {
            get => authorizationvm;
            set => authorizationvm = value;
        }

        public PersonalCabinetViewModel PPVM
        {
            get => personalpagevm;
            set => personalpagevm = value;
        }

        public CoursesViewModel CourseVM
        {
            get => coursesvm;
            set => coursesvm = value;
        }

        public void GoAuth()
        {
            int index = AuthorizationVM.Auth();
            if (index != 0)
            {
                if (DB.Users.Where(x => x.IdUser == index).Select(x => x.IdRole).FirstOrDefault() == 2)
                {
                    PPVM = new PersonalCabinetViewModel(index);
                    US = new PersonalCabinet();
                }
                else
                {
                    US = new Courses();
                }
            }
        }
    }
}