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);
	}


	@ActionKey(Config.GAME_EVT_FANGPAO)
	public void RouterFangwei(Session sender,ITObject params,int gid,Player owner){
		EXPlayer player = (EXPlayer) owner;
		int discard = params.getInt("card");
		ITObject response = new TObject();
		response.putInt("seat", owner.seat);
		response.putInt("card", discard);
		logger.info("jefe GAME_EVT_FANGPAO:" + discard+";player:"+player.playerid);
		owner.room.broadCastToClient(0, Config.GAME_EVT_DISCARD, response);
		player.banChiPeng = true;
		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);

		//回复放跑成功
		ITObject param = new TObject();
		param.putInt("card", discard);
		param.putInt("seat", player.seat);
		player.sendEvent(Config.GAME_EVT_FANGPAO_RSP, param);

	}


	public void outCard(EXPlayer player, int discard) {
		//判断是否 放跑
		//1 获取当前房间的wei
		int outflag = 0;
		if (player.getRoom().weiRoomGroup.size()>0){
			//2 遍历偎
			for (int i = 0; i < player.getRoom().weiRoomGroup.size(); i++) {
				if (discard == player.getRoom().weiRoomGroup.get(i)[0]) {
					//3,如果存在 提示放跑
					//判断是否是托管

					if (!player.isEntrust()){
						EXMainServer.gameCtr.fangPaoTipEvent(player,0,discard);
						outflag = 1;
					}

				}
			}
		}

		if (outflag==0){
			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();

			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.cur_huxi = 0;
			player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			
			logger.info("roomId:" + owner.roomid + ",round:" + owner.round + ",player:" + player + ",cardInhand:" + player.cardInhand);
		}

		logger.info("roomId:" + owner.roomid + ",round:" + owner.round + ",cardList:" + owner.card.cardList);
	}


	/**
	 * @param owner
	 */
	public void fangPaoTipEvent(EXPlayer owner,int reload,int card) {
		ITObject response = new TObject();
		response.putInt("card", card);
		response.putInt("reload", reload);
		response.putInt("seat", owner.seat);
		////System.outln("niao");
		owner.sendEvent(Config.GAME_EVT_FANGPAO_TIP, response);
	}


	/**
	 * @param owner
	 */
	public void daNiaoTipEvent(EXPlayer owner,int reload) {
		ITObject response = new TObject();
		response.putInt("niao", owner.room.config.getInt(Config.GAME_DANIAO));
		response.putInt("reload", reload);
		////System.outln("niao");
		owner.sendEvent(Config.GAME_EVT_DANIAO_TIP, response);
	}

	/**
	 * @param owner
	 * 打鸟事件
	 */
	public void daNiaoEvent(EXPlayer owner) {
		ITObject param = new TObject();
		param.putInt("seat", owner.seat);
		param.putInt("niao", owner.niaoValue*owner.daNiao);
		////System.outln("daniao 回复"+owner.daNiao);
		owner.getRoom().daniaoflag = owner.getRoom().daniaoflag+1;
		owner.room.broadCastToClient(0, Config.GAME_EVT_DANIAO, param);
	}

	/**
	 * @param owner
	 * 放跑协议事件
	 */
	public void sendFangPaoEvent(EXPlayer owner) {
		ITObject param = new TObject();
		param.putInt("seat", owner.seat);
		param.putInt("pao", owner.daNiao);
		////System.outln("fangpao 回复"+owner.daNiao);
		owner.room.broadCastToClient(0, Config.GAME_EVT_FANGPAO, param);
	}

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

	public void sendNiaoEvt(EXPlayer owner, int seat) {
		EXRoom room = owner.getRoom();
		ITObject param = new TObject();
		ITArray array = new TArray();
		param.putInt("start_seat", seat);

		room.broadCastToClient(0, Config.GAME_EVT_DANIAO, param);
		((PlayBack)room.playBackData).addNiaoCommand(owner.seat, param);
	}

	public void tipEvent(EXPlayer owner) {
		ITObject param = owner.tipMgr.toMP();
		if (!owner.tipMgr.tipMap.isEmpty()) {
			
			owner.getRoom().getPlayBack().addTipActionCommand(owner.room.activeSeat, param);
			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) {
		//System.out.println("getRoomResultData");
		ITObject mp = TObject.newInstance();
		mp.putBoolean("liuju", owner.liuju);
		mp.putInt("xipai_score", owner.xi_pai_score/100);
		//mp.putInt("active_player", owner.playerMapBySeat.get(owner.activeSeat).playerid);

		////System.out.println("getRoomResultData:::");
		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.cur_huxi);
			param.putInt("tun", player.winScore);

			player.hp_info(param);


			param.putInt("round_score", player.score.round_score); //当前分数
			param.putInt("fangpaoscore", player.getRoom().fangpaosubvalue); //当前分数
			param.putInt("total_score", player.total_huxi); //单局 累计战绩

			player.huType = 1;
			if (player.getRoom().isFanPao){
				player.huType = 2;
			}
			param.putInt("hu_type", player.huType); //胡牌类型

			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);
			ITArray opCardList = TArray.newInstance();
			if (player.winer == 1&&player.newcalc==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);
					//opCardList.addTObject(obj);
				}
			//	param.putTArray("cardList", cardList);
			} else {
				ITArray handCard = Util.toTArray(player.cardInhand);
				param.putTArray("hand_card", handCard);
			}


			if (player.tmpopCardList.size()>0){
				for (OpCard opCard : player.tmpopCardList) {
					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);
				}
			}else {
				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) {
		//System.out.println("Fule roomResult");
		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) {
		//System.out.println("roomTotalResult");
		ITObject data = TObject.newInstance();
		long t = System.currentTimeMillis() / 1000;
		data.putLong("time", t);
		ITArray infoList = new TArray();
		////System.out.println("roomTotalResult:"+dissmiss);
		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);
			int niaofen = player.niaoValue*player.daNiao;
			//player.score.total_score = player.score.total_score + niaofen;

			param.putInt("total_score", player.score.total_score);//总战绩

			////System.out.println("niaoscore:"+player.niaoValue*player.daNiao);
			player.hp_info(param);
			param.putInt("hupai_count", player.hupai_count); //胡次数
			param.putInt("dianpao_count", player.dianpao_count); //点炮次数
			param.putInt("tipai_count", player.tipai_count); //提牌总数
			param.putInt("paopai_count", player.paopai_count); //跑牌总数

			param.putInt("niaoscore",niaofen);
			infoList.addTObject(param);
		}
		data.putTArray("info_list", infoList);

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

		mp.putTObject("total_result", data);

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