package extend.mj.player.rule;

import java.util.ArrayList;
import java.util.List;

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.OpCard;
import extend.mj.RuleWeight;
import extend.mj.player.rulestate.PRPongOpenKongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.Paixing;

/**
 * ���ܿ��ܼ��
 * 
 *
 * 2017��8��30��
 * RulePongKong.java
 */
public class RulePongOpenKong implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		EXRoom room = player.getRoom();
		if (room.activeHaidi) {
			return false;
		}
		if(player.tianting)return false;
		
		boolean result = false;
		if(room.activeKong!=null){
			if(room.activeKong.kongSeat != player.seat)return false;
			if(room.activeKong.haidi)return false;
			for (int card : room.activeKong.drawList) {
				result = check(player,card,false) || result;
			}
		}else{
			List<Integer> cardList = new ArrayList<>();
			cardList.addAll(player.cardInhand);
			if(player.autoOutCard) {
				int card = player.drawCard;
				result = check(player, card,true);
			}else {
				for (int card : cardList) {
					result = check(player, card,true) || result;
				}
			}
			
		}
		return result;
	}

	private boolean check(EXPlayer player, int card,boolean remove) {
		boolean no_jiang = player.getRoom().config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
		for (OpCard opc : player.opCardList) {
			if (opc.type == RuleWeight.TYPE_PONG && card == opc.card) {
				if(remove)Util.removeCard(player.cardInhand, card, 1);
				try {
					if(Paixing.tingKongCheck(player.opCardList,player.cardInhand,card,!no_jiang)) {
						ITArray opcard = TArray.newInstance();
						opcard.addInt(card);
						Tip tip = new Tip(card,opcard, RuleWeight.SELF_KONG,this, RuleWeight.TYPE_PONG_KONG);
						tip.group_type = RuleWeight.GROUO_OPENKONG;
						player.tipMgr.addTip(tip);
						return true;
					}
				}finally {
					if(remove)Util.addCard(player.cardInhand, card, 1);
				}
			}
		}
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.stateMachine.changeState(Global.getState(PRPongOpenKongState.class));

	}

}
