package taurus.client.business;

import com.data.util.ErrorCode;
import com.robot.Global;
import com.robot.MainServer;
import com.robot.data.JoinRoomData;
import com.robot.data.Room;
import com.google.gson.Gson;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.plugin.redis.RedisLock;
import com.taurus.core.util.StringUtil;
import com.taurus.permanent.data.Session;
import redis.clients.jedis.Jedis;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间管理类
 * 
 *
 */
public class RoomBusiness {

	public ConcurrentHashMap<String, Room> tableMap;

	public RoomBusiness() {
		this.tableMap = new ConcurrentHashMap<String, Room>();
	}
	/**
	 * 进入房间
	 * 
	 * @param sender
	 * @param session_key
	 * @param gid
	 */
	public void joinRoom(Session sender, String session_key, int gid, String gps_pos) {
		Jedis jedis = Redis.use().getJedis();
		try {
			if (!jedis.exists(session_key)) {
				MainServer.instance.sendResponse(gid, ErrorCode._NO_SESSION, null, sender);
				return;
			}

			String room_key = jedis.hget(session_key, "room");
			if (StringUtil.isEmpty(room_key)) {
				MainServer.instance.sendResponse(gid, ErrorCode.NO_ROOM_NUM, null, sender);
				return;
			}
			Map<String, String> room_map = null;
			RedisLock lock = new RedisLock(room_key, jedis);
			try {
				lock.lock();
				room_map = jedis.hgetAll(room_key);
				String status = room_map.get("status");
				if (StringUtil.isEmpty(status)) {
					Global.gameCtr.delRoomSeat(session_key, room_key);
					MainServer.instance.sendResponse(gid, ErrorCode.NO_ROOM_NUM, null, sender);
					return;
				}
				if (status.equals("3") || status.equals("2")) {
					Global.gameCtr.delRoomSeat(session_key, room_key);
					MainServer.instance.sendResponse(gid, ErrorCode.GROUP_ROOM_DEL, null, sender);
					return;
				}

//				if (Global.gameId != Integer.parseInt(room_map.get("game"))) {
////					Global.logger.error("joinRoom gameId:"+Global.gameId+" != gameId:" + Integer.parseInt(room_map.get("game")));
//					MainServer.instance.sendResponse(gid, ErrorCode.NO_SERVICE, null, sender);
//					return;
//				}
				String room_id = room_map.get("id");
				Room room = null;
				if (this.tableMap.containsKey(room_id)) {
					room = this.tableMap.get(room_id);
					if (room.isDestroy) {
						MainServer.instance.sendResponse(gid, ErrorCode._FAILED, null, sender);
						return;
					}
				} else {
					Jedis jedis0 = Redis.use("group1_db0").getJedis();
					Gson gson = new Gson();
					String json = jedis0.get("robot_" + room_id);
					System.out.println("json:"+json);
					room = gson.fromJson(json, Room.class);
					System.out.println("room:" + room);
//					room = MainServer.instance.newRoom(room_id, room_map);
					room.loadRedisPlayer();
					this.tableMap.put(room_id, room);
					room.start();
//					room.stateMachine.changeState(Global.getState(RoomReloadState.class));
				}
				if (room.isActive() == false) {
					room.start();
				}

				sender.setHashId(session_key);
				final JoinRoomData jrd = new JoinRoomData();
				jrd.room = room;
				jrd.session_key = session_key;
				jrd.sender = sender;
				jrd.gps_pos = gps_pos;
				Global.gameCtr.joinRoom(jrd, gid);
				
			} finally {
				lock.unlock(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
//			Global.logger.error(e);
			MainServer.instance.sendResponse(gid, 500, null, sender);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 解散房间
	 * 
	 * @param roomid 房间ID
	 */
	public void dismissRoom(String roomid, boolean sendEvt) {
		Room room = this.tableMap.get(roomid);
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
		RedisLock lock = new RedisLock(room.room_key, jedis0);
		try {
			lock.lock();
			room.destroy(sendEvt);
			this.tableMap.remove(roomid);
		} finally {
			lock.unlock();
		}
	}

}
