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.Input;
using WpfAsyncPack.Command;

namespace Client.ViewModel
{
    public class PersonalEventDetailViewModel : ViewModelBase
    {

        public PersonalEventDto PersonalEvent { get; set; }
        public ICommand CancelCommand { get; set; }

        public ICommand SavePersonalEventCommand { get; set; }

        public IEnumerable<PersonalEventTypeDto> PersonalEventTypes { get; set; }

        public PersonalEventDetailViewModel(PersonalEventDto personalEvent)
        {
            _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;

            _personalEventTypeServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _personalEventTypeServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _personalEventTypeServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            PersonalEvent = personalEvent;
            CancelCommand = new RelayCommand<IClosable>(CloseWindow);
            SavePersonalEventCommand = new AsyncCommand<IClosable>(SavePersonalEventAsync);

            Task.Run(async () =>
            {
                PersonalEventTypes = await _personalEventTypeServiceClient.GetPersonalEventTypesAsync(null);
                if (PersonalEvent.PersonalEventType == null)
                {
                    PersonalEvent.PersonalEventType = PersonalEventTypes.FirstOrDefault();
                    PersonalEvent.PersonalEventTypeId = PersonalEventTypes.FirstOrDefault().Id;
                }

            });
        }

        private void CloseWindow(IClosable window)
        {
            window.Close();
        }

        private async Task SavePersonalEventAsync(IClosable window)
        {
            var personalEvent = await _personalEventServiceClient.GetPersonalEventByIdAsync(PersonalEvent.Id);

            if (personalEvent != null)
            {
                await _personalEventServiceClient.UpdatePersonalEventAsync(PersonalEvent);
            }
            else
            {
                await _personalEventServiceClient.CreatePersonalEventAsync(PersonalEvent);
            }

            window.Close();
        }

        PersonalEventServiceClient _personalEventServiceClient = new PersonalEventServiceClient();
        PersonalEventTypeServiceClient _personalEventTypeServiceClient = new PersonalEventTypeServiceClient();
    }
}
