package extend.zp;

import java.util.Collections;
import java.util.Map.Entry;

import com.game.GameController;
import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.game.player.state.PlayerWaitState;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.routes.ActionKey;
import com.taurus.core.util.Logger;
import com.taurus.core.util.Utils;
import com.taurus.permanent.data.Session;

import extend.zp.uitl.CardUtil;


public class EXGameController extends GameController {
	Logger logger = Logger.getLogger(getClass());
	public EXGameController() {
		super();
	}


	@ActionKey(Config.GAME_ACTION)
	public void RouterAction(Session sender,ITObject params,int gid,Player owner) {
		owner.stateMachine.execute(EXActionEvent.EVENT_ACTION, 0, params);
	}

	@ActionKey(Config.GAME_DIS_CARD)
	public void RouterDisCard(Session sender,ITObject params,int gid,Player owner) {
		owner.stateMachine.execute(EXActionEvent.EVENT_DISCARD, 0, params);
	}
	
	@ActionKey(Config.GAME_CHANGE_PAIXING)
	public void RouterChangePaixing(Session sender,ITObject params,int gid,Player owner) {
		EXPlayer player = (EXPlayer) owner;
		player.getRoom().changePaiXing(player, params);
	}
	
	@ActionKey(Config.GAME_CHANGE_TYPEFACE)
	public void RouterChangeTypeFace(Session sender,ITObject params,int gid,Player owner) {
		EXPlayer player = (EXPlayer) owner;
		int typeface = params.getInt("typeface");
		player.getRoom().typeface = typeface;
	}

	public void changeActiveSeat(EXRoom owner, int activeSeat) {
		owner.activeSeat = activeSeat;
		ITObject param = new TObject();
		param.putInt("seat", activeSeat);
		owner.broadCastToClient(0, Config.GAME_EVT_CHANGE_ACTIVE_PLAYER, param);
	}

	public void getCard(EXPlayer player, int leftCount) {
		ITObject param = new TObject();
		param.putInt("card", player.drawCard);
		param.putInt("seat", player.room.activeSeat);
		param.putInt("left_count", leftCount);
		player.sendEvent(Config.GAME_EVT_DRAW, param);

		player.getRoom().getPlayBack().addGetCardCommand(player.room.activeSeat, player.drawCard, leftCount);
	}

	public void outCard(EXPlayer player, int discard) {
		ITObject response = new TObject();
		response.putInt("seat", player.seat);
		response.putInt("card", discard);
		player.room.broadCastToClient(0, Config.GAME_EVT_DISCARD, response);
		EXRoom room = player.getRoom();
		room.getPlayBack().addOutCardCommand(player.seat, discard);

		room.activeCard = discard;
		room.lastDiscardSeat = player.seat;
		room.currenDiscardSeat = 0;

		CardUtil.removeCard(player.cardInhand, discard, 1);
		room.broadCastToServer(player.playerid, EXActionEvent.EVENT_OTHER_DISCARD, discard);
	}

	public void actionCard(EXPlayer player, int active_card, int card, int type, int from_seat) {
		actionCard(player, active_card, card, type, from_seat, null);
	}

	public void actionCard(EXPlayer player, int active_card, int card, int type, int from_seat, ITArray opcardArray) {
		ITObject paramBroadCast = new TObject();
		paramBroadCast.putInt("playerid", player.playerid);
		paramBroadCast.putInt("active_card", active_card);
		paramBroadCast.putInt("card", card);
		paramBroadCast.putInt("type", type);
		paramBroadCast.putInt("from_seat", from_seat);
		int huxi = player.getShowHuXi(true);
		paramBroadCast.putInt("hu_xi", huxi);
		if (opcardArray != null) {
			paramBroadCast.putTArray("opcard", opcardArray);
		}
		EXRoom room = player.getRoom();
		room.getPlayBack().addActionCommand(player.seat, type, active_card, card, from_seat, huxi, opcardArray);

		player.sendEvent(Config.GAME_EVT_ACTION, paramBroadCast);
		ITObject otherBroadCast = new TObject();
		Utils.objectCopyDeep(paramBroadCast, otherBroadCast);
		otherBroadCast.putInt("hu_xi", player.getShowHuXi(false));
		room.broadCastToClient(player.playerid, Config.GAME_EVT_ACTION, otherBroadCast);
	}

	public void dealCard(EXRoom owner) {
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			player.cardInhand = owner.card.deal();

//			if (2 == player.seat) {
//				player.cardInhand.clear();
//				String cardStr = "101, 102, 102, 103, 103, 109, 201, 201, 202, 202, 203, 203, 204, 204, 204, 206, 207, 208, 209, 210";
//				String[] cards = cardStr.split(", ");
//				for(String c : cards) {
//					Integer card = Integer.valueOf(c);
//					player.cardInhand.add(card);
//				}
//			}
//			if (3 == player.seat) {
//				player.cardInhand.clear();
//				String cardStr = "101, 101, 201, 201, 202, 202, 203, 203, 103, 104, 105, 106, 204, 207, 207, 107, 208, 209, 110, 110";
//				String[] cards = cardStr.split(", ");
//				for(String c : cards) {
//					Integer card = Integer.valueOf(c);
//					player.cardInhand.add(card);
//				}
//			}
//			if (1 == player.seat) {
//				player.cardInhand.clear();
//				String cardStr = "102, 103, 103, 104, 104, 104, 107, 107, 107, 108, 110, 201, 202, 204, 205, 205, 206, 206, 206, 209";
//				String[] cards = cardStr.split(", ");
//				for(String c : cards) {
//					Integer card = Integer.valueOf(c);
//					player.cardInhand.add(card);
//				}
//			}

			Collections.sort(player.cardInhand);

			ITObject param = new TObject();
			ITArray handCard = Util.toTArray(player.cardInhand);
			param.putTArray("card_list", handCard);
			param.putInt("bank_seat", player.room.bankerSeat);
			player.sendEvent(Config.GAME_EVT_PLAYER_DEAL, param);

			player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			
			logger.info("roomId:" + owner.roomid + ",round:" + owner.round + ",player:" + player + ",cardInhand:" + player.cardInhand);
		}
//		owner.card.cardList.clear();
//		String cardStr = "210, 105, 101, 209, 106, 203, 109, 109, 204, 109, 108, 207, 110, 103, 203, 201, 107, 106, 110, 201, 210, 208, 101, 201, 105, 210, 205, 102, 108, 208, 105, 108, 102, 207, 202, 103, 106, 210, 202, 209";
//		String[] cards = cardStr.split(", ");
//		for(String c : cards) {
//			Integer card = Integer.valueOf(c);
//			owner.card.cardList.add(card);
//		}
		logger.info("roomId:" + owner.roomid + ",round:" + owner.round + ",cardList:" + owner.card.cardList);
	}

	public void tipEvent(EXPlayer owner) {
		ITObject param = owner.tipMgr.toMP();
		if (!owner.tipMgr.tipMap.isEmpty()) {
			logger.info(param.toString());
			owner.sendEvent(Config.GAME_EVT_FZTIPS, param);
		}
	}

	public void discardTipEvent(EXPlayer owner) {
		owner.getRoom().currenDiscardSeat = owner.seat;
		owner.sendEvent(Config.GAME_EVT_DISCARD_TIP, null);
	}

	public void winEvent(EXPlayer owner, int from_seat) {
		ITArray cardInHand = TArray.newInstance();
		for (int card1 : owner.cardInhand) {
			cardInHand.addInt(card1);
		}
		ITObject param = new TObject();
		param.putTArray("card", cardInHand);
		param.putInt("seat", owner.seat);
		param.putInt("from_seat", from_seat);

		EXRoom room = owner.getRoom();
		room.broadCastToClient(0, Config.GAME_EVT_HU, param);
		room.getPlayBack().addWinCardCommand(owner.seat, false);
	}

	public ITObject getRoomResultData(EXRoom owner) {
		ITObject mp = TObject.newInstance();
		mp.putBoolean("liuju", owner.liuju);
		mp.putInt("xipai_score", owner.xi_pai_score);
		//mp.putInt("anchou_score", owner.an_chou_score);
		mp.putInt("active_player", owner.playerMapBySeat.get(owner.activeSeat).playerid);
		mp.putTArray("left_card", Util.toTArray(owner.card.cardList));
		mp.putTArray("throw_card", Util.toTArray(owner.throwCardsList));
		long time = System.currentTimeMillis();
		long t = time / 1000;
		mp.putLong("time", t);
		ITArray infoList = new TArray();
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			ITObject param = new TObject();
			param.putInt("seat", player.seat);
			param.putInt("win_card", player.winCard);
			param.putBoolean("is_win", player.winer == 1);
			param.putInt("hu_xi", player.huXi);
			param.putInt("tun", player.winScore);
			player.hp_info(param);
			if (owner.maxPlayers == 2) {
				param.putInt("total_tun", player.score.round_score);
			} else {
				param.putInt("total_tun", player.score.round_score / 2);
			}

			param.putInt("round_score", player.score.round_score);
			param.putInt("total_score", player.score.total_score);

			ITArray mtList = TArray.newInstance();
			for (MingTang mingTang : player.mingtangList) {
				ITObject obj = TObject.newInstance();
				obj.putInt("mingtang", mingTang.mingTang);
				obj.putInt("type", mingTang.type);
				obj.putInt("score", mingTang.score);

				mtList.addTObject(obj);
			}
			param.putTArray("mingtang", mtList);

			if (player.winer == 1) {
				ITArray handCard = Util.toTArray(player.handCardEx);
				param.putTArray("hand_card", handCard);

				ITArray cardList = TArray.newInstance();
				for (OpCard opCard : player.handCard) {
					ITObject obj = TObject.newInstance();
					obj.putInt("type", opCard.type);
					obj.putInt("hu_xi", opCard.huXi);
					obj.putInt("card1", opCard.card1);
					obj.putInt("card2", opCard.card2);
					obj.putInt("card3", opCard.card3);

					cardList.addTObject(obj);
				}
				param.putTArray("cardList", cardList);
			} else {
				ITArray handCard = Util.toTArray(player.cardInhand);
				param.putTArray("hand_card", handCard);
			}

			ITArray opCardList = TArray.newInstance();
			for (OpCard opCard : player.opCardList) {
				ITObject obj = TObject.newInstance();
				obj.putInt("type", opCard.type);
				obj.putInt("hu_xi", opCard.huXi);
				obj.putInt("card1", opCard.card1);
				obj.putInt("card2", opCard.card2);
				obj.putInt("card3", opCard.card3);

				opCardList.addTObject(obj);
			}
			param.putTArray("opCardList", opCardList);

			infoList.addTObject(param);
		}
		mp.putTArray("info_list", infoList);
		return mp;
	}

	public void roomResult(EXRoom owner) {
		ITObject mp = TObject.newInstance();
		mp.putInt("type", 0);
		ITObject result = getRoomResultData(owner);
		mp.putTObject("result", result);
		owner.broadCastToClient(0, Config.GAME_EVT_RESULT1, mp);
	}

	public void roomTotalResult(EXRoom owner, boolean dissmiss) {
		ITObject data = TObject.newInstance();
		long t = System.currentTimeMillis() / 1000;
		data.putLong("time", t);
		ITArray infoList = new TArray();
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			ITObject param = new TObject();
			param.putInt("seat", player.seat);
			param.putInt("total_huxi", player.total_huxi);
			param.putInt("win_count", player.winCount);
			param.putInt("total_score", player.score.total_score);
			player.hp_info(param);
			infoList.addTObject(param);
		}
		data.putTArray("info_list", infoList);

		ITObject mp = TObject.newInstance();
		mp.putInt("type", dissmiss ? 2 : 1);
		if (!dissmiss) {
			ITObject result = getRoomResultData(owner);
			mp.putTObject("result", result);
		}
		mp.putTObject("total_result", data);

		owner.broadCastToClient(0, Config.GAME_EVT_RESULT1, mp);
	}
}
