package ca.uhn.fhir.jpa.sched;

import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.jpa.model.sched.IHapiScheduler;
import ca.uhn.fhir.jpa.model.sched.ScheduledJobDefinition;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.Validate;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static org.quartz.impl.StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME;

public abstract class BaseHapiScheduler implements IHapiScheduler {
	private static final Logger ourLog = LoggerFactory.getLogger(BaseHapiScheduler.class);

	private static final AtomicInteger ourNextSchedulerId = new AtomicInteger();

	private final String myThreadNamePrefix;
	private final AutowiringSpringBeanJobFactory mySpringBeanJobFactory;
	private final StdSchedulerFactory myFactory = new StdSchedulerFactory();
	private final Properties myProperties = new Properties();

	private Scheduler myScheduler;

	public BaseHapiScheduler(String theThreadNamePrefix, AutowiringSpringBeanJobFactory theSpringBeanJobFactory) {
		myThreadNamePrefix = theThreadNamePrefix;
		mySpringBeanJobFactory = theSpringBeanJobFactory;
	}


	void setInstanceName(String theName) {
		myProperties.setProperty(PROP_SCHED_INSTANCE_NAME, theName + "-" + nextSchedulerId());
	}


	int nextSchedulerId() {
		return ourNextSchedulerId.getAndIncrement();
	}

	@Override
	public void init() throws SchedulerException {
		setProperties();
		myFactory.initialize(myProperties);
		myScheduler = myFactory.getScheduler();
		myScheduler.setJobFactory(mySpringBeanJobFactory);
		myScheduler.standby();
	}

	protected void setProperties() {
		addProperty("org.quartz.threadPool.threadCount", "4");
		addProperty("org.quartz.threadPool.threadNamePrefix", myThreadNamePrefix + "-" + myProperties.get(PROP_SCHED_INSTANCE_NAME));
	}

	protected void addProperty(String key, String value) {
		myProperties.put(key, value);
	}

	@Override
	public void start() {
		try {
			myScheduler.start();
		} catch (SchedulerException e) {
			ourLog.error("Failed to start up scheduler", e);
			throw new ConfigurationException("Failed to start up scheduler", e);
		}
	}

	@Override
	public void shutdown() {
		try {
			myScheduler.shutdown(true);
		} catch (SchedulerException e) {
			ourLog.error("Failed to shut down scheduler", e);
			throw new ConfigurationException("Failed to shut down scheduler", e);
		}
	}

	@Override
	public boolean isStarted() {
		try {
			return myScheduler.isStarted();
		} catch (SchedulerException e) {
			ourLog.error("Failed to determine scheduler status", e);
			return false;
		}
	}

	@Override
	public void clear() throws SchedulerException {
		myScheduler.clear();
	}

	@Override
	public void logStatusForUnitTest() {
		try {
			Set<JobKey> keys = myScheduler.getJobKeys(GroupMatcher.anyGroup());
			String keysString = keys.stream().map(t -> t.getName()).collect(Collectors.joining(", "));
			ourLog.info("Local scheduler has jobs: {}", keysString);
		} catch (SchedulerException e) {
			ourLog.error("Failed to get log status for scheduler", e);
			throw new InternalErrorException("Failed to get log status for scheduler", e);
		}
	}

	@Override
	public void scheduleJob(long theIntervalMillis, ScheduledJobDefinition theJobDefinition) {
		Validate.isTrue(theIntervalMillis >= 100);

		Validate.notNull(theJobDefinition);
		Validate.notNull(theJobDefinition.getJobClass());
		Validate.notBlank(theJobDefinition.getId());

		JobKey jobKey = new JobKey(theJobDefinition.getId());

		JobDetailImpl jobDetail = new NonConcurrentJobDetailImpl();
		jobDetail.setJobClass(theJobDefinition.getJobClass());
		jobDetail.setKey(jobKey);
		jobDetail.setName(theJobDefinition.getId());
		jobDetail.setJobDataMap(new JobDataMap(theJobDefinition.getJobData()));

		ScheduleBuilder<? extends Trigger> schedule = SimpleScheduleBuilder
			.simpleSchedule()
			.withIntervalInMilliseconds(theIntervalMillis)
			.repeatForever();

		Trigger trigger = TriggerBuilder.newTrigger()
			.forJob(jobDetail)
			.startNow()
			.withSchedule(schedule)
			.build();

		Set<? extends Trigger> triggers = Sets.newHashSet(trigger);
		try {
			myScheduler.scheduleJob(jobDetail, triggers, true);
		} catch (SchedulerException e) {
			ourLog.error("Failed to schedule job", e);
			throw new InternalErrorException(e);
		}

	}

	private static class NonConcurrentJobDetailImpl extends JobDetailImpl {
		private static final long serialVersionUID = 5716197221121989740L;

		// All HAPI FHIR jobs shouldn't allow concurrent execution
		@Override
		public boolean isConcurrentExectionDisallowed() {
			return true;
		}
	}
}

@@ -333,16 +333,58 @@
 lidate;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import o
@@ -396,16 +396,16 @@
 artz.*;%0A
-
 import o
@@ -1310,16 +1310,48 @@
 heduler;
+%0A%09private String myInstanceName;
 %0A%0A%09publi
@@ -1585,24 +1585,32 @@
 e(String the
+Instance
 Name) %7B%0A%09%09my
@@ -1613,91 +1613,38 @@
 %09%09my
-Properties.setProperty(PROP_SCHED_INSTANCE_NAME, theName + %22-%22 + nextSchedulerId())
+InstanceName = theInstanceName
 ;%0A%09%7D
@@ -2059,132 +2059,278 @@
 ;%0A%09%09
-addProperty(%22org.quartz.threadPool.threadNamePrefix%22, myThreadNamePrefix + %22-%22 + myProperties.get(PROP_SCHED_INSTANCE_NAME))
+myProperties.setProperty(PROP_SCHED_INSTANCE_NAME, myInstanceName + %22-%22 + nextSchedulerId());%0A%09%09addProperty(%22org.quartz.threadPool.threadNamePrefix%22, getThreadPrefix());%0A%09%7D%0A%0A%09@NotNull%0A%09private String getThreadPrefix() %7B%0A%09%09return myThreadNamePrefix + %22-%22 + myInstanceName
 ;%0A%09%7D
@@ -2453,32 +2453,32 @@
 d start() %7B%0A
-
 %09%09try %7B%0A
 %09%09%09mySchedul
@@ -2457,32 +2457,92 @@
 art() %7B%0A%09%09try %7B%0A
+%09%09%09ourLog.info(%22Starting scheduler %7B%7D%22, getThreadPrefix());%0A
 %09%09%09myScheduler.s

improve logging