package extend.mj.player.rule;

import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import extend.mj.*;
import extend.mj.player.rulestate.PRSelfKongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;

import java.util.ArrayList;
import java.util.List;
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;
		}

		if (player.is_gang)
		{
			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 (num >= 4) {
				if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
				{
					if (player.notPongKongList.contains(card))
					{
						continue;
					}
				}

				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				player.is_gang = false;
				List<Integer> delPai = new ArrayList<>();
				delPai.add(card);
				delPai.add(card);
				delPai.add(card);
				delPai.add(card);
				List<OpCard> opCards = new ArrayList<>();
				opCards.addAll(player.opCardList);
				OpCard pongCard = new OpCard(RuleWeight.TYPE_KONG, card);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				opCards.add(pongCard);
				if (((EXGameController) Global.gameCtr).isTingPai(player, 0, delPai, opCards))
				{
					Tip tip = new Tip(card,opcard, RuleWeight.SELFKONG,this, RuleWeight.TYPE_SELF_KONG);
					player.tipMgr.addTip(tip);
					Tip tip2 = new Tip(card,opcard, RuleWeight.SELFZHAO,this, RuleWeight.TYPE_SELF_ZHAO);
					player.tipMgr.addTip(tip2);
				}
				else {
					Tip tip = new Tip(card,opcard, RuleWeight.SELFZHAO,this, RuleWeight.TYPE_SELF_ZHAO);
					player.tipMgr.addTip(tip);
				}

				result = true;

				if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
				{
					player.notPongKongList.add(card);
				}
			}
			else if (num == 3 && Util.checkCard(card /100 * 100 + 4, player.cardInhand, 1)) {
				if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
				{
					if (player.notPongKongList.contains(card))
					{
						continue;
					}
				}

				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				opcard.addInt(card /100 * 100 + 4);
				player.is_gang = false;

				List<Integer> delPai = new ArrayList<>();
				delPai.add(card);
				delPai.add(card);
				delPai.add(card);
				delPai.add(card /100 * 100 + 4);
				List<OpCard> opCards = new ArrayList<>();
				opCards.addAll(player.opCardList);
				OpCard pongCard = new OpCard(RuleWeight.TYPE_KONG, card);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card /100 * 100 + 4);
				opCards.add(pongCard);
				if (((EXGameController) Global.gameCtr).isTingPai(player, 0, delPai, opCards))
				{
					Tip tip = new Tip(card,opcard, RuleWeight.SELFKONG,this, RuleWeight.TYPE_SELF_KONG);
					player.tipMgr.addTip(tip);
					Tip tip2 = new Tip(card,opcard, RuleWeight.SELFZHAO,this, RuleWeight.TYPE_SELF_ZHAO);
					player.tipMgr.addTip(tip2);
				}
				else {
					Tip tip = new Tip(card,opcard, RuleWeight.SELFZHAO,this, RuleWeight.TYPE_SELF_ZHAO);
					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);
		}
		if (tip.type == RuleWeight.TYPE_SELF_KONG)
		{
			player.is_gang = true;
			if (tip.card > 0)
			{
				for (Entry<Integer, Player> entry : player.getRoom().playerMapById.entrySet()) {
					EXPlayer pp = (EXPlayer) entry.getValue();
					pp.notPongList.add(tip.card/100*100+1);
					pp.notPongList.add(tip.card/100*100+2);
					pp.notPongList.add(tip.card/100*100+3);
					pp.notPongList.add(tip.card/100*100+4);
				}
			}
		}
		player.stateMachine.changeState(Global.getState(PRSelfKongState.class));

	}
}
