using Client.Helpers;
using Client.ISServiceRef;
using Client.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfAsyncPack.Command;

namespace Client.ViewModel
{
    public class ProjectsViewModel : ViewModelBase
    {

        public ObservableCollection<ProjectDto> Projects { get; set; }

        public ICommand ShowProjectDetailCommand { get; set; }

        public ICommand DeleteProjectCommand { get; set; }
        public ICommand ShowAddNewProjectCommand { get; set; }


        public Visibility AddButtonVisible { get; set; } = Visibility.Visible;
        public Visibility DeleteButtonVisible { get; set; } = Visibility.Visible;

        public ProjectsViewModel(bool isReadOnly = false)
        {
            _client.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _client.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            _isSelectOnly = isReadOnly;
            AddButtonVisible = _isSelectOnly ? Visibility.Hidden : Visibility.Visible;
            DeleteButtonVisible = _isSelectOnly ? Visibility.Hidden : Visibility.Visible;

            Task.Run(async () => await LoadProjectsAsync());
            ShowProjectDetailCommand = new AsyncCommand<ProjectDto>(ShowProjectDetailAsync);
            DeleteProjectCommand = new AsyncCommand<ProjectDto>(DeleteProjectAsync);
            ShowAddNewProjectCommand = new AsyncCommand(ShowAddNewProjectAsync);
        }


        private async Task LoadProjectsAsync(FilterDto filterDto = null)
        {
            var projects = await _client.GetProjectsAsync(filterDto);
            Projects = new ObservableCollection<ProjectDto>(projects);
        }

        private async Task ShowProjectDetailAsync(ProjectDto selectedProject)
        {
            if (selectedProject == null) return;

            if (_isSelectOnly)
            {
                Messenger.Default.Send<NotificationMessage<ProjectDto>>(new NotificationMessage<ProjectDto>(selectedProject, "Project selected"));
                Messenger.Default.Send<NotificationMessage>(new NotificationMessage("HostWindowClose"));
            }
            else
            {
                var projectDetailView = new ProjectDetailView(selectedProject);
                projectDetailView.ShowDialog();
                await LoadProjectsAsync();
            }

        }

        private async Task ShowAddNewProjectAsync(object param = null)
        {
            var newProject = new ProjectDto()
            {
                BeganOn = DateTime.Now,
            };
            var projectDetailView = new ProjectDetailView(newProject);
            projectDetailView.ShowDialog();
            await LoadProjectsAsync();
        }

        private async Task DeleteProjectAsync(ProjectDto projectDto)
        {
            if (projectDto == null) return;

            var result = Xceed.Wpf.Toolkit.MessageBox.Show(string.Format(Properties.Resources.DeleteProjectPrompt, projectDto.Name), string.Empty, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

            if (result == MessageBoxResult.Yes)
            {
                await _client.DeleteProjectAsync(projectDto.Id);
                await LoadProjectsAsync();
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.ProjectDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        ProjectServiceClient _client = new ProjectServiceClient();
        private bool _isSelectOnly = false;
    }
}
