using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace wispro.sp.api.Job
{
    public class QuartzUtil
    {
        private static ISchedulerFactory _schedulerFactory;
        private static IScheduler _scheduler;

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="type">类</param>
        /// <param name="jobKey">键</param>
        /// <param name="trigger">触发器</param>
        public static async Task Add(Type type, JobKey jobKey, ITrigger trigger = null)
        {
            await Init();
            //_scheduler = await _schedulerFactory.GetScheduler();

            //await _scheduler.Start();

            if (trigger == null)
            {
                trigger = TriggerBuilder.Create()
                    .WithIdentity("april.trigger")
                    .WithDescription("default")
                    .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionFireNow().WithRepeatCount(-1))
                    .Build();
            }
            var job = JobBuilder.Create(type)
                .WithIdentity(jobKey)
                .Build();


            await _scheduler.ScheduleJob(job, trigger);
        }

        public static async Task AddMailJob(string Subject,string Body,string To)
        {
            await Init();


            var trigger = TriggerBuilder.Create()
               .WithDescription("发送邮件通知")
               .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(0))
               .Build();

            var job = JobBuilder.Create(typeof(MailJob))
                .WithIdentity("邮件任务")
                .Build();

            job.JobDataMap.Put("Subject",Subject);
            job.JobDataMap.Put("Body", Body);
            job.JobDataMap.Put("To", To);

            await _scheduler.ScheduleJob(job, trigger);
        }
        /// <summary>
        /// 恢复任务
        /// </summary>
        /// <param name="jobKey">键</param>
        public static async Task Resume(JobKey jobKey)
        {
            Init();
            _scheduler = await _schedulerFactory.GetScheduler();
            //LogUtil.Debug($"恢复任务{jobKey.Group},{jobKey.Name}");
            await _scheduler.ResumeJob(jobKey);
        }
        /// <summary>
        /// 停止任务
        /// </summary>
        /// <param name="jobKey">键</param>
        public static async Task Stop(JobKey jobKey)
        {
            Init();
            _scheduler = await _schedulerFactory.GetScheduler();
            //LogUtil.Debug($"暂停任务{jobKey.Group},{jobKey.Name}");
            await _scheduler.PauseJob(jobKey);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private static async Task Init()
        {
            if (_scheduler == null)
            {
                _schedulerFactory = new StdSchedulerFactory();// ServiceLocator.Services.GetService<ISchedulerFactory>();
                _scheduler = await _schedulerFactory.GetScheduler();

                await _scheduler.Start();
            }
        }
    }
}
