package extend.zp.player.state;

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

import com.game.Global;
import com.game.data.Player;
import com.game.player.state.PlayerPauseState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.permanent.TPServer;

import extend.zp.*;


public class EXPlayerPreDrawState extends StateBase<EXPlayer> {
	@Override
	public void enter(EXPlayer owner) {
		
		EXRoom room = owner.getRoom();
		
		room.isAllPass = false;


		//owner.drawCard = room.card.pop();

		//room.drawCard = owner.drawCard;
		
		//发消息给自己，并记录玩家抓牌的日志
		int leftCount = room.card.getCount();
		//((EXGameController) Global.gameCtr).getCard(owner, leftCount);
		
		//通知所有其他的玩家
		ITObject otherParam = new TObject();
		otherParam.putInt("card", owner.drawCard);
		otherParam.putInt("seat", owner.room.activeSeat);
	 	otherParam.putInt("left_count", leftCount);
		//jefe
		for (Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			//owner.room.broadCastToClient(player.playerid, Config.GAME_EVT_DRAW, otherParam);
			//player.sendEvent(Config.GAME_EVT_DRAW, otherParam);
			if (player.room.bankerSeat==player.seat){
				//owner.room.broadCastToClient(owner.playerid, Config.GAME_EVT_ACTION, otherParam);
			}
			player.sendEvent(Config.GAME_EVT_DRAW, otherParam);
		}
		//owner.room.broadCastToClient(owner.playerid, Config.GAME_EVT_DRAW, otherParam);

		
		//启动定时器
		timer(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {

		if (!owner.getRoom().isAllPass) {
			//根据规则判定自己能不能胡
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.HU_LIU_RULE, owner);
			//看看其他玩家能不能胡
			owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_DEAL, null);

			// 如果有玩家能胡
			if (owner.getRoom().tipMap.size() != 0) {
				if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
					owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
				}
				return;
			}
		}

		owner.getRoom().isTianHu = false;

		//判断玩家抓牌之后可以进行的操作
		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DEAL_RULE, owner);
		
		//判断玩家是否有坎的操作
		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.KAN_RULE, owner);

		int drawCard = owner.drawCard;
		owner.drawCard = 0;

		if (drawCard != 0) {
			
			owner.cardInhand.add(drawCard);
			Collections.sort(owner.cardInhand);

			ITObject param = new TObject();
			param.putInt("card", drawCard);
			param.putInt("seat", owner.room.activeSeat);
			owner.room.broadCastToClient(0, Config.GAME_EVT_ADD_CARD, param);
			
			owner.getRoom().getPlayBack().addAddCardCommand(owner.room.activeSeat, drawCard);
		}

		owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
	}

	public static void timer(EXPlayer owner) {
		
		TPServer.me().getTimerPool().schedule(new Runnable() {
			
			@Override
			public void run() {
				
				if (owner.room == null) {
					Global.logger.error("EXPlayerDiscardState room is null");
					return;
				}
				
				if (owner.room.isDestroy)
					return;
				
				owner.room.enqueueRunnable(new Runnable() {
					
					@Override
					public void run() {

						EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DEAL_RULE, owner);

						owner.stateMachine.toNextState();
					}
				});
			}
		}, 1500, TimeUnit.MILLISECONDS);
	}
}
