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.PROtherKongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;

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

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

	@Override
	public boolean condition(EXPlayer player) {
		EXRoom room = player.getRoom();

		int eventCard = room.activeCard;
		if (player.isEntrust()) {
			return false;
		}

		if (player.is_gang)
		{
			return false;
		}

		EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(room.activeSeat);
		if (activePlayer.gangshangpao)
		{
			return false;
		}

		if (Util.checkCard(eventCard, player.cardInhand, 3)) {
			if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
			{
				if (player.notPongKongList.contains(eventCard))
				{
					return false;
				}
			}
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard);
			player.is_gang = false;

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

			if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
			{
				player.notPongKongList.add(eventCard);
			}
			return true;
		}
		else if (Util.checkCard(eventCard, player.cardInhand, 2) && Util.checkCard(eventCard /100 * 100 + 4, player.cardInhand, 1)) {
			if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
			{
				if (player.notPongKongList.contains(eventCard))
				{
					return false;
				}
			}
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard);
			opcard.addInt(eventCard /100 * 100 + 4);
			player.is_gang = false;
			List<Integer> delPai = new ArrayList<>();
			delPai.add(eventCard);
			delPai.add(eventCard);
			delPai.add(eventCard /100 * 100 + 4);
			List<OpCard> opCards = new ArrayList<>();
			opCards.addAll(player.opCardList);
			OpCard pongCard = new OpCard(RuleWeight.TYPE_KONG, eventCard);
			pongCard.opCard = new TArray();
			pongCard.opCard.addInt(eventCard);
			pongCard.opCard.addInt(eventCard);
			pongCard.opCard.addInt(eventCard);
			pongCard.opCard.addInt(eventCard /100 * 100 + 4);
			opCards.add(pongCard);
			if (((EXGameController) Global.gameCtr).isTingPai(player, 0, delPai, opCards))
			{
				Tip tip = new Tip(eventCard,opcard, RuleWeight.SELFKONG,this, RuleWeight.TYPE_KONG);
				player.tipMgr.addTip(tip);
				Tip tip2 = new Tip(eventCard,opcard, RuleWeight.SELFZHAO,this, RuleWeight.TYPE_ZHAO);
				player.tipMgr.addTip(tip2);
			}
			else {
				Tip tip = new Tip(eventCard, opcard, RuleWeight.ZHAO, this, RuleWeight.TYPE_ZHAO);
				player.tipMgr.addTip(tip);
			}

			if (player.getRoom().config.getBoolean(Config.ROOM_CONFIG_GUO_GONG))
			{
				player.notPongKongList.add(eventCard);
			}
			return true;
		}
		return false;
	}

	@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_KONG)
		{
			player.is_gang = true;
			if (tip.card > 0)
			{
				for (Map.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(PROtherKongState.class));
	}

}
