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.PROtherGongSelfWinState;
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 RuleOtherKongSelfWin 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>();
        if (player.is_gang)
        {
            if (room.config.getBoolean(Config.ROOM_CONFIG_GANGSHANGKAIHUA_2bei))
            {
                WinCard.putWinCardType(map, WinCardType.GANGSHANGHUA, 1,WinCardType.GANGSHANGHUA_SCORE*2, true, "杠上开花");
            }
            else
            {
                WinCard.putWinCardType(map, WinCardType.GANGSHANGHUA, 1,WinCardType.GANGSHANGHUA_SCORE, true, "杠上开花");
            }
        }

        WinCard win = new WinCard(player.cardInhand, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, false);
        if (win.checkWin(map, player, room, 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;
            player.tipMgr.addTip(tip);

            return true;
        }

        return false;
    }

    @Override
    public void action(EXPlayer player,Tip tip) {
        // TODO Auto-generated method stub
        player.is_gang = false;
        player.winType = 1;
        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;
        }

        if (player.is_gang)
        {
            player.stateMachine.changeState(Global.getState(PROtherGongSelfWinState.class));
        }
        else {
            player.stateMachine.changeState(Global.getState(PRSelfWinState.class));
        }
    }

}
