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.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 PRTiState extends StateBase<EXPlayer> {
	@Override
	public void enter(EXPlayer owner) {
		owner.kongCount++;

		int card = owner.opCard.getInt(0);
		int[] tiGroup = new int[4];
		tiGroup[0] = card;
		tiGroup[1] = card;
		tiGroup[2] = card;
		tiGroup[3] = card;

		owner.tiGroup.add(tiGroup);
		owner.opCardList.add(new OpCard(RuleWeight.TYPE_TI, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_TI, card)));

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

		EXMainServer.gameCtr.actionCard(owner, card, card, RuleWeight.TYPE_TI, owner.seat, opcard);
		EXMainServer.playerRuleMgr.condition(PlayerRuleManager.HU_RULE, owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.isTingHu = false;
		if (owner.getRoom().card.getCount() == 0) {
			owner.getRoom().liuju = true;
			owner.getRoom().liujuNum++;
			owner.getRoom().endGame();
			return;
		}
		int count = owner.tiCount + owner.kongCount;
		if (count == 1) {
			owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
		} else if (count == 2) {
			if (owner.tiCount == 2) {
				owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
			} else {
				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);
	}
}
