package com.eactive.eai.common.scheduler;

import java.util.HashMap;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;
import com.eactive.eai.common.util.Logger;

public class SchedulerManager implements Lifecycle{
    static Logger logger = Logger.getLogger(Logger.LOGGER_SCHEDULER);
	
	private static SchedulerManager myInstance = new SchedulerManager();
    private LifecycleSupport lifecycle = new LifecycleSupport(this);
    private boolean started;

	
	private HashMap<String, Scheduler> schedulers = new HashMap<String, Scheduler>();
	
	public static SchedulerManager getInstance() {
		if(myInstance == null) {
			myInstance = new SchedulerManager();
		}
		return myInstance;
	}
	
	public void create(String schedulerId) throws Exception {
		Scheduler scheduler = Scheduler.create(schedulerId);
		schedulers.put(schedulerId, scheduler);
	}
	
	public void start(String schedulerId) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.start();
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void stop(String schedulerId) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.stop();
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public Scheduler getScheduler(String schedulerId) {
		Scheduler scheduler = schedulers.get(schedulerId);
		return scheduler;
	}
	
	public void scheduleAllJobs(String schedulerId) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.scheduleAllJobs();
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void deleteAllJobs(String schedulerId) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.clear();
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void scheduleJob(String schedulerId, ScheduleJobInfo info) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			try {
				scheduler.scheduleJob(info);
			}
			catch(Exception ex) {
				logger.warn("Scheduler Error["+schedulerId+"] " + ex.toString());				
			}
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void deleteJob(String schedulerId, String jobNm) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			logger.warn("Scheduler.deleteJob - " + jobNm);
			scheduler.deleteJob(jobNm);
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void triggerJob(String schedulerId, String jobNm) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.triggerJob(jobNm);
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}
	
	public void pauseJob(String schedulerId, String jobNm) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.pauseJob(jobNm);
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}

	public void resumeJob(String schedulerId, String jobNm) {
		Scheduler scheduler = this.getScheduler(schedulerId);
		if(scheduler != null) {
			scheduler.resumeJob(jobNm);
		}
		else {
			logger.warn("Scheduler is null - ");// 20250910 npe +scheduler 삭제
		}
	}

	public void addLifecycleListener(LifecycleListener listener) {
		lifecycle.addLifecycleListener(listener);
	}

	public LifecycleListener[] findLifecycleListeners() {
		return lifecycle.findLifecycleListeners();
	}

	public void removeLifecycleListener(LifecycleListener listener) {
        lifecycle.removeLifecycleListener(listener);		
	}

	public void start() throws LifecycleException {
		
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STARTING_EVENT, this);

		String schedulerId = SchedulerKeys.DEFAULT_SCHEDULER_ID;
		SchedulerManager scheduleManager = SchedulerManager.getInstance();
		try {
			scheduleManager.create(schedulerId);
		} catch (Exception e) {
			logger.error( e.getMessage(), e);
		}
		scheduleManager.scheduleAllJobs(schedulerId);
		this.start(schedulerId);
		
        started = true;

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STARTED_EVENT, this);

	}

	public void stop() throws LifecycleException {
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STOPING_EVENT, this);

		String schedulerId = SchedulerKeys.DEFAULT_SCHEDULER_ID;
		this.stop(schedulerId);

		started = false;
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);

	}

	public boolean isStarted() {
		return this.started;
	}
	
	
}
