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

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

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

	@Override
	public boolean condition(EXPlayer player) {
		if (player.isEntrust()) {
			return  false;
		}
		Map<Integer, Integer> cardMap = Util.getCardNumMap(player.cardInhand);
		boolean result = false;

		for (Entry<Integer, Integer> entry : cardMap.entrySet()) {

			int card = entry.getKey();

			int num = entry.getValue();
			if (player.getRoom().isLaizi(card))
			{
				continue;
			}
			if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
			{
				if (player.notPongKongList.contains(card))
				{
					continue;
				}
			}
			if (player.getRoom().isFanPai(card))
			{
				if (num >= 3)
				{
					ITArray opcard = TArray.newInstance();
					opcard.addInt(card);
					Tip tip = new Tip(card,opcard, RuleWeight.SELFKONG,this, RuleWeight.TYPE_SELF_KONG);
					player.tipMgr.addTip(tip);
					result = true;
					if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
					{
						player.notPongKongList.add(card);
					}
				}
			}
			else {
				if (num >= 4) {
					ITArray opcard = TArray.newInstance();
					opcard.addInt(card);
					Tip tip = new Tip(card,opcard, RuleWeight.SELFKONG,this, RuleWeight.TYPE_SELF_KONG);
					player.tipMgr.addTip(tip);
					result = true;
					if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
					{
						player.notPongKongList.add(card);
					}
				}
			}
		}
		return result;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
		{
			Util.removeCard(player.notPongKongList, tip.card, 4);
		}
		player.stateMachine.changeState(Global.getState(PRSelfKongState.class));

	}
}
