package com.group;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.data.cache.AccountCache;
import com.data.cache.GroupCache;
import com.group.controller.GroupController;
import com.group.controller.GroupLogController;
import com.group.controller.GroupRoomController;
import com.group.job.UpdatePlayRoomJob;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.routes.Extension;
import com.taurus.core.routes.Routes;
import com.taurus.core.util.FileUtil;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import redis.clients.jedis.Jedis;

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

public class MainServer extends Extension {
	private ScheduledThreadPoolExecutor timeScheduler;
	
	private static final String WEB_CONFIG_KEY = "web_config";
	private static final String FORCE_VER_KEY = "force_ver";
	
	public static Map<String,String> lua_map = new HashMap<>();
	public MainServer() {
		super();
		//3秒
//		NetManager.TIMEOUT_TIME =3;
		timeScheduler = new ScheduledThreadPoolExecutor(1);
		timeScheduler.scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				AccountCache.clearData();
				GroupCache.clearData();
				System.gc();
			}
		}, 0,600, TimeUnit.SECONDS);
		loadLua();
		initJob();
	}

	@Override
	public int readVersion() {
		int ver = Integer.parseInt(Redis.use("group1_db1").hget(WEB_CONFIG_KEY,FORCE_VER_KEY));
		return ver;
	}


	@Override
	public void configRoute(Routes me) {
		me.setInterceptor(new WebInterceptor());

		me.add("group", GroupController.class);
		me.add("group/room", GroupRoomController.class);
		me.add("group/log",GroupLogController.class);
	}
	
	private static void loadLua() {
		String path = System.getProperty("WORKDIR");
		try {
			Jedis jedis10 = Redis.use("group1_db10").getJedis();
			try {
				String str = FileUtil.readTxt(path + "/config/mgr.lua");
				lua_map.put("mgr", jedis10.scriptLoad(str));
				str = FileUtil.readTxt(path + "/config/trade.lua");
				lua_map.put("trade", jedis10.scriptLoad(str));
				str = FileUtil.readTxt(path + "/config/take_hp.lua");
				lua_map.put("take_hp", jedis10.scriptLoad(str));
				str = FileUtil.readTxt(path + "/config/bank_hp.lua");
				lua_map.put("bank_hp", jedis10.scriptLoad(str));
			} finally {
				jedis10.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initJob() {

		try {
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();

			JobDetail job = newJob(UpdatePlayRoomJob.class).withIdentity("update_play_room", "group").build();
			CronTrigger trigger= newTrigger().withIdentity("update_play_room_trigger", "group").withSchedule(cronSchedule("0/5 * * * * ? ")).build();
			sched.scheduleJob(job, trigger);

			job = newJob(UpdatePlayRoomJob.class).withIdentity("create_play_room", "group").build();
			trigger= newTrigger().withIdentity("create_play_room_trigger", "group").withSchedule(cronSchedule("0/3 * * * * ? ")).build();
			sched.scheduleJob(job, trigger);

			sched.start();
		}
		catch(Exception e) {

		}
	}


	@Override
	public void onStart() {
		System.out.println("web group start!");
	}
	
	@Override
	public void onStop() {
		if(timeScheduler!=null) {
			timeScheduler.shutdownNow();
		}
	}




	


}
