package extend.mj.player.rule;

import java.util.Map;
import java.util.Map.Entry;

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

/**
 * ���ܿ��ܼ��
 * 
 *
 * 2017��8��30��
 * RuleSelfKong.java
 */
public class RuleSelfOpenKong implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		if(player.tianting)return false;
		if (player.getRoom().activeHaidi) {
			return false;
		}
		boolean no_jiang = player.getRoom().config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
		boolean result = false;
		if(player.autoOutCard) {
			int card = player.drawCard;
			if(Util.checkCardAndRomve(card, player.cardInhand,4)) {
				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_SELF_KONG);
					tip.group_type = RuleWeight.GROUO_OPENKONG;
					player.tipMgr.addTip(tip);
					result = true;
				}
				Util.addCard(player.cardInhand, card, 4);
			}
		}else {
			Map<Integer, Integer> cardMap = Util.getCardNumMap(player.cardInhand);
			for (Entry<Integer, Integer> entry : cardMap.entrySet()) {
				int card = entry.getKey();
				int num = entry.getValue();
				if (num >= 4) {
					Util.removeCard(player.cardInhand, card, 4);
					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_SELF_KONG);
						tip.group_type = RuleWeight.GROUO_OPENKONG;
						player.tipMgr.addTip(tip);
						result = true;
					}
					Util.addCard(player.cardInhand, card, 4);
				}
			}
		}
		return result;
	}

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

	}
}
