package extend.mj.player.rulestate;

import java.util.Map.Entry;

import com.game.Global;
import com.game.Util;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;

import extend.mj.EXActionEvent;
import extend.mj.EXMainServer;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.OpCard;
import extend.mj.RuleWeight;
import extend.mj.player.state.EXPlayerDrawState;
import extend.mj.player.state.EXPlayerWaitKongWinState;
import extend.mj.tip.TipManager;
import extend.mj.uitl.CardUtil;

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

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		ITArray opCard = owner.opCard;
		int card = opCard.getInt(0);

		Util.removeCard(owner.cardInhand, card, 1);
		OpCard opc = CardUtil.findOpcard(owner.opCardList, card,RuleWeight.TYPE_PONG);
		opc.type = RuleWeight.TYPE_KONG;
		
		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_PONG_KONG, owner.seat,opcard,false);
		
		room.activeCard = card;
		owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_KONG_WIN, null);
		room.activeCard = 0;
		if(room.tipMap.size() == 0){
			toNextState(owner);
		}else{
			int win_count = 0;
			for (Entry<Integer, TipManager> entry : room.tipMap.entrySet()) {
				TipManager tipmgr = entry.getValue();
				if(tipmgr.isWin)win_count++;
			}
			room.tongpao = 0;
			if( win_count>1)room.tongpao = win_count;
			owner.stateMachine.changeState(Global.getState(EXPlayerWaitKongWinState.class));
		}
	}

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