package extend.zp.player.rule;

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

import extend.zp.Config;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.player.rulestate.PRWinState;
import extend.zp.tip.IRuleBase;
import extend.zp.tip.Tip;
import extend.zp.uitl.PaiXing;

public class RuleWin implements IRuleBase{

	@Override
	public boolean condition(EXPlayer player) {
		if (player.siShou) return false;
		EXRoom room = player.getRoom();
		int card = room.showCard;
		player.winType = 0;
		player.winCard = room.showCard == 0 ? player.actionCard : room.showCard;
		player.currentOpCardList.clear();
		for (OpCard opCard : player.opCardList) {
			player.currentOpCardList.add(new OpCard(opCard.type, opCard.card1, opCard.card2, opCard.card3, opCard.huXi));
		}
		EXPlayer activePlayer = (EXPlayer) player.room.playerMapBySeat.get(player.getRoom().draw);
		if ((room.tianHu && player.seat == room.bankerSeat) || !room.tianHu) {
			if (PaiXing.checkWin(player, card, player.currentOpCardList)) {
				player.winType = 2;
			}
		}
		if (room.tianHu && (room.config.getInt(Config.ROOM_CONFIG_WANFA) == 1 || 
				room.config.getInt(Config.ROOM_CONFIG_WANFA) == 4 || 
				(room.config.containsKey(Config.ROOM_CONFIG_TH) && room.config.getBoolean(Config.ROOM_CONFIG_TH))) 
				&& PaiXing.checkSpecialWin(player, room.config.getInt(Config.ROOM_CONFIG_WANFA))) {
			if (player.winType > 0) {
				player.winType = 3;
			}else {
				player.winType = 1;
			}
		}
		if (player.winType > 0) {
			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			int weight = RuleWeight.WIN3;
			if (activePlayer.seat != player.seat) {
				if (player.seat == activePlayer.nextSeat) {
					weight = RuleWeight.WIN2;
				} else {
					weight = RuleWeight.WIN1;
				}
			}
			Tip tip = new Tip(card, opcard, weight, this, RuleWeight.TYPE_WIN);
			player.tipMgr.addTip(tip);
			if (player.seat == activePlayer.seat) {
				player.isZiMo = true;
			}
			Global.logger.info(player + " zimo:"+ player.isZiMo+ ",winType��"+player.winType);
			return true;
		}
		player.winCard = 0;
		return false;
	}

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

}
