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.util.Logger;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.PlayerRuleManager;
import extend.zp.player.state.EXPlayerDiscardState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.room.state.EXRoomSetpState;

public class PRDrawChiState extends StateBase<EXPlayer> {
	Logger logger = Logger.getLogger(getClass());

	@Override
	public void enter(EXPlayer owner) {
		owner.biCard = 0;
		owner.isTingHu = false;
		owner.isDiHu = false;
		owner.handleDeadPut();
		logger.info("jefe PRDrawChiState: enter" );
		this.toNextState(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		logger.info("jefe PRDrawChiState");
		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);
			if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
				owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
			}
			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));
	}

	public int duplicate(Integer[] numbers) {
		Integer[] res = new Integer[numbers.length];
		for (int i=0;i<res.length;i++){
			int f = 0;
			for (int j=0;j<res.length;j++){
				if (res[i]==res[j]){
					f++;
				}
			}
			if (f==4){
				return 1;
			}
		}
		return -1;
	}

	@Override
	public void execute(EXPlayer owner, String cmd, int gid, Object param) {
		// TODO Auto-generated method stub

	}
}
