package extend.zp.player.rulestate;

import com.game.Global;
import com.game.player.state.PlayerWaitState;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;

import com.taurus.core.util.Logger;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.tip.Tip;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;

public class PRChi {
	public static final Logger logger = Logger.getLogger(PRChi.class);

	public static void prChi(int card, EXPlayer owner) {
		////System.out.println("prchi");
		EXPlayer activePlayer = (EXPlayer) owner.room.playerMapBySeat.get(owner.room.activeSeat);
		activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		Tip actionTip = owner.actionTip;
		ITArray opcard = actionTip.opcard;
		int chowcard1 = opcard.getInt(0);
		int chowcard2 = opcard.getInt(1);

		__chow(owner, chowcard1, chowcard2, card, activePlayer, false);
		if (actionTip.bi_list != null && actionTip.biid != null) {
			ITObject obj = actionTip.bi_list.getTObject(actionTip.biid.getInt(0));
			ITArray biopcard = obj.getTArray("opcard");
			__chow(owner, biopcard.getInt(0), biopcard.getInt(1), card, owner, true);
			if (obj.containsKey("bi_list")) {
				ITArray bi_list1 = obj.getTArray("bi_list");
				ITObject obj1 = bi_list1.getTObject(actionTip.biid.getInt(1));
				ITArray biopcard1 = obj1.getTArray("opcard");
				__chow(owner, biopcard1.getInt(0), biopcard1.getInt(1), card, owner, true);
			}
		}

	}

	private static final void __chow(EXPlayer player, int chowcard1, int chowcard2, int card, EXPlayer activePlayer, boolean bi) {
		int[] chiGroup = new int[3];
		chiGroup[0] = chowcard1;
		chiGroup[1] = card;
		chiGroup[2] = chowcard2;
		CardUtil.removeCard(player.cardInhand, chowcard1, 1);
		if (bi) {
			CardUtil.removeCard(player.cardInhand, card, 1);
		}
		CardUtil.removeCard(player.cardInhand, chowcard2, 1);
		logger.info("jefe chowGroup,player:" + player.playerid + ",card1:" + chowcard1+",card2:"+card+",card3:"+chowcard2);
		player.chowGroup.add(chiGroup);
		int huXi = 0;
		if (chiGroup[0] % 100 != chiGroup[1] % 100) {
			if (chiGroup[0] % 100 + chiGroup[1] % 100 + chiGroup[2] % 100 == 6 || chiGroup[0] % 100 + chiGroup[1] % 100 + chiGroup[2] % 100 == 19) {
				huXi += Paixing.caclHuxi(RuleWeight.TYPE_CHOW, chiGroup[0]);
			}
		}
		player.opCardList.add(new OpCard(RuleWeight.TYPE_CHOW, chiGroup[0], chiGroup[1], chiGroup[2], huXi));
		
		ITArray opcard = TArray.newInstance();
		opcard.addInt(chiGroup[0]);
		opcard.addInt(chiGroup[2]);
		EXMainServer.gameCtr.actionCard(player, card, card, bi ? RuleWeight.TYPE_BI : RuleWeight.TYPE_CHOW, activePlayer.seat, opcard);
	}
}
