package extend.mj.player.rule;

import com.game.Global;
import com.game.Util;
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.PRPongKongWinState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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

    @Override
    public boolean condition(EXPlayer player) {
        if(player.louhu)return false;
        EXRoom room = player.getRoom();
        ITObject config = room.config;

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

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

        int activieCard = room.activeCard;
        int laizi = 0;
        HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();

        WinCard.putWinCardType(map, WinCardType.QIANG_GANG_HU, 1, WinCardType.QIANG_GANG_HU_SOCRE, false, "抢杠胡");


        boolean flag = false;
        if (WinCard.IsDiaoYu(player, player.cardInhand, player.drawCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false))
        {
            WinCard win = new WinCard(player.cardInhand, room.activeCard, 0, 0, false, false);
            if (win.checkWin(map, player, room, false)) {
                flag = true;
            }
        }
        else {
            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)) {
                flag = true;
            }
        }
        if (flag) {
            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);


            ArrayList<Integer> tempCardList = new ArrayList<Integer>();
            tempCardList.addAll(player.cardInhand);
            Util.removeCard(tempCardList, player.drawCard, 1);
            WinCard win2 = new WinCard(tempCardList, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false);
            int cheng_num = 0;
            for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
                EXPlayer tempPlayer = (EXPlayer)entry.getValue();
                cheng_num += tempPlayer.cheng_num;
            }
            if (player.is_cheng)
            {

                if (win2.laizi_count <= 0)
                {
                    WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.YING_MO_SCORE, true, "硬摸");
                }
                else {
                    WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                    if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                        WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.YING_MO_SCORE, true, "硬摸");
                    }
                }


            }
            else {
                if (win2.laizi_count <= 0)
                {
                    WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.YING_MO_SCORE, true, "硬摸");
                }
                else {
                    WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                    if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                        WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.YING_MO_SCORE, true, "硬摸");
                    }
                }
            }

            if (player.seat==room.bankerSeat){
                WinCard.putWinCardType(map, WinCardType.ZHUANGJIA, 1,WinCardType.BASE_HU_SCORE, false, "庄家");
            }else{
                WinCard.putWinCardType(map, WinCardType.XIANJIA, 1,WinCardType.BASE_HU_SCORE, false, "闲家");
            }


            return true;
        }

        return false;
    }

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

        Global.logger.info("PongKong player:" + player.playerid + " di fen:" + player.di_score + " hu fen:" + player.hu_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 (dahu_score > 0)
        {
            player.hu_score *= dahu_score;
        }

        player.hu_score += jia_score;;

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