using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
using XYY.Tool.TimingTask.jobs;

namespace XYY.Tool.QuotaReduction
{
    public class ServiceRunner : ServiceControl, ServiceSuspend
    {
        private readonly IScheduler scheduler;
        public ServiceRunner()
        {
            scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
        }
        public bool Start(HostControl hostControl)
        {
            scheduler.Start();

            //PBPostManifestJob testPbJob = new PBPostManifestJob();
            //testPbJob.Execute(null).Wait();

            //具体任务启动可在配置文件编写
            for (var i = 0; i < JobConfig.jobs.Length; i++)
            {
                IJobDetail job1 = JobConfig.jobs[i];
                ITrigger trigger1 = TriggerBuilder.Create().StartNow().WithCronSchedule(JobConfig.crons[i]).Build();
                scheduler.ScheduleJob(job1, trigger1);
            }
            return true;
        }
        public bool Continue(HostControl hostControl)
        {
            scheduler.ResumeAll();
            return true;
        }

        public bool Pause(HostControl hostControl)
        {
            scheduler.PauseAll();
            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            scheduler.Shutdown(false);
            return true;
        }
    }
}
