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

        public AttendanceRecordDto AttendanceRecord { get; set; }
        public ICommand CancelCommand { get; set; }

        public ICommand SaveAttendanceCommand { get; set; }

        public IEnumerable<AttendanceEventTypeDto> AttendanceEventTypes { get; set; }

        public AttendanceDetailViewModel(AttendanceRecordDto attendanceRecord)
        {
            _attendanceEventTypeServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _attendanceEventTypeServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _attendanceEventTypeServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

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


            AttendanceRecord = attendanceRecord;
            CancelCommand = new RelayCommand<IClosable>(CloseWindow);
            SaveAttendanceCommand = new AsyncCommand<IClosable>(SaveAttendanceRecordAsync);

            Task.Run(async () =>
            {
                AttendanceEventTypes = await _attendanceEventTypeServiceClient.GetAttendanceEventTypesAsync(null);

                AttendanceRecord.AttendanceEventType = AttendanceRecord.AttendanceEventType ?? AttendanceEventTypes.FirstOrDefault();
                AttendanceRecord.AttendanceEventTypeId = AttendanceRecord.AttendanceEventType.Id;
            });
        }

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

        private async Task SaveAttendanceRecordAsync(IClosable window)
        {
            var project = await _attendanceRecordServiceClient.GetAttendanceRecordByIdAsync(AttendanceRecord.Id);

            if (project != null)
            {
                await _attendanceRecordServiceClient.UpdateAttendanceRecordAsync(AttendanceRecord);
            }
            else
            {
                await _attendanceRecordServiceClient.CreateAttendanceRecordAsync(AttendanceRecord);
            }

            window.Close();
        }

        AttendanceRecordServiceClient _attendanceRecordServiceClient = new AttendanceRecordServiceClient();
        AttendanceEventTypeServiceClient _attendanceEventTypeServiceClient = new AttendanceEventTypeServiceClient();
    }
}
