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

        public ObservableCollection<ISServiceRef.EmployeeDto> Employees { get; set; }

        public ICommand ShowEmployeeDetailCommand { get; set; }

        public ICommand DeleteEmployeeCommand { get; set; }
        public ICommand ShowAddNewEmployeeCommand { get; set; }

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

        public EmployeesViewModel(bool selectOnly = 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;

            _usersClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _usersClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _usersClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            _selectOnly = selectOnly;
            Task.Run(async () => await LoadEmployeesAsync());
            ShowEmployeeDetailCommand = new AsyncCommand<EmployeeDto>(ShowEmployeeDetailAsync);
            DeleteEmployeeCommand = new AsyncCommand<EmployeeDto>(DeleteEmployeeAsync);
            ShowAddNewEmployeeCommand = new AsyncCommand(ShowAddNewEmployeeAsync);

            if (_selectOnly)
            {
                AddButtonVisible = Visibility.Hidden;
                DeleteButtonVisible = Visibility.Hidden;
            }
        }


        private async Task LoadEmployeesAsync(FilterDto filterDto = null)
        {
            var employees = await _client.GetEmployeesAsync(filterDto);
            Employees = new ObservableCollection<EmployeeDto>(employees);
        }

        private async Task ShowEmployeeDetailAsync(EmployeeDto selectedEmployee)
        {
            if (selectedEmployee == null) return;

            if (_selectOnly)
            {
                Messenger.Default.Send<NotificationMessage<EmployeeDto>>(new NotificationMessage<EmployeeDto>(selectedEmployee, "Employee selected"));
                Messenger.Default.Send<NotificationMessage>(new NotificationMessage("HostWindowClose"));
            }
            else
            {
                var employeeDetailView = new EmployeeDetailView(selectedEmployee);
                employeeDetailView.ShowDialog();
                await LoadEmployeesAsync();
            }
        }

        private async Task ShowAddNewEmployeeAsync(object param = null)
        {
            var newEmployee = new EmployeeDto()
            {
                DateOfBirth = DateTime.Now,
                DateJoined = DateTime.Now
            };
            var employeeDetailView = new EmployeeDetailView(newEmployee);
            employeeDetailView.ShowDialog();
            await LoadEmployeesAsync();
        }

        private async Task DeleteEmployeeAsync(EmployeeDto employeeDto)
        {
            if (employeeDto == null) return;

            var result = Xceed.Wpf.Toolkit.MessageBox.Show(string.Format(Properties.Resources.DeleteEmployeePrompt, $"{employeeDto.FirstName} {employeeDto.LastName}"), string.Empty, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

            if (result == MessageBoxResult.Yes)
            {
                await _usersClient.DeleteUserAsync(employeeDto.Id);
                await _client.DeleteEmployeeAsync(employeeDto.Id);
                await LoadEmployeesAsync();
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.EmployeeDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        EmployeeServiceClient _client = new EmployeeServiceClient();
        UserServiceClient _usersClient = new UserServiceClient();
        private bool _selectOnly = false;

    }
}
