package extend.zp.player.rule;

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

import extend.zp.Config;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.RuleWeight;
import extend.zp.player.rulestate.PRChowState;
import extend.zp.tip.IRuleBase;
import extend.zp.tip.Tip;
import extend.zp.uitl.ChowCard;

/**
 * ���Ƽ��
 * 
 */
public class RuleChow implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		// �й�Ĭ�ϲ����
		if (player.isEntrust() || player.siShou) return false;
		EXRoom room = player.getRoom();
		if (!(player.seat == room.activeSeat || player.lastSeat == room.activeSeat)) {
			return false;
		}
		if (room.config.containsKey(Config.ROOM_CONFIG_CHUPAI) && room.draw == 0 
				&& room.config.getInt(Config.ROOM_CONFIG_CHUPAI) == 1) return false;
		if (room.config.containsKey(Config.ROOM_CONFIG_FANPAI) && room.draw > 0 
				&& room.config.getInt(Config.ROOM_CONFIG_FANPAI) == 1 && player.seat != room.draw) return false;
		if(player.cardInhand.size() <= 2 || Util.checkCard(room.showCard, player.cardInhand, 2)) {
			return false;
		}
		
		int card = room.showCard;
		if(player.chouList.contains(card))return false;
		
		boolean result = false;
		boolean self = player.seat == room.activeSeat;

		ChowCard chow = new ChowCard(player.cardInhand,card);
		ITArray list = TArray.newInstance();
		result = chow.tryChow(list);
		if(result) {
			for(int i=0;i<list.size();++i) {
				ITObject obj = list.getTObject(i);
				ITArray opcard = obj.getTArray("opcard");
				Tip tip = new Tip(card,opcard, self?RuleWeight.SELF_CHOW:RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
				tip.group_type = RuleWeight.GROUP_CHOW;
				log(player.playerid, card, i, opcard);
				player.tipMgr.addTip(tip);
			}
		}
		return result;
	}
	
	private void log(int playerId, int card, int i, ITArray opcard) {
		Global.logger.info("playerId:" + playerId + ",eventCard:" + card + ",i:"+i + ",opcard:" +logOpcard(opcard));
	}
	
	private String logOpcard(ITArray opcard) {
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < opcard.size(); i++) {
			sb.append(opcard.get(i).getObject() + ",");
		}
		return sb.toString().substring(0, sb.lastIndexOf(","));
	}

	@Override
	public void action(EXPlayer player) {
		player.stateMachine.changeState(Global.getState(PRChowState.class));
	}

}
