using Hangfire;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Domain.Share;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Helper
{
    public static class HangfireJobHelper
    {



        //public static void AddOrUpdateCronJob(string jobId, string queueName, Expression<Action> methodCall, string cron)
        //{
        //    RecurringJob.AddOrUpdate(jobId, queueName, methodCall, cron);         
        //}



        //添加 或者更新定时任务  Id 要唯一标识一个定义任务
        public static void AddOrUpdateCronJob<T>(string jobId, Expression<Action<T>> methodCall, string cron, string queueName = "default")
        {
            RecurringJob.AddOrUpdate<T>(jobId, queueName, methodCall, cron);
        }

        public static void AddOrUpdateInitCronJob<T>(string jobId, Expression<Action<T>> methodCall, string cron)
        {
            RecurringJob.AddOrUpdate<T>(jobId, "sys_init", methodCall, cron);
        }

        public static void RemoveCronJob(string jobId)
        {
            RecurringJob.RemoveIfExists(jobId);

        }



        public static void ImmediatelyOnceOnlyJob(Expression<Action> methodCall)
        {
            BackgroundJob.Enqueue("immediately_once", methodCall);
        }

        public static void ImmediatelyOnceOnlyJob<T>(Expression<Action<T>> methodCall)
        {
            BackgroundJob.Enqueue<T>("immediately_once", methodCall);
        }

        public static void NotImmediatelyOnceOnlyJob(Expression<Action> methodCall, TimeSpan timeSpan)
        {
            BackgroundJob.Schedule("not_immediately_once", methodCall, timeSpan);
        }

        public static void NotImmediatelyOnceOnlyJob<T>(Expression<Action<T>> methodCall, TimeSpan timeSpan)
        {
            BackgroundJob.Schedule<T>("not_immediately_once", methodCall, timeSpan);
        }


        public static void AddOrUpdateTrialCronJob (string jobId, Guid trialId, EmailBusinessScenario businessScenario, string emailCron)
        {

            switch (businessScenario)
            {

                case EmailBusinessScenario.QCTask:
                    HangfireJobHelper.AddOrUpdateCronJob<IEmailSendService>(jobId, t => t.SendTrialImageQCTaskEmailAsync(trialId), emailCron);
                    break;
                case EmailBusinessScenario.QCQuestion:
                    HangfireJobHelper.AddOrUpdateCronJob<IEmailSendService>(jobId, t => t.SendTrialQCQuestionEmailAsync(trialId), emailCron);
                    break;
                case EmailBusinessScenario.ImageQuestion:
                    HangfireJobHelper.AddOrUpdateCronJob<IEmailSendService>(jobId, t => t.SendTrialImageQuestionAsync(trialId), emailCron);
                    break;

                default:
                    break;
            }

        }
    }
}
    

