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.RuleWeight;
import extend.zp.player.state.EXPlayerDiscardState;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.uitl.Paixing;


public class PRDiscardPaoState extends StateBase<EXPlayer> {
	@Override
	public void enter(EXPlayer owner) {
		owner.kongCount++;
		owner.isDiHu = false;
		EXRoom room = owner.getRoom();
		EXPlayer activePlayer = (EXPlayer) room.playerMapBySeat.get(owner.room.activeSeat);
		activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));

		int fromseat = activePlayer.seat;
		EXMainServer.gameCtr.changeActiveSeat(room, owner.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)));

		room.activeCard = 0;
		room.lastDiscardSeat = 0;
		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);
		opcard.addInt(card);

		EXMainServer.gameCtr.actionCard(owner, card, card, RuleWeight.TYPE_PAO, fromseat, opcard);
		this.toNextState(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.isTingHu = false;
		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);
	}

	@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
	}
}
