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

        public ObservableCollection<PersonalEventDto> PersonalEvents { get; set; }

        public ICommand ShowPersonalEventDetailCommand { get; set; }

        public ICommand DeletePersonalEventCommand { get; set; }
        public ICommand ShowAddNewPersonalEventCommand { get; set; }

        public PersonalEventsViewModel()
        {
            _personalEventServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _personalEventServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _personalEventServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            Task.Run(async () => await LoadPersonalEventsAsync());
            ShowPersonalEventDetailCommand = new AsyncCommand<PersonalEventDto>(ShowPersonalEventDetailAsync);
            DeletePersonalEventCommand = new AsyncCommand<PersonalEventDto>(DeletePersonalEventAsync);
            ShowAddNewPersonalEventCommand = new AsyncCommand(ShowAddNewPersonalEventAsync);
        }


        private async Task LoadPersonalEventsAsync(FilterDto filterDto = null)
        {
            var personalEvents = await _personalEventServiceClient.GetPersonalEventsAsync(filterDto);
            PersonalEvents = new ObservableCollection<PersonalEventDto>(personalEvents);
        }

        private async Task ShowPersonalEventDetailAsync(PersonalEventDto selectedPersonalEvent)
        {
            if (selectedPersonalEvent == null) return;

            var projectDetailView = new PersonalEventDetailView(selectedPersonalEvent);
            projectDetailView.ShowDialog();
            await LoadPersonalEventsAsync();
        }

        private async Task ShowAddNewPersonalEventAsync(object param = null)
        {
            var newPersonalEvent = new PersonalEventDto()
            {
                DateTime = DateTime.Now,
                EmployeeId = UserManager.Instance.CurrentUser.Id
            };
            var projectDetailView = new PersonalEventDetailView(newPersonalEvent);
            projectDetailView.ShowDialog();
            await LoadPersonalEventsAsync();
        }

        private async Task DeletePersonalEventAsync(PersonalEventDto personalEvent)
        {
            if (personalEvent == null) return;

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

            if (result == MessageBoxResult.Yes)
            {
                await _personalEventServiceClient.DeletePersonalEventAsync(personalEvent.Id);
                await LoadPersonalEventsAsync();
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.PersonalEventDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private FilterDto BuildFilter()
        {
            var filter = new FilterDto()
            {
                Conditions = new List<ConditionItemDto>()
                 {
                      new ConditionItemDto()
                      {
                           Column = nameof(PersonalEventDto.EmployeeId),
                           Type = ConditionTypeEnum.Equal,
                           Value = UserManager.Instance.CurrentUser.Id.ToString()
                      }
                 },
                Sorting = new List<SortingItemDto>()
                 {
                      new SortingItemDto()
                      {
                           Ascending = false,
                           Column = nameof(PersonalEventDto.DateTime),
                      }
                 }
            };

            return filter;
        }

        PersonalEventServiceClient _personalEventServiceClient = new PersonalEventServiceClient();

    }
}
