package extend.zp.player.rule;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;

import com.taurus.core.util.Logger;
import extend.zp.Config;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.player.rulestate.PRWeiState;
import extend.zp.tip.IRuleBase;
import extend.zp.tip.Tip;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;


public class RuleWei implements IRuleBase {
	Logger logger = Logger.getLogger(getClass());
	@Override
	public boolean condition(EXPlayer player) {
		if (player.isDeadPut)
		{
			return false;
		}
		int eventCard = player.drawCard;
		logger.info("jefe RuleWei "+eventCard);
		if (CardUtil.cardNum(eventCard, player.cardInhand) >= 2) {
			if (player.getRoom().config.getInt(Config.ROOM_CONFIG_KA_WAI) == 0)
			{
				if (Paixing.checkWin(player, player.drawCard, true)) {
					return false;
				}
				else {
					player.winCard = player.drawCard;
					player.drawCard = 0;
					CardUtil.removeCard(player.cardInhand, eventCard, 2);
					ITArray opcard = TArray.newInstance();
					opcard.addInt(eventCard);
					opcard.addInt(eventCard);
					opcard.addInt(0);
					player.initOpCard(opcard);
					this.action(player);
				}
			}
			else {
				ITArray opcard = TArray.newInstance();
				opcard.addInt(eventCard);
				opcard.addInt(eventCard);
				Tip tip = new Tip(eventCard, opcard, RuleWeight.WAI, this, RuleWeight.TYPE_WEI);
				player.tipMgr.addTip(tip);
				return true;
			}
		}
		else {
			for (int i = 0; i < player.kanGroup.size(); i++) {
				if (eventCard == player.kanGroup.get(i)[0]) {
					if (player.getRoom().config.getInt(Config.ROOM_CONFIG_KA_WAI) == 0)
					{
						if (Paixing.checkWin(player, player.drawCard, true)) {
							return false;
						}
						else {
							player.winCard = player.drawCard;
							player.drawCard = 0;
							player.kanGroup.remove(i);
							CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, eventCard, eventCard, eventCard, 0));
							player.cardInhand.add(eventCard);
							ITArray opcard = TArray.newInstance();
							opcard.addInt(eventCard);
							opcard.addInt(eventCard);
							opcard.addInt(1);
							player.initOpCard(opcard);
							this.action(player);
							break;
						}
					}
					else {
						ITArray opcard = TArray.newInstance();
						opcard.addInt(eventCard);
						opcard.addInt(eventCard);
						Tip tip = new Tip(eventCard, opcard, RuleWeight.WAI, this, RuleWeight.TYPE_WEI);
						player.tipMgr.addTip(tip);
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	public void action(EXPlayer player) {
		player.notPutSet.add(player.drawCard);
		if (player.getRoom().config.getInt(Config.ROOM_CONFIG_KA_WAI) != 0)
		{
			int eventCard = player.drawCard;
			player.winCard = player.drawCard;
			player.drawCard = 0;
			if (CardUtil.cardNum(eventCard, player.cardInhand) >= 2) {
				CardUtil.removeCard(player.cardInhand, eventCard, 2);
				ITArray opcard = TArray.newInstance();
				opcard.addInt(eventCard);
				opcard.addInt(eventCard);
				opcard.addInt(0);
				player.initOpCard(opcard);
			}
			else
			{
				for (int i = 0; i < player.kanGroup.size(); i++) {
					if (eventCard == player.kanGroup.get(i)[0]) {
						player.kanGroup.remove(i);
						CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, eventCard, eventCard, eventCard, 0));
						player.cardInhand.add(eventCard);

						ITArray opcard = TArray.newInstance();
						opcard.addInt(eventCard);
						opcard.addInt(eventCard);
						opcard.addInt(1);
						player.initOpCard(opcard);
						break;
					}
				}
			}
		}

		player.stateMachine.changeState(Global.getState(PRWeiState.class));
	}
}
