using Avalonia.Controls;
using Avalonia.Media;
using CheckArticlesForSpam.Models;
using CheckArticlesForSpam.Views;
using ChecksArticlesForSpam.ViewModels;
using Microsoft.EntityFrameworkCore;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;

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;
        ArticlesUserPageViewModel articlesUserPageViewModel;

        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 ArticlesUserPageViewModel ArticlesUserPageVM { get => articlesUserPageViewModel; set => articlesUserPageViewModel = 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)
            {
                if (currentUser.Articles.Count == 0)
                {
                    ListArticlesVM = new ListArticlesViewModel(new NotArticlesInDB(), currentUser.Username);
                }
                else
                {
                    ArticlesUserPageVM = new ArticlesUserPageViewModel(currentUser);
                    ListArticlesVM = new ListArticlesViewModel(new ArticlesUserPage(), currentUser.Username);
                }
                UC = new ListArticles();
            }
            else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
        }
        public void LogedToProgram(User? currentUser)
        {
            AuthorizationPageVM.Login = currentUser.Login;
            AuthorizationPageVM.Password = currentUser.Password;
            if (currentUser != null)
            {
                if (currentUser.Articles.Count == 0)
                {
                    ListArticlesVM = new ListArticlesViewModel(new NotArticlesInDB(), currentUser.Username);
                }
                else
                {
                    ArticlesUserPageVM = new ArticlesUserPageViewModel(currentUser);
                    ListArticlesVM = new ListArticlesViewModel(new ArticlesUserPage(), currentUser.Username);
                }
                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);
            }
        }
        public void OpenNewArticleDate()
        {
            User? currentUser = teamContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
            ArticleDataPageVM = new ArticleDataPageViewModel();
            ListArticlesVM = new ListArticlesViewModel(new ArticleDatePage(), currentUser.Username);
            UC = new ListArticles();
        }
        public void OpenArticle()
        {
            User? currentUser = teamContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
            if (ArticlesUserPageVM.ItemArticle == null)
            {
                ArticleDataPageVM = new ArticleDataPageViewModel(ArticlesUserPageVM.Article[0]);

            }
            else ArticleDataPageVM = new ArticleDataPageViewModel(ArticlesUserPageVM.ItemArticle);
            ListArticlesVM = new ListArticlesViewModel(new ArticleDatePage(), currentUser.Username);
            UC = new ListArticles();
        }
        public bool SaveArticle()
        {
            User? currentUser = teamContext.Users.Include(x => x.Articles).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
            Dictionary<string, Article> objectArticle = ArticleDataPageVM.GetDataArticle(currentUser);
            if (objectArticle.ContainsKey("3"))
            {
                Article currentArticle = objectArticle["3"];
                if (teamContext.Articles.FirstOrDefault(x => x.IdArticle == currentArticle.IdArticle) != null) teamContext.SaveChanges();
                else
                {
                    teamContext.Add(currentArticle);
                    teamContext.SaveChanges();
                }
                ArticleDataPageVM.Message = "Данные сохранены в базе";
                ArticleDataPageVM.ColorMessage = Brushes.Green;
                return true;
            }
            else if (objectArticle.ContainsKey("2"))
            {
                ArticleDataPageVM.Message = "Название статьи не может превышать длину в 100 символов";
                ArticleDataPageVM.ColorMessage = Brushes.Red;
                return false;
            }
            else if (objectArticle.ContainsKey("1"))
            {
                ArticleDataPageVM.Message = "Название статьи не может быть пустым";
                ArticleDataPageVM.ColorMessage = Brushes.Red;
                return false;
            }
            return false;
        }
        public void DeleteSrticleInBD()
        {
            if (ArticlesUserPageVM.ItemArticle == null)
            {
                return;
            }
            else
            {
                teamContext.Remove(ArticlesUserPageVM.ItemArticle);
                teamContext.SaveChanges();
                LogedToProgram();
            }
        }
        public void ShowHidePasswordAuthorization()
        {
            if(AuthorizationPageVM.PasswordChar == null)
            {
                AuthorizationPageVM.PasswordChar = '*';
            }
            else if(AuthorizationPageVM.PasswordChar != null)
            {
                AuthorizationPageVM.PasswordChar = null;
            }
        }
        public void ShowHidePasswordRegistration()
        {
            if (RegistrationPageVM.PasswordChar == null)
            {
                RegistrationPageVM.PasswordChar = '*';
            }
            else if (RegistrationPageVM.PasswordChar != null)
            {
                RegistrationPageVM.PasswordChar = null;
            }
        }
        public void ShowHidePasswordProfile()
        {
            if (ProfileUserVM.PasswordChar == null)
            {
                ProfileUserVM.PasswordChar = '*';
            }
            else if (ProfileUserVM.PasswordChar != null)
            {
                ProfileUserVM.PasswordChar = null;
            }
        }
        public void SendArticleForCheck()
        {
            ArticleDataPageVM.CheckArticleForSpam(teamContext);
        }
    }
}
