package extend.mj.player.rule;

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

import extend.mj.Config;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.RuleWeight;
import extend.mj.player.rulestate.PROtherOpenKongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.Paixing;

/**
 * �����ܼ��
 * 
 *
 * 2017��8��30��
 * RuleOtherKong.java
 */
public class RuleOtherOpenKong implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		if(player.tianting)return false;
		EXRoom room = player.getRoom();
		if (room.activeHaidi) {
			return false;
		}
		
		boolean result = false;
		if(room.activeKong!=null){
			if(room.activeKong.haidi)return false;
			for (int card : room.activeKong.drawList) {
				result = check(player,card) || result;
			}
		}else{
			int eventCard = room.activeCard;
			result = check(player,eventCard);
		}
		
		return result;
	}
	
	private boolean check(EXPlayer player,int eventCard){
		
		if (Util.checkCardAndRomve(eventCard, player.cardInhand,3)) {
			boolean no_jiang = player.getRoom().config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
			try {
				if(Paixing.tingKongCheck(player.opCardList,player.cardInhand,eventCard,!no_jiang)) {
					ITArray opcard = TArray.newInstance();
					opcard.addInt(eventCard);
					Tip tip = new Tip(eventCard,opcard, RuleWeight.KONG,this, RuleWeight.TYPE_KONG);
					tip.group_type = RuleWeight.GROUO_OPENKONG;
					player.tipMgr.addTip(tip);
					return true;
				}
			}finally {
				Util.addCard(player.cardInhand, eventCard, 3);
			}
		}
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.getRoom().activeKong = null;
		player.getRoom().activeCard = tip.card;
		player.stateMachine.changeState(Global.getState(PROtherOpenKongState.class));

	}

}
