package extend.zp.player.state;

import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.player.state.PlayerPauseState;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.util.Logger;
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.room.state.EXRoomSetpState;
import extend.zp.uitl.CardUtil;


public class EXPlayerDrawState extends StateBase<EXPlayer> {
	Logger logger = Logger.getLogger(getClass());
	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		if (owner.getRoom().card.getCount() == 0) {
			owner.getRoom().liuju = true;
			owner.getRoom().liujuNum++;
			owner.getRoom().endGame();
			return;
		}
		room.isAllPass = false;
		owner.getRoom().tipMap.clear();
		owner.drawCard = room.card.pop();
		
		room.step++;
		int tempStep = room.step;
		// if (owner.seat == 2) {
		// owner.drawCard = 102;
		// }
		logger.info("drawCard,player:" + owner + ",card:" + owner.drawCard);
		room.drawCard = owner.drawCard;

		int leftCount = room.card.getCount();
		((EXGameController) Global.gameCtr).getCard(owner, leftCount);

		int card = owner.drawCard;
		for (int i = 0; i < owner.kanGroup.size(); i++) {
			if (owner.drawCard == owner.kanGroup.get(i)[0]) {
				card = 0;
				break;
			}
		}
		for (int i = 0; i < owner.weiGroup.size(); i++) {
			if (owner.drawCard == owner.weiGroup.get(i)[0]) {
				card = 0;
				break;
			}
		}
		if (CardUtil.cardNum(owner.drawCard, owner.cardInhand) >= 2) {
			card = 0;
		}
		ITObject otherParam = new TObject();
		otherParam.putInt("card", card);
		otherParam.putInt("seat", owner.room.activeSeat);
		otherParam.putInt("left_count", leftCount);
		owner.room.broadCastToClient(owner.playerid, Config.GAME_EVT_DRAW, otherParam);

		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.TI_RULE, owner);
		if (owner.drawCard != 0 && tempStep == room.step) {
			this.toNextState(owner);
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		if (!owner.getRoom().isAllPass) {
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DRAW_RULE, owner);
			owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_OTHER_DRAW, null);
			if (owner.getRoom().tipMap.size() != 0) {
				if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
					owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
				}
				return;
			}
		}
		
		int tempStep = owner.getRoom().step;

		EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DRAW_PAO_RULE, owner);
		owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_OTHER_PAO, null);

		if (owner.drawCard != 0 && tempStep == owner.getRoom().step) {

			timer(owner);
		}
	}

	public static void timer(EXPlayer owner) {
		if (owner.getRoom().card.getCount() == 0) {
			owner.getRoom().liuju = true;
			owner.getRoom().liujuNum++;
			owner.getRoom().endGame();
			return;
		}
		if (owner.getRoom().isAllPass) {
			ITObject param = new TObject();
			param.putInt("card", owner.drawCard);
			param.putInt("seat", owner.seat);
			owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, param);
			owner.outcardList.add(owner.drawCard);
			owner.getRoom().throwCardsSet.add(owner.drawCard);
			owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, owner.drawCard);

			owner.drawCard = 0;
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
		} else {
			TPServer.me().getTimerPool().schedule(new Runnable() {
				@Override
				public void run() {
					if (owner.room == null) {
						Global.logger.error("EXPlayerDrawState room is null");
						return;
					}
					if (owner.room.isDestroy)
						return;
					owner.room.enqueueRunnable(new Runnable() {
						@Override
						public void run() {
							if(owner.drawCard != 0) {
								ITObject param = new TObject();
								param.putInt("card", owner.drawCard);
								param.putInt("seat", owner.seat);
								owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, param);
								owner.outcardList.add(owner.drawCard);
								owner.getRoom().throwCardsSet.add(owner.drawCard);
								owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, owner.drawCard);
	
								owner.drawCard = 0;
								owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
								owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
							}
						}
					});
				}
			}, 1, TimeUnit.SECONDS);
		}
	}
}
