package extend.mj.player.rule;

import com.game.Global;
import com.game.data.Player;
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.PRSelfWinState;
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��
 * RuleSelfWin.java
 */
public class RuleSelfWin implements IRuleBase {

	@Override
	public boolean condition(EXPlayer player) {
		// TODO Auto-generated method stub
		ITObject config = player.room.config;
		EXRoom room = player.getRoom();
		int laizi = config.getInt(Config.ROOM_CONFIG_LAIZI);
		HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
		WinCard win = new WinCard(player.cardInhand, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, false);
		if (win.checkWin(map, player, room, true)) {
			if (player.cardInhand.size() == 0 && player.opCardList.size() == 6)
			{
				//WinCard.putWinCardType(map, WinCardType.MAN_TIAN_HU, 1,WinCardType.MAN_TIAN_HU_SCORE, false, "满天胡");
			}

			if (map.containsKey(WinCardType.PENGPENGHU))
			{
				if (room.maxPlayers == 4 && (room.bankerSeat+2)%room.maxPlayers+1 == player.seat)
				{
					map.remove(WinCardType.PENGPENGHU);
					WinCard.putWinCardType(map, WinCardType.GUN_GUN, 1,WinCardType.GUN_GUN_SCORE, true, "滚滚");
				}
			}

			ITArray opcard = TArray.newInstance();
			opcard.addInt(player.drawCard);
			Tip tip = new Tip(player.drawCard,opcard, RuleWeight.SELF_WIN,this, RuleWeight.TYPE_WIN);
			tip.winMap = map;
			tip.self = true;
			player.tipMgr.addTip(tip);

			return true;
		}
		return false;
	}

	@Override
	public void action(EXPlayer player,Tip tip) {
		// TODO Auto-generated method stub
		player.winCard = tip.card;
		player.drawCard = tip.card;
		player.winMap = tip.winMap;
		EXRoom room = player.getRoom();

		player.niao_score = WinCardType.BASE_HU_SCORE;
		player.hu_score = WinCardType.BASE_HU_SCORE;
		player.di_score = WinCardType.BASE_HU_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);
		boolean isDaHu = false;
		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())
			{
				isDaHu = true;
			}
		}

		if (isDaHu)
		{
			player.hu_score = 3;
		}

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

}
