package extend.zp.player.state;

import java.util.concurrent.TimeUnit;

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 com.taurus.permanent.TPServer;

import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.OpCard;
import extend.zp.PlayerRuleManager;
import extend.zp.RuleWeight;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.uitl.CardUtil;


public class EXPlayerWeiState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		boolean pass = owner.stateMachine.lastState instanceof EXPlayerDisCardTipState;
		if(pass) return;
		EXRoom room = owner.getRoom();
		
		int fromseat = owner.seat;
		int card = room.showCard;
		owner.actionCard = card;
		Util.removeCard(owner.cardInhand, card, 2);
		if (owner.suoCardList.contains(card)) 
			owner.suoCardList.remove(Integer.valueOf(card));
		room.showCard = 0;
		
		ITArray opcard = TArray.newInstance();
		opcard.addInt(card);
		opcard.addInt(card);
		
		//boolean chou = owner.chouList.contains(card);
		int type = RuleWeight.TYPE_WEI;
		OpCard opc = new OpCard(type, opcard, card);
		opc.huXi = CardUtil.getCardGroupHuxi(card, type, owner.opCardList);
		owner.opCardList.add(opc);
		
		
		EXMainServer.gameCtr.actionCard(owner, card, type, fromseat,opcard,opc.huXi, null);
		owner.kaWei = true;
		EXMainServer.playerRuleMgr.condition(PlayerRuleManager.WIN_RULE, owner);
		//timer(owner);
	}

	
	@Override
	public void toNextState(EXPlayer owner) {
		owner.actionCard = 0;
		owner.kaWei = false;
		if(CardUtil.checkHandKan(owner.cardInhand)) {
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
			return;
		}
		owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
	}
	
	public static void timer(EXPlayer owner) {
		TPServer.me().getTimerPool().schedule(new Runnable() {
			@Override
			public void run() {
				if (owner.room == null) {
					Global.logger.error("EXPlayerDiscardState room is null");
					return;
				}
				if (owner.room.isDestroy)
					return;
				owner.room.enqueueRunnable(new Runnable() {
					@Override
					public void run() {
						EXMainServer.playerRuleMgr.condition(PlayerRuleManager.WIN_RULE, owner);
					}
				});
			}
		}, 900, TimeUnit.MILLISECONDS);
	}
}
