package extend.zp.player.rulestate;

import com.game.Global;
import com.game.player.state.PlayerPauseState;
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.core.util.Logger;
import extend.zp.*;
import extend.zp.player.state.EXPlayerDiscardState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;

/**
 * ��Ӧ���ƴ���״̬
 * 
 * 
 *
 *         2017��8��30�� PRPongState.java
 */
public class PRPongState extends StateBase<EXPlayer> {
	Logger logger = Logger.getLogger(getClass());
	@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.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		int fromseat = activePlayer.seat;
		owner.isDiHu = false;
		EXMainServer.gameCtr.changeActiveSeat(room, owner.seat);

		int[] pongGroup = new int[3];
		int card = room.activeCard;
		pongGroup[0] = card;
		pongGroup[1] = card;
		pongGroup[2] = card;

		int haoCard = 0;
		if (CardUtil.cardNum(card, owner.cardInhand) >= 2) {
			CardUtil.removeCard(owner.cardInhand, card, 2);
			for (int i = 0; i < owner.chowGroup.size(); i++) {
				//if (owner.chowGroup.get(i)[0] == card || owner.chowGroup.get(i)[1] == card || owner.chowGroup.get(i)[2] == card)
				if ( owner.chowGroup.get(i)[1] == card )
				{
					haoCard = card;
					break;
				}
			}
        }
		else
		{
			for (int i = 0; i < owner.kanGroup.size(); i++) {
				if (card == owner.kanGroup.get(i)[0]) {
					owner.kanGroup.remove(i);
					CardUtil.removeOpcard(owner.opCardList, new OpCard(RuleWeight.TYPE_KAN, card, card, card, 0));
					owner.cardInhand.add(card);
					owner.notPutSet.add(card);
					haoCard = card;
					break;
				}
			}
		}
		room.activeCard = 0;
		room.lastDiscardSeat = 0;

		owner.pongGroup.add(pongGroup);
		owner.isTingHu = false;
		if (haoCard == 0)
		{
			owner.opCardList.add(new OpCard(RuleWeight.TYPE_PONG, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_PONG, card)));
		}
		else
		{
			owner.opCardList.add(new OpCard(RuleWeight.TYPE_PONG, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_PONG, card), false, RuleWeight.HAO_WAI, haoCard));
		}

		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);

		EXMainServer.gameCtr.actionCard(owner, card, card, RuleWeight.TYPE_PONG, fromseat, opcard);
		logger.info("jefe PRPongState: enter" );
		owner.handleDeadPut();

		this.toNextState(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		//if (!owner.getRoom().isAllPass) {
		//logger.info("jefe PRPongState:"+owner.tiGroup.size()+" isguoliu:"+owner.isGuoliu );
		//	logger.info("jefe PRPongState:"+owner.weiGroup.size()+" isguoliu:"+owner.kanGroup.size()+"liuGroup:");
		//logger.info("jefe PRPongState:"+owner.weiGroup.get(0)[0]+" isguoliu:"+owner.kanGroup.get(0)[0]+"wei:" +owner.weiGroup.get(1)[0]);
		boolean flags = false;
		for (int i = 0; i < owner.weiGroup.size(); i++) {
			for (int j = 0; j < owner.cardInhand.size(); j++) {
				//logger.info("jefe PRPongState:"+owner.cardInhand.get(j) );
				if (owner.cardInhand.get(j) == owner.weiGroup.get(i)[0]) {
					flags =  true;
				}
			}
		}

		if (owner.tiGroup.size()>0&&owner.isGuoliu==false) {
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.WAI_LIU_RULE, owner);
			owner.isGuoliu = true;
		}
		//判断是否可以溜
		if(flags&&owner.isGuoliu==false){
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.WAI_LIU_RULE, owner);
			owner.isGuoliu = true;
		}

		// 如果有玩家能胡
		if (owner.getRoom().tipMap.size() != 0) {
			if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
				owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
			}
			return;
		}
		//}
		owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
	}

	@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

	}
}
