package extend.mj;

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.permanent.data.Session;

import extend.mj.tip.Tip;
import extend.mj.tip.TipManager;

/**
 * 
 * 
 *
 * EXGameController.java
 */
public class EXGameController extends GameController {
	
	public EXGameController() {
		super();
	}
	
	@ActionKey(Config.GAME_ACTION)
	public void RouterAction(Session sender,ITObject params,int gid,Player owner) {
		if(Global.loggerDebug) {
			int id = params.getInt("id");
			if(id ==0) {
				Global.logger.info(owner + " pass!");
			}else {
				 TipManager tipMgr =((EXPlayer)owner).tipMgr;
				if(tipMgr.tipMap.containsKey(id)) {
					Tip tip = tipMgr.tipMap.get(id);
					Global.logger.info(owner + " select_action type:"+tip.weight+" card:"+tip.card);
				}
			}
		}
		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);	
	}

	public void changeActiveSeat(EXRoom owner,int activeSeat) {
		owner.activeSeat = activeSeat;
		TObject 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 otherParam = new TObject();
		otherParam.putInt("card", 0);
		otherParam.putInt("seat", player.room.activeSeat);
		otherParam.putInt("left_count", 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.cardInhand.add(player.drawCard);
		if(Global.loggerDebug) {
			Global.logger.info(player + " drawcard["+player.drawCard+"]");
		}
		player.room.broadCastToClient(player.playerid, Config.GAME_EVT_DRAW, otherParam);
		((EXPlayBack)player.getRoom().playBackData).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();
		((EXPlayBack)room.playBackData).addOutCardCommand(player.seat, discard);
		
		room.activeCard = discard;
		room.lastDiscardSeat = player.seat;
		room.currenDiscardSeat = 0;
		player.outcardList.add(discard);
		Util.removeCard(player.cardInhand, discard, 1);
		if(Global.loggerDebug) {
			Global.logger.info(player + " outcard["+discard+"]");
		}
		room.broadCastToServer(player.playerid, EXActionEvent.EVENT_OTHER_DISCARD, discard);
	}
	
	

	public void actionCard(EXPlayer player,int card,int type,int from_seat,ITArray opcardArray){
		ITObject paramBroadCast = new TObject();
		paramBroadCast.putInt("playerid", player.playerid);
		paramBroadCast.putInt("card", card);
		paramBroadCast.putInt("type", type);
		paramBroadCast.putInt("from_seat", from_seat);
		if(opcardArray!=null){
			paramBroadCast.putTArray("opcard", opcardArray);
		}
		EXRoom room = player.getRoom();
		
		if(Global.loggerDebug) {
			Global.logger.info(String.format("%s from_seat:%d card:%d type:%d ", player,from_seat,card,type));
		}
		room.broadCastToClient(0, Config.GAME_EVT_ACTION, paramBroadCast);
		((EXPlayBack)room.playBackData).addActionCommand(player.seat, type, card, from_seat,opcardArray);
	}
	
	public void dealCard(EXRoom owner) {
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer)entry.getValue();
			//Global.logger.info("playerid:"+player.playerid+" isw: "+player.is_white);

			player.cardInhand = owner.card.deal(player.is_white);
			Collections.sort(player.cardInhand);
			if(Global.loggerDebug) {
				Global.logger.info(player + " cardlist:" + player.cardInhand);
			}
			Global.logger.info("红中麻将:"+player.playerid + ",player.black_white_rate:" +
			player.black_white_rate+",player.is_white:"+player.is_white+",room.white_value:"+owner.white_value);

			ITObject param = new TObject();
			ITArray handCard = Util.toTArray(player.cardInhand);
			param.putTArray("card_list", handCard);
			param.putInt("bank_seat", player.room.bankerSeat);
			param.putInt("round", player.room.round);
			player.sendEvent(Config.GAME_EVT_PLAYER_DEAL, param);
			player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		}

		Global.logger.info("dealCard() ===> round = "+owner.round);
	}
	
	public void tipEvent(EXPlayer owner) {
		ITObject param = owner.tipMgr.toMP();
		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,int win_card) {
		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("win_card", win_card);
		param.putInt("from_seat", from_seat);
		if(Global.loggerDebug) {
			Global.logger.info(owner + " hupai!["+(owner.seat == from_seat?"self":"other")+"]");
		}
		EXRoom room = owner.getRoom();
		room.broadCastToClient(0, Config.GAME_EVT_HU, param);
		((EXPlayBack)room.playBackData).addWinCardCommand(owner.seat, false);
	}
	
	public void sendNiaoEvt(EXPlayer owner, int seat) {
		EXRoom room = owner.getRoom();
		ITObject param = new TObject();
//		param.putInt("playerid", owner.playerid);
		ITArray array = new TArray();
		for (int index = 0; index < room.niao.size(); index++) {
			array.addTObject(room.niao.get(index).toMP());
		}
		param.putTArray("niao", array);
		param.putInt("start_seat", seat);

		room.broadCastToClient(0, Config.GAME_EVT_NIAO, param);
		((EXPlayBack)room.playBackData).addNiaoCommand(owner.seat, param);
	}
	
	/**
	 * Ʊ����ʾ
	 * @param owner
	 */
	public void piaoNiaoTipEvent(EXPlayer owner) {
		owner.sendEvent(Config.GAME_EVT_PIAONIAO_TIP, null);
	}
	
	/**
	 * Ʊ���¼�
	 * @param owner
	 */
	public void piaoNiaoEvent(EXPlayer owner) {
	//	if(owner.piaoNiao==0)return;
		ITObject param = new TObject();
		param.putInt("seat", owner.seat);
		param.putInt("num", owner.piaoNiao);
		owner.room.broadCastToClient(0, Config.GAME_EVT_PIAONIAO, param);
	}
	
	public ITObject getRoomResultData(EXRoom owner) {
		ITObject mp = TObject.newInstance();
		mp.putBoolean("liuju", owner.liuju);
		mp.putFloat("xipai_score", Float.parseFloat(owner.xi_pai_score + "")/100 );
//		mp.putInt("xipai_score", owner.xi_pai_score);
		mp.putInt("active_player", owner.playerMapBySeat.get(owner.activeSeat).playerid);
		long time = System.currentTimeMillis();
		long t = time / 1000;
		mp.putLong("time", t);
		ITArray niao = new TArray();
		for (int index = 0; index < owner.niao.size(); index++) {
			niao.addTObject(owner.niao.get(index).toMP());
		}
		mp.putTArray("niao", niao);
		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_score", player.score.round_log.get(EXScore.WIN));
			param.putInt("niao_score", player.score.round_log.get(EXScore.NIAO));
			param.putInt("gang_score", player.score.round_log.get(EXScore.KONG));
			param.putInt("piao_niao_score", player.score.round_log.get(EXScore.PIAO_NIAO));
			player.hp_info(param);
			param.putInt("round_score", player.score.round_score);
			param.putInt("total_score", player.score.total_score);
			ITArray handCard = Util.toTArray(player.cardInhand);
			param.putTArray("hand_card", handCard);
			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);
		owner.playBackData.addResult(result);
		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.putTObject("settle_log", player.settleLog.toTObject());
			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);
			owner.playBackData.addResult(result);
			mp.putTObject("result", result);
		}
		mp.putTObject("total_result", data);
		
		owner.broadCastToClient(0, Config.GAME_EVT_RESULT1, mp);
		
	}
}
