using Hangfire;
using IRaCIS.Core.Application.MassTransit.Consumer;
using IRaCIS.Core.Domain.Share;
using MassTransit.Mediator;
using System.Globalization;

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, new RecurringJobOptions() { TimeZone = TimeZoneInfo.Local });
        }

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

        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,string defaultCulture="")
        {
            if (defaultCulture == "")
            {
                defaultCulture = CultureInfo.CurrentCulture.Name;
            }

            switch (businessScenario)
            {

                case EmailBusinessScenario.QCTask:

                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new ImageQCRecurringEvent() { TrialId = trialId,CultureInfoName= defaultCulture }, default), emailCron);
                    break;
                case EmailBusinessScenario.CRCToQCQuestion:
                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new CRCImageQuestionRecurringEvent() { TrialId = trialId, CultureInfoName = defaultCulture }, default), emailCron);
                    break;
                case EmailBusinessScenario.QCToCRCImageQuestion:
                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new QCImageQuestionRecurringEvent() { TrialId = trialId, CultureInfoName = defaultCulture }, default), emailCron);
                    break;
                //加急阅片 10分钟
                case EmailBusinessScenario.ExpeditedReading:
                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new UrgentIRUnReadTaskRecurringEvent() { TrialId = trialId, CultureInfoName = defaultCulture }, default), emailCron);
                    break;
                default:
                    break;
            }

        }

        public static void AddOrUpdateTimingCronJob(string jobId, EmailBusinessScenario businessScenario, string emailCron, string defaultCulture="")
        {
            if (defaultCulture == "")
            {
                defaultCulture = CultureInfo.CurrentCulture.Name;
            }

            switch (businessScenario)
            {

                case EmailBusinessScenario.GeneralTraining_ExpirationNotification:


                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new SystemDocumentErverDayEvent() {  CultureInfoName = defaultCulture }, default), emailCron);
                    break;
                case EmailBusinessScenario.TrialTraining_ExpirationNotification:

                    Console.WriteLine("更新项目到期job");
                    HangfireJobHelper.AddOrUpdateCronJob<IMediator>(jobId, t => t.Send(new TrialDocumentErverDayEvent() {  CultureInfoName = defaultCulture }, default), emailCron);
                    break;

                default:
                    break;
            }
        }
    }
}


