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.OpenKong;
import extend.mj.RuleWeight;
import extend.mj.player.state.EXPlayerOpenKongState;

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

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		EXPlayer activePlayer =  (EXPlayer)room.playerMapBySeat.get(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);
		}
		if(activePlayer!=owner) {
			activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		}
		int fromseat  = activePlayer.seat;
		OpenKong ok = new OpenKong();
		ok.fromSeat = fromseat;
		ok.kongSeat = owner.seat;
		owner.openKong = ok;
		EXMainServer.gameCtr.changeActiveSeat(room,owner.seat);
		
		int card = room.activeCard;
		OpCard opc = new OpCard(RuleWeight.TYPE_KONG, card);
		owner.opCardList.add(opc);
		
		Util.removeCard(owner.cardInhand, card, 3);
		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);
		opcard.addInt(card);
		EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_KONG, fromseat,opcard,true);
		room.activeCard = 0;
		room.lastDiscardSeat = 0;
		toNextState(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.getRoom().activeKong = owner.openKong;
		owner.openKong=null;
		owner.stateMachine.changeState(Global.getState(EXPlayerOpenKongState.class));	
	}

}
