using Client.Helpers;
using Client.ISServiceRef;
using Client.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
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 PositionsViewModel : ViewModelBase
    {

        public ObservableCollection<EmployeePositionDto> Positions { get; set; }

        public ICommand DeletePositionCommand { get; set; }
        public ICommand ShowPositionDetailCommand { get; set; }
        public ICommand ShowAddNewPositionCommand { get; set; }

        public PositionsViewModel()
        {
            _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;

            Task.Run(async () => await LoadPositionsAsync());
            DeletePositionCommand = new AsyncCommand<EmployeePositionDto>(DeletePositionAsync);
            ShowPositionDetailCommand = new AsyncCommand<EmployeePositionDto>(ShowPositionDetailView);
            ShowAddNewPositionCommand = new AsyncCommand(ShowAddNewDepartmentView);
        }


        private async Task LoadPositionsAsync(FilterDto filterDto = null)
        {
            var departments = await _client.GetEmployeePositionsAsync(filterDto);
            Positions = new ObservableCollection<EmployeePositionDto>(departments);
        }

        private async Task ShowPositionDetailView(EmployeePositionDto position)
        {
            if (position == null) return;

            var departmentDetailWindow = new PositionDetailView(position);
            departmentDetailWindow.ShowDialog();
            await LoadPositionsAsync();
        }

        private async Task ShowAddNewDepartmentView(object param = null)
        {
            var departmentDetailWindow = new PositionDetailView(new EmployeePositionDto());
            departmentDetailWindow.ShowDialog();
            await LoadPositionsAsync();
        }


        private async Task DeletePositionAsync(EmployeePositionDto position)
        {
            if (position == null) return;

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

            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    await _client.DeleteEmployeePositionAsync(position.Id);
                    await LoadPositionsAsync();
                    Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.PositionDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch(Exception ex)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.PositionDeleteFailed, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        EmployeePositionServiceClient _client = new EmployeePositionServiceClient();

    }
}
