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.ComponentModel;
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 EditRequestsViewModel : ViewModelBase
    {

        public ObservableCollection<RequestDto> Requests { get; set; }

        public ICommand ShowRequestDetailCommand { get; set; }

        public ICommand DeleteRequestCommand { get; set; }
        public ICommand ShowAddNewRequestCommand { get; set; }
        public ICommand SwitchViewCommand { get; set; }
        public string SwitchButtonText { get; set; }


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

            SwitchButtonText = Properties.Resources.ShowSentToMeRequestsButtonText;
            Task.Run(async () => await LoadRequestsAsync());
            ShowRequestDetailCommand = new AsyncCommand<RequestDto>(ShowRequestDetailAsync);
            DeleteRequestCommand = new AsyncCommand<RequestDto>(DeleteRequestAsync);
            ShowAddNewRequestCommand = new AsyncCommand(ShowAddNewAttendanceRecordAsync);
            SwitchViewCommand = new AsyncCommand(SwitchViewAsync);

        }

        private async Task LoadRequestsAsync()
        {
            var filterDto = SwitchButtonText == Properties.Resources.ShowSentToMeRequestsButtonText ? BuildFilter() : BuildFilter(true);
            var attendanceRecords = await _client.GetRequestsAsync(filterDto);
            Requests = new ObservableCollection<RequestDto>(attendanceRecords);
        }

        private async Task ShowRequestDetailAsync(RequestDto selectedRequest)
        {
            if (selectedRequest == null) return;

            var detailView = new RequestDetailView(selectedRequest);
            detailView.ShowDialog();
            await LoadRequestsAsync();
        }

        private async Task ShowAddNewAttendanceRecordAsync(object param = null)
        {
            var newRequest = new RequestDto()
            {
                TimeStamp = DateTime.Now,
                EmployeeId = UserManager.Instance.CurrentUser.Id
            };
            var detailView = new RequestDetailView(newRequest);
            detailView.ShowDialog();
            await LoadRequestsAsync();
        }

        private async Task SwitchViewAsync(object param = null)
        {
            if (SwitchButtonText == Properties.Resources.ShowSentToMeRequestsButtonText)
            {
                SwitchButtonText = Properties.Resources.ShowMyRequestsButtonText;
            }
            else
            {
                SwitchButtonText = Properties.Resources.ShowSentToMeRequestsButtonText;
            }

            await LoadRequestsAsync();
        }

        private async Task DeleteRequestAsync(RequestDto request)
        {
            if (request == null) return;

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

            if (result == MessageBoxResult.Yes)
            {
                await _client.DeleteRequestAsync(request.Id);
                await LoadRequestsAsync();
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.RequestDeleted, string.Empty, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private FilterDto BuildFilter(bool showSentToUser = false)
        {
            string columnName = nameof(RequestDto.EmployeeId);

            if (showSentToUser)
                columnName = nameof(RequestDto.SupervisorId);

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

            return filter;
        }

        RequestServiceClient _client = new RequestServiceClient();

    }
}
