package extend.mj.player.rule;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import extend.mj.*;
import extend.mj.player.rulestate.PROtherWinState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.WinCard;

import java.util.HashMap;

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

	@Override
	public boolean condition(EXPlayer player) {
		if(player.louhu)return false;
		EXRoom room = player.getRoom();
		ITObject config = room.config;
		if (!config.getBoolean(Config.ROOM_CONFIG_DIANPAO)) {
			if (!config.getBoolean(Config.ROOM_CONFIG_SPECAIL_ZIMO))
			{
				return false;
			}
		}

		if (player.lastSeat != room.activeSeat) {
			return false;
		}

		if(room.activeSeat == player.seat)return false;

		EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(room.activeSeat);

		if (player.cardInhand.size() == 1 && room.isLaizi(player.cardInhand.get(0)))
		{
			return false;
		}

		if (room.activeCard % 100 == 4)
		{
			return false;
		}
		
		int activieCard = room.activeCard;
		int laizi = room.config.getInt(Config.ROOM_CONFIG_LAIZI);
		HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
		if (activePlayer.gangshangpao)
		{
			if ( room.config.getBoolean(Config.ROOM_CONFIG_GANGSHANGPAO_2bei))
			{
				WinCard.putWinCardType(map, WinCardType.GANGSHANGPAO, 1,WinCardType.GANGSHANGPAO_SOCRE*2, true, "杠上炮");
			}
			else {
				WinCard.putWinCardType(map, WinCardType.GANGSHANGPAO, 1,WinCardType.GANGSHANGPAO_SOCRE, true, "杠上炮");
			}
		}
		WinCard win = new WinCard(player.cardInhand, room.activeCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, false, false);
		if (win.checkWin(map, player, room, false)) {
			if (map.containsKey(WinCardType.MEN_QING) || map.containsKey(WinCardType.PENGPENGHU) || map.containsKey(WinCardType.GANGSHANGPAO))
			{
				ITArray opcard = TArray.newInstance();
				opcard.addInt(activieCard);
				Tip tip = new Tip(room.activeCard,opcard, RuleWeight.WIN,this, RuleWeight.TYPE_WIN);
				tip.winType = player.forceCheckWin? 1 :0;
				tip.winMap = map;
				player.tipMgr.addTip(tip);

				return true;
			}
		}
		
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		player.winType = tip.winType;
		player.getRoom().activeCard = tip.card;
		player.winMap = tip.winMap;
		EXRoom room = player.getRoom();
		player.niao_score = WinCardType.BASE_HU_SCORE;

		player.hu_score = 1;
		player.di_score = 1;

		if (tip.winMap.containsKey(WinCardType.PENGPENGHU))
		{
			player.hu_score = WinCardType.PENGPENGHU_SCORE;
		}

		Global.logger.info("player:" + player.playerid + " di fen:" + player.di_score + " hu fen:" + player.hu_score + " niao fen:" + player.niao_score);

		room.handle_repeatwin(player);

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