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.Config;
import extend.zp.EXActionEvent;
import extend.zp.EXGameController;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.PlayerRuleManager;
import extend.zp.uitl.Paixing;


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);
		owner.room.broadCastToClient(owner.playerid, Config.GAME_EVT_DRAW, otherParam);
		timer(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		if (!owner.getRoom().isAllPass) {
			//判断是否可以进行地胡
			////System.out.println("owner seat:"+owner.seat);
			////System.out.println("owner id:"+owner.playerid);
			////System.out.println("beakseat:"+owner.getRoom().bankerSeat);
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.HU_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;
			}
		}
		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DEAL_RULE, owner);
		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.KAN_RULE, owner);
		//EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.OTHER_HU_RULE, owner);//点炮

		int drawCard = owner.drawCard;
		owner.drawCard = 0;
		owner.getRoom().isTianHu = false;
		////System.out.println("istianhu "+owner.getRoom().isTianHu);
		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);
		} else {
			owner.getRoom().getPlayBack().addAddCardCommand(owner.room.activeSeat, owner.getRoom().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() {
						boolean win = false;
						for (Entry<Integer, Player> entry : owner.getRoom().playerMapById.entrySet()) {
							if (entry.getKey().equals(owner.playerid)) {
								continue;
							}
							EXPlayer player = (EXPlayer) entry.getValue();
							if(Paixing.checkWin(player, owner.drawCard)) {
								win = true;
							}
						}
						if(!win)
							EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DEAL_RULE, owner);
						owner.stateMachine.toNextState();
					}
				});
			}
		}, 1500, TimeUnit.MILLISECONDS);
	}
}
