using Hotline.BatchTask;
using Hotline.BatchTask.Notifications;
using Quartz;
using Hotline.Share.Enums.BatchTask;
using Microsoft.Extensions.DependencyInjection;
using Hotline.EventBus;
using Hotline.Application.OrderApp.OrderDelayApp;
using Hotline.Application.OrderApp.OrderVisitApp;
using Microsoft.AspNetCore.Http;

namespace Hotline.Application.Jobs
{
    public class ApptaskJob : IJob, IDisposable
    {
        private readonly IApptaskDomainService _apptaskDomainService;
        private readonly IServiceProvider _serviceProvider;
        private readonly Publisher _publisher;

        public ApptaskJob(
            IApptaskDomainService apptaskDomainService,
            IServiceProvider serviceProvider,
            Publisher publisher)
        {
            _apptaskDomainService = apptaskDomainService;
            _serviceProvider = serviceProvider;
            _publisher = publisher;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            //Console.WriteLine($"执行ApptaskJob: {DateTime.Now}");
            var task = await _apptaskDomainService.GetWaitingTaskAsync(context.CancellationToken);
            if (task is null) return;
            var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
            httpContextAccessor.HttpContext ??= new DefaultHttpContext();
            ApptaskExecuteResult? result = null;
            switch (task.TaskType)
            {
                case ETaskType.OrderDelay:
                    var delayExecutor = _serviceProvider.GetService<IApptaskExecutor<OrderDelayReviewWithSessionRequest>>();
                    result = await _apptaskDomainService.ExecuteAsync(delayExecutor, task, context.CancellationToken);
                    break;
                case ETaskType.OrderScreen:
                    break;
                case ETaskType.VoiceVisit:
                    var vvExecutor = _serviceProvider.GetService<IApptaskExecutor<VoiceVisitWithSessionRequest>>();
                    result = await _apptaskDomainService.ExecuteAsync(vvExecutor, task, context.CancellationToken);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (result is not null)
            {
                if (result.IsSuccess)
                {
                    //todo pub single complete suc
                    await _publisher.PublishAsync(new ApptaskSuccessNotify(task), PublishStrategy.Async, context.CancellationToken);

                    //todo check if task is all complete
                    //pub all complete
                    var isCompleted = await _apptaskDomainService.IsCompletedAsync(task.ApptaskId, context.CancellationToken);
                    if (isCompleted)
                        await _publisher.PublishAsync(new ApptaskCompletedNotify(task), PublishStrategy.Async, context.CancellationToken);
                }
                else
                {
                    //todo pub single complete fail
                    await _publisher.PublishAsync(new ApptaskFailNotify(task), PublishStrategy.Async, context.CancellationToken);
                }
            }
        }

        /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}