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 com.taurus.permanent.TPServer;
import extend.zp.*;
import extend.zp.player.state.EXPlayerDiscardState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.uitl.Paixing;

import java.util.concurrent.TimeUnit;

/**
 * ��Ӧ���ƴ���״̬
 * 
 * 
 *
 *         2017��8��30�� PRPongState.java
 */
public class PRWeiState extends StateBase<EXPlayer> {

	Logger logger = Logger.getLogger(getClass());

	@Override
	public void enter(EXPlayer owner) {
		// TODO Auto-generated method stub
		owner.isDiHu = false;
		logger.info("jefe weistate :" +owner.getRoom().config.getInt(Config.ROOM_CONFIG_KA_WAI));
		if (owner.getRoom().config.getInt(Config.ROOM_CONFIG_KA_WAI) != 0)
		{
			handleWai(owner);
		}
		else {
			//handleWai(owner);
			timer(owner, this);
		}
	}

	public static void timer(EXPlayer owner, PRWeiState state) {
		TPServer.me().getTimerPool().schedule(new Runnable() {
			@Override
			public void run() {
				if (owner.room.isDestroy)
					return;
				owner.room.enqueueRunnable(new Runnable() {
					@Override
					public void run() {
						state.handleWai(owner);
					}
				});
			}
		}, 1, TimeUnit.SECONDS);
	}

	public void handleWai(EXPlayer owner)
	{
		int card = owner.opCard.getInt(0);
		int kan = owner.opCard.getInt(2);
		int[] weiGroup = new int[3];
		weiGroup[0] = card;
		weiGroup[1] = card;
		weiGroup[2] = card;

		owner.weiGroup.add(weiGroup);

		int type = owner.chouPongSet.contains(card) ? RuleWeight.TYPE_CHOUWEI : RuleWeight.TYPE_WEI;
		if (kan == 0)
		{
			boolean firstChi = false;
			for (int i = 0; i < owner.chowGroup.size(); i++) {
				//if (card == owner.chowGroup.get(i)[0] || card == owner.chowGroup.get(i)[1] || card == owner.chowGroup.get(i)[2]) {
				if ( card == owner.chowGroup.get(i)[1]) {
					firstChi = true;
					break;
				}
			}
			//先吃后歪，算外毫
			if (firstChi)
			{
				owner.opCardList.add(new OpCard(type, card, card, card, Paixing.caclHuxi(type, card), false, RuleWeight.HAO_WAI, card));
			}
			else
			{
				owner.opCardList.add(new OpCard(type, card, card, card, Paixing.caclHuxi(type, card)));
			}
		}
		else
		{
			owner.opCardList.add(new OpCard(type, card, card, card, Paixing.caclHuxi(type, card), false, RuleWeight.HAO_NUI, card));
		}

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

		EXMainServer.gameCtr.actionCard(owner, card, card, type, owner.seat, opcard);
		EXMainServer.playerRuleMgr.condition(PlayerRuleManager.WAI_LIU_RULE, owner);

		owner.handleDeadPut();
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.isTingHu = false;
		if (owner.getRoom().card.getCount() == 0) {
			owner.getRoom().liuju = true;
			owner.getRoom().liujuNum++;
			owner.getRoom().endGame();
			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

	}
}
