using Avalonia.Controls;
using CheckArticlesForSpam.Models;
using CheckArticlesForSpam.Views;
using ChecksArticlesForSpam.ViewModels;
using Microsoft.EntityFrameworkCore;
using ReactiveUI;
using System.Linq;

namespace CheckArticlesForSpam.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        MlgTeamContext teamContext = new MlgTeamContext();
        UserControl uc = new AuthorizationPage();
        AuthorizationPageViewModel authorizationPageViewModel = new AuthorizationPageViewModel();
        RegistrationPageViewModel registrationPageViewModel;
        ArticleDataPageViewModel articleDataPageViewModel = new ArticleDataPageViewModel();
        ListArticlesViewModel listArticlesViewModel;
        ProfileUserViewModel profileUserViewModel;

        public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
        public AuthorizationPageViewModel AuthorizationPageVM { get => authorizationPageViewModel; set => authorizationPageViewModel = value; }
        public RegistrationPageViewModel RegistrationPageVM { get => registrationPageViewModel; set => registrationPageViewModel = value; }
        public ArticleDataPageViewModel ArticleDataPageVM { get => articleDataPageViewModel; set => articleDataPageViewModel = value; }
        public ListArticlesViewModel ListArticlesVM { get => listArticlesViewModel; set => listArticlesViewModel = value; }
        public ProfileUserViewModel ProfileUserVM { get => profileUserViewModel; set => profileUserViewModel = value; }

        public void LogedToProgram()
        {
            User? currentUser = teamContext.Users.Include(x => x.Articles).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
            if (currentUser != null)
            {
                UC = new ListArticles();
            }
            else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
        }
        public void LogedToProgram(User? currentUser)
        {
            AuthorizationPageVM.Login = currentUser.Login;
            AuthorizationPageVM.Password = currentUser.Password;
            if (currentUser != null)
            {
                UC = new ListArticles();
            }
            else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
        }
        public void ToAuthorizathionPage()
        {
            UC = new AuthorizationPage();
        }
        public void RegistrationInDB()
        {
            User? currentUser = registrationPageViewModel.AddUserInDataBase();
            if (currentUser != null)
            {
                LogedToProgram(currentUser);
            }
        }
        public void ToRegistrationPage()
        {
            registrationPageViewModel = new RegistrationPageViewModel(teamContext);
            UC = authorizationPageViewModel.LoadPageRegistration();
        }
        public void EditProfile()
        {
            User? currentUser = teamContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
            ProfileUserVM = new ProfileUserViewModel(currentUser, teamContext);
            UC = new ProfileUser();
        }
        public void BackToListNotChanges()
        {
            LogedToProgram();
        }
        public void SaveChangeUserData()
        {
            User currentUser = ProfileUserVM.SaveChanges();
            if (currentUser != null)
            {
                LogedToProgram(currentUser);
            }
        }
    }
}
