using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using FMSApp.Repositories;
using Quartz;

namespace FMSAdmin.QuartzSchedule.Jobs {
    public class WorkToOrderAndResultJob : IJob {
        private readonly ILogger<WorkToOrderAndResultJob> _logger;
        private readonly WorkRequestRepository _workRequestRepository;

        public WorkToOrderAndResultJob(ILogger<WorkToOrderAndResultJob> logger, WorkRequestRepository workRequestRepository) {
            _logger = logger;
            _workRequestRepository = workRequestRepository;
        }

        public Task Execute(IJobExecutionContext context) {
            // work to order
            try {
                _workRequestRepository.WorkToOrders();
            } catch (Exception ex) {
                _logger.LogError(ex, "");
            }

            // work to result
            try {
                _workRequestRepository.WorkToResults();
            } catch (Exception ex) {
                _logger.LogError(ex, "");
            }

            return Task.CompletedTask;
        }
    }
}