package extend.zp.player.rule;

import java.util.Collections;
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 com.taurus.core.plugin.redis.Redis;
import extend.zp.*;
import extend.zp.player.rulestate.PRCheck;
import extend.zp.tip.IRuleBase;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;
import redis.clients.jedis.Jedis;


public class RuleDealTi 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 (card == player.drawCard && num == 3) {
				player.winCard = player.drawCard;
				player.drawCard = 0;
				player.tiCount++;

				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				opcard.addInt(card);
				opcard.addInt(card);
				CardUtil.removeCard(player.cardInhand, card, num);

				int[] tiGroup = new int[4];
				tiGroup[0] = card;
				tiGroup[1] = card;
				tiGroup[2] = card;
				tiGroup[3] = card;

				player.tiGroup.add(tiGroup);
				player.opCardList.add(new OpCard(RuleWeight.TYPE_TI, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_TI, card)));

				EXMainServer.gameCtr.actionCard(player, card, card, RuleWeight.TYPE_TI, player.seat, opcard);
			}
			if (num == 4) {
				player.tiCount++;

				ITArray opcard = TArray.newInstance();
				opcard.addInt(card);
				opcard.addInt(card);
				opcard.addInt(card);
				opcard.addInt(card);
				CardUtil.removeCard(player.cardInhand, card, num);

				int[] tiGroup = new int[4];
				tiGroup[0] = card;
				tiGroup[1] = card;
				tiGroup[2] = card;
				tiGroup[3] = card;

				player.tiGroup.add(tiGroup);
				player.opCardList.add(new OpCard(RuleWeight.TYPE_TI, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_TI, card)));

				EXMainServer.gameCtr.actionCard(player, card, card, RuleWeight.TYPE_TI, player.seat, opcard);


			}
		}

		//判断是否是补牌
		if(player.tiGroup.size()>=2&&player.outcardList.size()==0&&player.bupaicount==0) {
			//随机取一张牌
			System.out.println("补牌:"+player.tiGroup.size());
			if (player.tiGroup.size()==2){
				EXRoom room = (EXRoom) player.room;
				int drawCard = room.card.cardList.remove(1);
				player.cardInhand.add(drawCard);
				Collections.sort(player.cardInhand);
				ITObject param = new TObject();
				param.putInt("card", drawCard);
				param.putInt("seat", player.seat);
				player.room.broadCastToClient(0, Config.GAME_EVT_ADD_CARD, param);
				int leftCount = room.card.getCount();
				param.putInt("left_count", leftCount);
				player.room.broadCastToClient(0, Config.GAME_EVT_DRAW, param);
				player.getRoom().getPlayBack().addAddCardCommand(player.room.activeSeat, drawCard);
				player.bupaicount = player.bupaicount+1;
			}else if(player.tiGroup.size()==3){
				for (int i=0;i<2;i++) {
					EXRoom room = (EXRoom) player.room;
					int drawCard = room.card.cardList.remove(1);
					player.cardInhand.add(drawCard);
					Collections.sort(player.cardInhand);
					ITObject param = new TObject();
					param.putInt("card", drawCard);
					param.putInt("seat", player.seat);
					player.room.broadCastToClient(0, Config.GAME_EVT_ADD_CARD, param);
					int leftCount = room.card.getCount();
					param.putInt("left_count", leftCount);
					player.room.broadCastToClient(0, Config.GAME_EVT_DRAW, param);
					player.getRoom().getPlayBack().addAddCardCommand(player.room.activeSeat, drawCard);
					player.bupaicount = player.bupaicount + 1;
				}

			}


		}
		return false;
	}

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