package extend.mj.player.rule;

import java.util.List;

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

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

/**
 * ����״̬
 * 
 *
 * 2017��11��2��
 * PRChowRule.java
 */
public class RuleChow implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
	    if (player.room.config.getBoolean(Config.ROOM_CONFIG_KAN_CHI) == false) return false;

		if(player.autoOutCard)return false;
		EXRoom room = player.getRoom();
		if (room.activeHaidi) {
			return false;
		}
		
		if (player.lastSeat != room.activeSeat) {
			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){
		boolean result = false;
		List<Integer>	cardInhand = player.cardInhand;
		if (Util.checkCard(eventCard - 1, cardInhand)  && Util.checkCard(eventCard - 2, cardInhand)) {
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard - 1);
			opcard.addInt(eventCard - 2);
			Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
			tip.group_type = RuleWeight.GROUP_CHOW;
			player.tipMgr.addTip(tip);
			result = result || true;
		}
		
		if (Util.checkCard(eventCard + 1, cardInhand) && Util.checkCard(eventCard - 1, cardInhand)) {
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard + 1);
			opcard.addInt(eventCard - 1);
			Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
			tip.group_type = RuleWeight.GROUP_CHOW;
			player.tipMgr.addTip(tip);
			result = result || true;
		}
		
		if (Util.checkCard(eventCard + 1, cardInhand) && Util.checkCard(eventCard + 2, cardInhand)) {
			ITArray opcard = TArray.newInstance();
			opcard.addInt(eventCard + 1);
			opcard.addInt(eventCard + 2);
			Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
			tip.group_type = RuleWeight.GROUP_CHOW;
			player.tipMgr.addTip(tip);
			result = result || true;
		}
		return result;
	}

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

}
