using Backend.Common;
using Backend.Models;
using Backend.Services.Interface;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Mediator
{
    public class AddLigneCommandeHandler : IRequestHandler<AddLigneCommandeCommand, int>
    {
        public readonly ILigneCommandeService _LigneCommandeService;
        private readonly ILogger<AddLigneCommandeHandler> _log;
        public AddLigneCommandeHandler(ILogger<AddLigneCommandeHandler> log, ILigneCommandeService LigneCommandeService)
        {
            _log = log;
            _LigneCommandeService = LigneCommandeService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(AddLigneCommandeCommand request, CancellationToken cancellationToken)
        {
            _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - Command Handle of Type");
            if (_LigneCommandeService != null)
            {
                using Task<int> task = _LigneCommandeService.CreateOrUpdate(request);
                await task;
                if (task.IsCompleted && task.IsCompletedSuccessfully)
                {
                    return task.Result;
                }
                else
                {
                    _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_SERVICE_MSG_ERROR + ": Type"}");
                    throw new Exception(ErrorsConstants.C_SERVICE_MSG_ERROR + ": Type");
                }
            }
            else
            {
                _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG}");
                throw new ArgumentNullException(ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG);
            }
        }

       
    }
}
