﻿using System.Collections.Generic;
using System.Windows.Input;
using ArMail.Models.Database;
using ArMail.Models.Database.Tables;
using ArMail.Models.Helpers;
using ArMail.ViewModels.Windows;
using ArMail.Views.UserControls;

namespace ArMail.ViewModels.UserControls
{
    public class MailCategoriesViewModel : NotifyObject
    {
        #region .ctor

        public MailCategoriesViewModel(MainViewModel mainViewModel)
        {
            MainViewModel = mainViewModel;
            WindowHelper = new WindowHelper();
        }

        #endregion .ctor

        #region Fields

        private readonly DbQueries _dbQueries = new DbQueries();


        #endregion Fields

        #region Properties

        public MainViewModel MainViewModel { get; set; }
        public WindowHelper WindowHelper { get; set; }  

        private List<MailCategoriesTable> _categories;
        public List<MailCategoriesTable> Categories
        {
            get => _categories;
            set
            {
                if (_categories == value) return;
                _categories = value;
                OnPropertyChanged(() => Categories);
            }
        }

        private MailCategoriesTable _selectedCategory;
        public MailCategoriesTable SelectedCategory
        {
            get => _selectedCategory;
            set
            {
                if (_selectedCategory == value) return;
                _selectedCategory = value;
                OnPropertyChanged(() => SelectedCategory);
            }
        }

        #endregion Properties

        #region Methods

        public void AddCategory(string categoryName)
        {
            _dbQueries.AddCategory(categoryName);
            Categories = _dbQueries.GetCategories(StaticData.Instance.UserId);
        }

        #endregion Methods

        #region Commands

        #region SelectedCategoryChanged

        RelayCommand _selectedCategoryChangedCommand;
        public ICommand SelectedCategoryChangedCommand => _selectedCategoryChangedCommand ?? (_selectedCategoryChangedCommand = new RelayCommand(SelectedCategoryChangedCommandExecute, SelectedCategoryChangedCommandCanExecute));

        void SelectedCategoryChangedCommandExecute(object param)
        {
            MainViewModel.MailListViewModel.MailList = _dbQueries.GetMailList(SelectedCategory.Id);
        }

        bool SelectedCategoryChangedCommandCanExecute(object param)
        {
            return true;
        }

        #endregion

        #region SendMail

        RelayCommand _sendMailCommand;
        public ICommand SendMailCommand => _sendMailCommand ?? (_sendMailCommand = new RelayCommand(SendMailCommandExecute, SendMailCommandCanExecute));

        void SendMailCommandExecute(object param)
        {
            var sendMessageDialog = new SendMessageView();
            sendMessageDialog.DataContext = new SendMessagesViewModel(this, sendMessageDialog);
            WindowHelper.ReleaseDialogWindow("Send Messages", 610, 460, sendMessageDialog);
        }

        bool SendMailCommandCanExecute(object param)
        {
            return SelectedCategory != null && (long)param == SelectedCategory.Id;
        }

        #endregion

        #endregion Commands
    }
}
