package extend.mj.player.rulestate;

import com.game.Global;
import com.game.Util;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import extend.mj.*;
import extend.mj.player.state.EXPlayerDiscardState;

/**
 * ��Ӧ���ƴ���״̬
 * 
 *
 * 2017��8��30��
 * PRPongState.java
 */
public class PRPongState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		// TODO Auto-generated method stub
		EXRoom room = (EXRoom) owner.room;
		EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(owner.room.activeSeat);
		activePlayer.outcardList.remove(activePlayer.outcardList.size() - 1);
		activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		int fromseat = activePlayer.seat;

		EXMainServer.gameCtr.changeActiveSeat(room, owner.seat);
		owner.cardInhand.add(room.activeCard);

		int[] pongGroup = new int[3];
		int card = room.activeCard;
		OpCard pongCard = new OpCard(RuleWeight.TYPE_PONG, card, activePlayer.seat);
		pongCard.opCard = new TArray();
		if (Util.checkCard(card, owner.cardInhand, 3)) {
			pongGroup[0] = card;
			pongGroup[1] = card;
			pongGroup[2] = card;
			owner.pongGroup.add(pongGroup);
			Util.removeCard(owner.cardInhand, card, 3);

			room.activeCard = 0;
			room.lastDiscardSeat = 0;


			pongCard.opCard.addInt(card);
			pongCard.opCard.addInt(card);
			pongCard.opCard.addInt(card);
			owner.opCardList.add(pongCard);

			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			opcard.addInt(card);
			EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_PONG, fromseat,opcard);
		}
		else if (Util.checkCard(card, owner.cardInhand, 2) && Util.checkCard(card / 100 * 100 + 4, owner.cardInhand, 1)) {
			pongGroup[0] = card;
			pongGroup[1] = card;
			pongGroup[2] = card / 100 * 100 + 4;
			owner.pongGroup.add(pongGroup);
			Util.removeCard(owner.cardInhand, card, 2);
			Util.removeCard(owner.cardInhand, card / 100 * 100 + 4, 1);

			room.activeCard = 0;
			room.lastDiscardSeat = 0;


			pongCard.opCard.addInt(card);
			pongCard.opCard.addInt(card);
			pongCard.opCard.addInt(card / 100 * 100 + 4);
			owner.opCardList.add(pongCard);

			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			opcard.addInt(card / 100 * 100 + 4);
			EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_PONG, fromseat,opcard);
		}

		if(!EXMainServer.playerRuleMgr.condition(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, owner,false)){
			toNextState(owner);
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.drawCard = 0;
		owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
	}

}
