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.Input;
using WpfAsyncPack.Command;

namespace Client.ViewModel
{
    public class ProjectDetailViewModel : ViewModelBase
    {

        public ProjectDto Project { get; set; }
        public ICommand CancelCommand { get; set; }

        public ICommand SaveProjectCommand { get; set; }

        public ICommand SelectEmployeeCommand { get; set; }

        public IEnumerable<string> ProjectStatesLocalized { get; set; }
        public string SelectedProjectState { get; set; }
        public ProjectDetailViewModel(ProjectDto project)
        {
            _projectServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _projectServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _projectServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            Project = project;
            CancelCommand = new RelayCommand<IClosable>(CloseWindow);
            SaveProjectCommand = new AsyncCommand<IClosable>(SaveProjectAsync);
            SelectEmployeeCommand = new RelayCommand(ShowEmployeeSelectWindow);
            ProjectStatesLocalized = new List<string>() { Properties.Resources.ProjectStateActive, Properties.Resources.ProjectStateInPreparation, Properties.Resources.ProjectStateDone };
            switch(Project.ProjectState)
            {
                case ProjectState.Active:
                    SelectedProjectState = ProjectStatesLocalized.FirstOrDefault(x => x == Properties.Resources.ProjectStateActive);
                    break;
                case ProjectState.Done:
                    SelectedProjectState = ProjectStatesLocalized.FirstOrDefault(x => x == Properties.Resources.ProjectStateDone);
                    break;
                case ProjectState.InPreparation:
                    SelectedProjectState = ProjectStatesLocalized.FirstOrDefault(x => x == Properties.Resources.ProjectStateInPreparation);
                    break;
            }

            Messenger.Default.Register<NotificationMessage<EmployeeDto>>(this, SelectEmployee);
        }

        private void SelectEmployee(NotificationMessage<EmployeeDto> message)
        {
            Project.ResponsibleEmployee = message.Content;
            Project.ResponsibleEmployeeId = message.Content.Id;
        }

        private void CloseWindow(IClosable window)
        {
            window.Close();
        }

        private void ShowEmployeeSelectWindow()
        {
            var hostWindow = new HostWindow(new EmployeesView(true));
            hostWindow.ShowDialog();
        }



        private async Task SaveProjectAsync(IClosable window)
        {
            if (SelectedProjectState == Properties.Resources.ProjectStateActive)
                Project.ProjectState = ProjectState.Active;
            else if (SelectedProjectState == Properties.Resources.ProjectStateInPreparation)
                Project.ProjectState = ProjectState.InPreparation;
            else if (SelectedProjectState == Properties.Resources.ProjectStateDone)
                Project.ProjectState = ProjectState.Done;

            var project = await _projectServiceClient.GetProjectByIdAsync(Project.Id);

            if (project != null)
            {
                await _projectServiceClient.UpdateProjectAsync(Project);
            }
            else
            {
                await _projectServiceClient.CreateProjectAsync(Project);
            }

            window.Close();
        }

        ProjectServiceClient _projectServiceClient = new ProjectServiceClient();
    }
}
