package com.evt.mgr;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import com.evt.mgr.job.CleanGroupLogJob;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import com.data.util.ConsumeCode;
import com.data.util.EventType;
import com.evt.mgr.handler.HandlerGroupMemberRound;
import com.evt.mgr.handler.HandlerGroupRound;
import com.evt.mgr.handler.HandlerHpConsume;
import com.evt.mgr.handler.HandlerLose;
import com.evt.mgr.handler.HandlerOver;
import com.evt.mgr.handler.HandlerPay;
import com.evt.mgr.handler.HandlerWin;
import com.evt.mgr.job.CleanTimeOutRoomJob;
import com.taurus.core.routes.Extension;
import com.taurus.core.routes.Routes;
import com.taurus.core.util.Logger;

public class EventServer extends Extension{
	static EventReceiver eventReceiver;
	public static Logger log;
	
	@Override
	public void onStart() {
		log = Logger.getLogger(EventServer.class);
		eventReceiver = new EventReceiver();
		initHandler();
		
		initJob();
		
		eventReceiver.start();
	}
	
	@Override
	public void configRoute(Routes me) {
	}
	
	public void initJob() {
		
		try {
			log.info("clean invalid room, no diamond");
			
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();
			
			JobDetail job = newJob(CleanTimeOutRoomJob.class).withIdentity("clean_group_room", "group").build();
			CronTrigger trigger= newTrigger().withIdentity("clean_group_room_trigger", "group").withSchedule(cronSchedule("0 0 5 * * ? ")).build();
			sched.scheduleJob(job, trigger);
			
			job = newJob(CleanTimeOutRoomJob.class).withIdentity("clean_invalid_room", "group").build();
			trigger= newTrigger().withIdentity("clean_invalid_room_trigger", "group").withSchedule(cronSchedule("0 30 5 * * ? ")).build();
			sched.scheduleJob(job, trigger);

			JobDetail log_job = newJob(CleanGroupLogJob.class).withIdentity("clean_group_member_log", "group").build();
			CronTrigger log_trigger= newTrigger().withIdentity("clean_group_member_log_trigger", "group").withSchedule(cronSchedule("0 0 0/1 * * ? ")).build();
			sched.scheduleJob(log_job, log_trigger);

			log_job = newJob(CleanGroupLogJob.class).withIdentity("clean_group_hp_log", "group").build();
			log_trigger= newTrigger().withIdentity("clean_group_hp_log_trigger", "group").withSchedule(cronSchedule("0 0 0/1 * * ? ")).build();
			sched.scheduleJob(log_job, log_trigger);

			log_job = newJob(CleanGroupLogJob.class).withIdentity("clean_rec_room_log", "group").build();
			log_trigger= newTrigger().withIdentity("clean_rec_room_log_trigger", "group").withSchedule(cronSchedule("0 0 0/1 * * ? ")).build();
			sched.scheduleJob(log_job, log_trigger);
			
			sched.start();
		}
		catch(Exception e) {
			
		}
	}
	
	
	private void initHandler() {
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_OVER, new HandlerOver());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_LOSE, new HandlerLose());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_WIN, new HandlerWin());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_PAY, new HandlerPay(ConsumeCode.DIAMO_JOIN_ROOM,true));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_BACK_PAY, new HandlerPay(ConsumeCode.DIAMO_REFUND,false));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_GROUP_ROOM, new HandlerPay(ConsumeCode.DIAMO_AGENT_ROOM,true));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_CREATE_ROOM, new HandlerPay(ConsumeCode.DIAMO_CREAT_ROOM,true));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_MAIL, new HandlerPay(ConsumeCode.DIAMO_MAIL,false));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_WHEEL, new HandlerPay(ConsumeCode.DIAMO_WHEEL,false));
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_GROUP_ROUND, new HandlerGroupRound());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_GROUP_MEMBER_ROUND, new HandlerGroupMemberRound());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_GROUP_HP, new HandlerHpConsume());
		eventReceiver.handlerMap.put(EventType.REDIS_EVENT_ADDPAY, new HandlerPay(ConsumeCode.DIAMO_ADD,false));
	}

	@Override
	public void onStop() {
		eventReceiver.destroy();
	}
	
	
}
