package com.eactive.eai.rms.common.scheduler;

import com.eactive.eai.common.util.ContainerUtil;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Properties;

@Configuration
public class ClusteredSchedulerConfig {
   
   @Autowired
   private ApplicationContext applicationContext;
   
   @Autowired @Qualifier("MONITORING")
   private DataSourceType monitoringDatasource;
   
   /*
   public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory {
      @Override
      protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
         Object job = super.createJobInstance(bundle);
         applicationContext.getAutowireCapableBeanFactory().autowireBean(job);
         return job;
      }
   }
   
   @Bean
   public SpringBeanJobFactory springBeanJobFactory() {
      return new AutowiringSpringBeanJobFactory();
   }
   */
   
   @Bean(name = "clusteredScheduler")
   public SchedulerFactoryBean clusteredScheduler() {
      
      String monitoringSchema = monitoringDatasource.getSchema();
      String monitoringJndiName = monitoringDatasource.getJndiName();
      
      SchedulerFactoryBean factory = new SchedulerFactoryBean();
      
      Properties quartzProperties = new Properties();
      
      quartzProperties.put("applicationContextSchedulerContextKey", "applicationContext");
      
      quartzProperties.put("org.quartz.scheduler.instanceName", "EMS_ClusteredScheduler");
      quartzProperties.put("org.quartz.scheduler.instanceId", "AUTO");
      
      quartzProperties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
      quartzProperties.put("org.quartz.threadPool.threadCount", "5");
      quartzProperties.put("org.quartz.threadPool.threadPriority", "5");
      
      quartzProperties.put("org.quartz.jobStore.misfireThreshold", "60000");
      quartzProperties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
      quartzProperties.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
      quartzProperties.put("org.quartz.jobStore.useProperties", "false");
      quartzProperties.put("org.quartz.jobStore.dataSource", monitoringSchema);
      quartzProperties.put("org.quartz.jobStore.tablePrefix", monitoringSchema + ".QRTZ_");
      quartzProperties.put("org.quartz.jobStore.clusterCheckinInterval", "20000");
      quartzProperties.put("org.quartz.jobStore.acquireTriggersWithinLock", "true");
      
      String eaiSystemMode = System.getProperty("eai.systemmode");
      
      if(isTomcat()) {
         
         quartzProperties.put("org.quartz.jobStore.isClustered", "true");
         quartzProperties.put("org.quartz.dataSource."+monitoringSchema+".jndiURL", "java:comp/env/" + monitoringJndiName);
         
      } else {
         
         quartzProperties.put("org.quartz.jobStore.isClustered", "true");
         quartzProperties.put("org.quartz.dataSource."+monitoringSchema+".jndiURL", monitoringJndiName);
         
      }
      
      factory.setQuartzProperties(quartzProperties);
      factory.setAutoStartup(true);
      factory.setSchedulerName("EMS_ClusteredScheduler");
//      factory.setJobFactory(springBeanJobFactory());
      
      factory.setApplicationContextSchedulerContextKey("applicationContext");
      factory.setApplicationContext(applicationContext);
      
      return factory;
   }

   private boolean isTomcat() {
      return ContainerUtil.TOMCAT == ContainerUtil.get();
   }
   
}
