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

        public ObservableCollection<ISServiceRef.DepartmentDto> Departments { get; set; }

        public ICommand DeleteDepartmentCommand { get; set; }
        public ICommand ShowDepartmentDetailCommand { get; set; }
        public ICommand ShowAddNewDepartmentCommand { get; set; }

        public DepartmentsViewModel()
        {
            _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 LoadDepartmentsAsync());
            DeleteDepartmentCommand = new AsyncCommand<DepartmentDto>(DeleteDepartmentAsync);
            ShowDepartmentDetailCommand = new AsyncCommand<DepartmentDto>(ShowDepartmentDetailView);
            ShowAddNewDepartmentCommand = new AsyncCommand(ShowAddNewDepartmentView);
        }


        private async Task LoadDepartmentsAsync(FilterDto filterDto = null)
        {
            var departments = await _client.GetDepartmentsAsync(filterDto);
            Departments = new ObservableCollection<DepartmentDto>(departments);
        }

        private async Task ShowDepartmentDetailView(DepartmentDto department)
        {
            if (department == null) return;

            var departmentDetailWindow = new DepartmentDetailView(department);
            departmentDetailWindow.ShowDialog();
            await LoadDepartmentsAsync();
        }

        private async Task ShowAddNewDepartmentView(object param = null)
        {
            var departmentDetailWindow = new DepartmentDetailView(new DepartmentDto());
            departmentDetailWindow.ShowDialog();
            await LoadDepartmentsAsync();
        }


        private async Task DeleteDepartmentAsync(DepartmentDto departmentDto)
        {
            if (departmentDto == null) return;

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

            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    await _client.DeleteDepartmentAsync(departmentDto.Id);
                    await LoadDepartmentsAsync();
                    Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.DepartmentDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch(Exception ex)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.DepartmentDeleteFailed, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        DepartmentServiceClient _client = new DepartmentServiceClient();

    }
}
