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.EXMainServer;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.OpCard;
import extend.mj.PlayerRuleManager;
import extend.mj.RuleWeight;
import extend.mj.player.state.EXPlayerDiscardState;

/**
 * 
 */
public class PRPongState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = (EXRoom) owner.room;

		EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(owner.room.activeSeat);
		if(activePlayer.outcardList.get(activePlayer.outcardList.size() - 1)!=room.activeCard){
			activePlayer.outcardList.remove(activePlayer.outcardList.size() - 2);
		}else{
			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 card = room.activeCard;
		Util.removeCard(owner.cardInhand, card, 3);
		room.activeCard = 0;
		room.lastDiscardSeat = 0;
		
		owner.opCardList.add(new OpCard(RuleWeight.TYPE_PONG, card));
		
		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);
		EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_PONG, fromseat,opcard,false);
		
		if(!EXMainServer.playerRuleMgr.condition(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, owner,false)){
			owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
		}
	}

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