package extend.mj.player.rule;

import java.util.ArrayList;

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

import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.RuleWeight;
import extend.mj.player.rulestate.PRPongState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;

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

	@Override
	public boolean condition(EXPlayer player) {
		if(player.autoOutCard)return false;
		EXRoom room = player.getRoom();
		if (room.activeHaidi) {
			return false;
		}
		
		boolean result = false;
		if(room.activeKong!=null){
			if(room.activeKong.haidi)return false;
			if(room.activeKong.kongSeat == player.seat)return false;
			for (int card : room.activeKong.drawList) {
				result = check(player,card) || result;
			}
		}else{
			int eventCard = room.activeCard;
			result = check(player,eventCard);
		}
		
		return result;
	}
	
	private boolean check(EXPlayer player,int eventCard){
		if (Util.cardNum(eventCard, player.cardInhand) >= 2) {
			ArrayList<Integer> opCard = new ArrayList<>();
			opCard.add(eventCard);
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard);
			Tip tip = new Tip(eventCard,opcard, RuleWeight.PONG,this, RuleWeight.TYPE_PONG);
			tip.group_type = RuleWeight.GROUO_PONG;
			player.tipMgr.addTip(tip);
			return true;
		}
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.getRoom().activeKong = null;
		player.getRoom().activeCard = tip.card;
		player.stateMachine.changeState(Global.getState(PRPongState.class));
	}

}
