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;
import java.util.Map;

/**
 * ���Ƽ��
 * 
 *
 * 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(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.isLaizi(room.activeCard))
		{
			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 && room.config.getBoolean(Config.ROOM_CONFIG_GANGSHANGPAO_2bei))
		{
			WinCard.putWinCardType(map, WinCardType.GANGSHANGPAO, 1,WinCardType.GANGSHANGPAO_SOCRE*2, false, "杠上炮");
		}
		WinCard win = new WinCard(player.cardInhand, room.activeCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, false);
		if (win.checkWin(map, player, room, false)) {
			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;

		int laizi = room.config.getInt(Config.ROOM_CONFIG_LAIZI);
		WinCard win = new WinCard(player.cardInhand, room.activeCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, false);
		if (win.laizi_count > 0)
		{
			player.hu_score = 1;
			player.di_score = 1;
		}
		else {
			player.hu_score = 2;
			player.di_score = 2;
		}

		player.hu_score = room.config.getInt(Config.LAIYOU_DIFEN);
		player.di_score = room.config.getInt(Config.LAIYOU_DIFEN);

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

		int dahu_score = 1;
		int jia_score = 0;
		for (Map.Entry<Integer, WinCardType> entry : player.winMap.entrySet()) {
			WinCardType cardType = entry.getValue();
			Global.logger.info("player:" + player.playerid + " hu:" + cardType.toString());
			if (cardType.IsDaHu())
			{
				dahu_score *= cardType.score;
			}
			else
			{
				jia_score +=  cardType.score;
			}

			if (cardType.type == WinCardType.LIAN_GUN_DAI_PA)
			{
				room.addAllScore(player, Config.AN_GANG_SCORE, EXScore.KONG);
				player.an_gang_num++;
			}
		}

		if (dahu_score > 0)
		{
			player.hu_score *= dahu_score;
		}

		player.hu_score += jia_score;

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