package extend.zp.player.rulestate;

import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import com.taurus.permanent.TPServer;

import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.OpCard;
import extend.zp.PlayerRuleManager;
import extend.zp.RuleWeight;
import extend.zp.player.state.EXPlayerDiscardState;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.uitl.Paixing;


public class PRDrawPaoState extends StateBase<EXPlayer> {
	@Override
	public void enter(EXPlayer owner) {

		EXRoom room = owner.getRoom();
		EXPlayer activePlayer = (EXPlayer) room.playerMapBySeat.get(owner.room.activeSeat);
		if (owner.seat != activePlayer.seat) {
			activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			EXMainServer.gameCtr.changeActiveSeat(room, owner.seat);
		}

		owner.kongCount++;
		int fromseat = activePlayer.seat;
		int card = owner.opCard.getInt(0);
		int[] paoGroup = new int[4];
		paoGroup[0] = card;
		paoGroup[1] = card;
		paoGroup[2] = card;
		paoGroup[3] = card;

		owner.paoGroup.add(paoGroup);
		owner.opCardList.add(new OpCard(RuleWeight.TYPE_PAO, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_PAO, card)));

		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);
		opcard.addInt(card);

		EXMainServer.gameCtr.actionCard(owner, card, card, RuleWeight.TYPE_PAO, fromseat, opcard);
		int count = owner.tiCount + owner.kongCount;
		if (count<2&&!owner.guohupao){
			EXMainServer.playerRuleMgr.condition(PlayerRuleManager.HU_RULE, owner);
		}else{
			this.toNextState(owner);
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.isTingHu = false;
		owner.guohupao = false;//重置
		if (owner.getRoom().card.getCount() == 0) {
			owner.getRoom().liuju = true;
			owner.getRoom().liujuNum++;
			owner.getRoom().endGame();
			return;
		}
		int count = owner.tiCount + owner.kongCount;
		owner.paopai_count = owner.paopai_count + 1;
		owner.paoqiflag =true;

		if (count == 1) {
			//判断是否能胡牌 1跑出牌
			owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));

		} else if (count == 2) {
			//二跑 不出牌
			timer(owner);
		} else {
			timer(owner);
		}
	}

	public static void timer(EXPlayer owner) {
		TPServer.me().getTimerPool().schedule(new Runnable() {
			@Override
			public void run() {
				if (owner.room.isDestroy)
					return;
				owner.room.enqueueRunnable(new Runnable() {
					@Override
					public void run() {
						owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
						owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
					}
				});
			}
		}, 2, TimeUnit.SECONDS);
	}

	@Override
	public void exit(EXPlayer owner) {
		// TODO Auto-generated method stub
	}

	@Override
	public void execute(EXPlayer owner, String cmd, int gid, Object param) {
		// TODO Auto-generated method stub
	}
}
