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 RequestDetailViewModel : ViewModelBase
    {

        public RequestDto Request { get; set; }
        public ICommand CancelCommand { get; set; }

        public ICommand SaveRequestCommand { get; set; }

        public ICommand SelectSupervisorCommand { get; set; }

        public bool IsReadOnlyEmployee { get; set; } = false;
        public bool IsReadOnlySupervisor { get; set; } = false;
        public Visibility SelectEmployeeVisibility => IsReadOnlyEmployee ? Visibility.Hidden : Visibility.Visible;

        public IEnumerable<string> RequestStatesLocalized { get; set; }
        public string SelectedRequestState { get; set; }

        public RequestDetailViewModel(RequestDto request)
        {
            _requestServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _requestServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _requestServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            Request = request;
            CancelCommand = new RelayCommand<IClosable>(CloseWindow);
            SaveRequestCommand = new AsyncCommand<IClosable>(SaveRequestAsync);
            SelectSupervisorCommand = new RelayCommand(ShowEmployeeSelectWindow);

            if (Request.SupervisorId == UserManager.Instance.CurrentUser.Id)
                IsReadOnlyEmployee = true;
            else
                IsReadOnlySupervisor = true;

            RequestStatesLocalized = new List<string>() { Properties.Resources.RequestStateApproved, Properties.Resources.RequestStateDeclined, Properties.Resources.RequestStateWaiting };
            switch (Request.RequestState)
            {
                case RequestState.Approved:
                    SelectedRequestState = RequestStatesLocalized.FirstOrDefault(x => x == Properties.Resources.RequestStateApproved);
                    break;
                case RequestState.Declined:
                    SelectedRequestState = RequestStatesLocalized.FirstOrDefault(x => x == Properties.Resources.RequestStateDeclined);
                    break;
                case RequestState.Waiting:
                    SelectedRequestState = RequestStatesLocalized.FirstOrDefault(x => x == Properties.Resources.RequestStateWaiting);
                    break;
            }

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

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

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

        private void SelectSupervisor(NotificationMessage<EmployeeDto> message)
        {
            Request.Supervisor = message.Content;
            Request.SupervisorId = message.Content.Id;
        }

        private async Task SaveRequestAsync(IClosable window)
        {
            if ((Request.RequestState == RequestState.Approved || Request.RequestState == RequestState.Declined) && Request.SupervisorId != UserManager.Instance.CurrentUser.Id)
            {
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.CantEditRequest, string.Empty, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
            }

            if (SelectedRequestState == Properties.Resources.RequestStateApproved)
                Request.RequestState = RequestState.Approved;
            else if (SelectedRequestState == Properties.Resources.RequestStateDeclined)
                Request.RequestState = RequestState.Declined;
            else if (SelectedRequestState == Properties.Resources.RequestStateWaiting)
                Request.RequestState = RequestState.Waiting;

            var request = await _requestServiceClient.GetRequestByIdAsync(Request.Id);

            if (request != null)
            {
                await _requestServiceClient.UpdateRequestAsync(Request);
            }
            else
            {
                await _requestServiceClient.CreateRequestAsync(Request);
            }

            window.Close();
        }

        RequestServiceClient _requestServiceClient = new RequestServiceClient();
    }
}
