package com.ym.mec.task.core;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ym.mec.task.core.BaseTask;

public class QuartzHelper {

	private final static Logger LOGGER = LoggerFactory.getLogger(QuartzHelper.class);

	/**
	 * 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
	 * @param scheduler 调度对象
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组
	 * @param triggerName 触发器名称
	 * @param triggerGroupName 触发器组
	 * @param jobClass 具体任务
	 * @param cronExp 任务执行的时间表达式
	 * @return
	 * @throws SchedulerException
	 */
	public static boolean addJob(Scheduler scheduler, String jobName, String jobGroupName, String triggerName, String triggerGroupName,
			Class<? extends BaseTask> jobClass, String cronExp) throws SchedulerException {
		if (scheduler == null) {
			throw new RuntimeException("Scheduler-instance must be not null");
		}
		if (StringUtils.isBlank(jobName)) {
			throw new RuntimeException("jobName must be not blank");
		}
		if (StringUtils.isBlank(triggerName)) {
			throw new RuntimeException("triggerName must be not blank");
		}
		if (StringUtils.isBlank(jobGroupName)) {
			jobGroupName = Scheduler.DEFAULT_GROUP;
		}
		if (StringUtils.isBlank(triggerGroupName)) {
			triggerGroupName = Scheduler.DEFAULT_GROUP;
		}
		JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
		if (scheduler.checkExists(jobKey)) {
			scheduler.deleteJob(jobKey);
		}
		JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();

		// 表达式调度构建器
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExp);
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(scheduleBuilder).build();

		scheduler.scheduleJob(jobDetail, trigger);
		return true;
	}

	/**
	 * 修改一个任务的触发时间
	 * 
	 * @param scheduler 调度对象
	 * @param triggerName 触发器名称
	 * @param triggerGroupName 触发器组
	 * @param time 任务执行新的时间表达式
	 * @return
	 * @throws SchedulerException 
	 */
	public static boolean modifyJobTime(Scheduler scheduler, String triggerName, String triggerGroupName, String time) throws SchedulerException {

		TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
		Trigger trigger = scheduler.getTrigger(triggerKey);
		if (trigger == null) {
			return false;
		}
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time);
		trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

		Date date = scheduler.rescheduleJob(triggerKey, trigger);
		LOGGER.info("Successed to modify trigger time(JobTrigger[{},{}]), and next fired time {}", triggerGroupName, triggerName, date);
		return true;
	}

	/**
	 * 移除一个任务
	 * 
	 * @param scheduler 调度对象
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组
	 * @return
	 * @throws SchedulerException 
	 */
	public static boolean deleteJob(Scheduler scheduler, String jobName, String jobGroupName) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
		if (jobKey == null) {
			return false;
		}
		return scheduler.deleteJob(jobKey);
	}

	/**
	 * 暂停任务
	 * @param scheduler 调度对象
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组
	 * @return
	 * @throws SchedulerException
	 */
	public static boolean pauseJob(Scheduler scheduler, String jobName, String jobGroupName) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
		if (jobKey == null) {
			return false;
		}
		scheduler.pauseJob(jobKey);
		return true;
	}

	/**
	 * 恢复任务
	 * @param scheduler 调度对象
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组
	 * @return
	 * @throws SchedulerException
	 */
	public static boolean resumeJob(Scheduler scheduler, String jobName, String jobGroupName) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
		if (jobKey == null) {
			return false;
		}
		scheduler.resumeJob(jobKey);
		return true;
	}

	/**
	 * 立即执行任务
	 * @param scheduler 调度对象
	 * @param jobName 任务名称
	 * @param jobGroupName 任务组
	 * @return
	 * @throws SchedulerException
	 */
	public static boolean triggerJob(Scheduler scheduler, String jobName, String jobGroupName) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
		if (jobKey == null) {
			return false;
		}
		scheduler.triggerJob(jobKey);
		return true;
	}

}
