package extend.zp.player.rule;

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

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;

import extend.zp.Config;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.tip.IRuleBase;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;


public class RuleDealKan implements IRuleBase {
	@Override
	public boolean condition(EXPlayer player) {
		Map<Integer, Integer> cardMap = CardUtil.getCardNumMap(player.cardInhand);
		for (Entry<Integer, Integer> entry : cardMap.entrySet()) {
			int card = entry.getKey();
			int num = entry.getValue();
			if(num == 2 && card == player.drawCard) {
				player.winCard = player.drawCard;
				player.drawCard = 0;
				
				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				opcard.addInt(card);
				opcard.addInt(card);
				CardUtil.removeCard(player.cardInhand, card, num);
				
				int[] kanGroup = new int[3];
				kanGroup[0] = card;
				kanGroup[1] = card;
				kanGroup[2] = card;
				
				player.kanGroup.add(kanGroup);
				player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_KAN, card), true));
				EXMainServer.gameCtr.actionCard(player, card, card, RuleWeight.TYPE_KAN, player.seat, opcard);
				
				ITObject param = new TObject();
				param.putInt("card", card);
				param.putInt("seat", player.room.activeSeat);
				player.room.broadCastToClient(0, Config.GAME_EVT_ADD_CARD, param);
			}
			if (num == 3) {
				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				opcard.addInt(card);
				opcard.addInt(card);
				CardUtil.removeCard(player.cardInhand, card, num);

				int[] kanGroup = new int[3];
				kanGroup[0] = card;
				kanGroup[1] = card;
				kanGroup[2] = card;

				player.kanGroup.add(kanGroup);
				player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_KAN, card), true));
				EXMainServer.gameCtr.actionCard(player, card, card, RuleWeight.TYPE_KAN, player.seat, opcard);
			}
		}
		return false;
	}

	@Override
	public void action(EXPlayer player) {
	}
}
