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

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


            ArrayList<Integer> tempCardList = new ArrayList<Integer>();
            tempCardList.addAll(player.cardInhand);
            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 (cheng_num == 0)
                {
                    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 (cheng_num == 1)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.ONE_CHENG_QING_LAI_YOU, 1,WinCardType.ONE_CHENG_QING_LAI_YOU_SOCRE, true, "一撑清癞油");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.ONE_CHENG_LAI_YOU, 1,WinCardType.ONE_CHENG_LAI_YOU_SOCRE, true, "一撑癞油");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.ONE_CHENG_QING_LAI_YOU, 1,WinCardType.ONE_CHENG_QING_LAI_YOU_SOCRE, true, "一撑清癞油");
                    }
                }
                else if (cheng_num == 2)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.TWO_CHENG_QING_LAI_YOU, 1,WinCardType.TWO_CHENG_QING_LAI_YOU_SOCRE, true, "二撑清癞油");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.TWO_CHENG_LAI_YOU, 1,WinCardType.TWO_CHENG_LAI_YOU_SOCRE, true, "二撑癞油");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.TWO_CHENG_QING_LAI_YOU, 1,WinCardType.TWO_CHENG_QING_LAI_YOU_SOCRE, true, "二撑清癞油");
                    }
                }
                else if (cheng_num == 3)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.THREE_CHENG_QING_LAI_YOU, 1,WinCardType.THREE_CHENG_QING_LAI_YOU_SOCRE, true, "三撑清癞油");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.THREE_CHENG_LAI_YOU, 1,WinCardType.THREE_CHENG_LAI_YOU_SOCRE, true, "三撑癞油");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.THREE_CHENG_QING_LAI_YOU, 1,WinCardType.THREE_CHENG_QING_LAI_YOU_SOCRE, true, "三撑清癞油");
                    }
                }
                else if (cheng_num == 4)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_QING_LAI_YOU, 1,WinCardType.FOUR_CHENG_QING_LAI_YOU_SOCRE, true, "四撑清癞油");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_LAI_YOU, 1,WinCardType.FOUR_CHENG_LAI_YOU_SOCRE, true, "四撑癞油");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_QING_LAI_YOU, 1,WinCardType.FOUR_CHENG_QING_LAI_YOU_SOCRE, true, "四撑清癞油");
                    }
                }
            }
            else {
                if (cheng_num == 0)
                {
                    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 (cheng_num == 1)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.ONE_CHENG_YING_MO, 1,WinCardType.ONE_CHENG_YING_MO_SCORE, true, "一撑硬摸");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.ONE_CHENG_LAI_MO, 1,WinCardType.ONE_CHENG_LAI_MO_SCORE, true, "一撑癞摸");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.ONE_CHENG_YING_MO, 1,WinCardType.ONE_CHENG_YING_MO_SCORE, true, "一撑硬摸");
                    }
                }
                else if (cheng_num == 2)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.TWO_CHENG_YING_MO, 1,WinCardType.TWO_CHENG_YING_MO_SOCRE, true, "二撑硬摸");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.TWO_CHENG_LAI_MO, 1,WinCardType.TWO_CHENG_LAI_MO_SOCRE, true, "二撑癞摸");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.TWO_CHENG_YING_MO, 1,WinCardType.TWO_CHENG_YING_MO_SOCRE, true, "二撑硬摸");
                    }
                }
                else if (cheng_num == 3)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.THREE_CHENG_YING_MO, 1,WinCardType.THREE_CHENG_YING_MO_SOCRE, true, "三撑硬摸");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.THREE_CHENG_LAI_MO, 1,WinCardType.THREE_CHENG_LAI_MO_SOCRE, true, "三撑癞摸");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.THREE_CHENG_YING_MO, 1,WinCardType.THREE_CHENG_YING_MO_SOCRE, true, "三撑硬摸");
                    }
                }
                else if (cheng_num == 4)
                {
                    if (win2.laizi_count > 0)
                    {
                        WinCard win3 = new WinCard(player.cardInhand, 0, 0, false);
                        if (win2.laizi_count == 1 && win3.checkWin(map, player, room, true)) {
                            WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_YING_MO, 1,WinCardType.FOUR_CHENG_YING_MO_SOCRE, true, "四撑硬摸");
                        }
                        else {
                            WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_LAI_MO, 1,WinCardType.FOUR_CHENG_LAI_MO_SOCRE, true, "四撑癞摸");
                        }
                    }
                    else {
                        WinCard.putWinCardType(map, WinCardType.FOUR_CHENG_YING_MO, 1,WinCardType.FOUR_CHENG_YING_MO_SOCRE, true, "四撑硬摸");
                    }
                }
            }

            if (room.config.getBoolean(Config.lian_gun_dai_pai))
            {
                if (room.card.laiziCardBefore > 0)
                {
                    if (player.drawCard ==  room.card.laiziCardBefore)
                    {
                        if (Util.checkCard(player.drawCard, tempCardList, 2))
                        {
                            WinCard.putWinCardType(map, WinCardType.LIAN_GUN_DAI_PA, 1, 0, true, "连滚带爬");
                        }
                    }
                }

                if (room.card.laiziCard2Before > 0)
                {
                    if (player.drawCard ==  room.card.laiziCard2Before)
                    {
                        if (Util.checkCard(player.drawCard, tempCardList, 2))
                        {
                            WinCard.putWinCardType(map, WinCardType.LIAN_GUN_DAI_PA, 1, 0, true, "连滚带爬");
                        }
                    }
                }
            }

            return true;
        }
        return false;
    }

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

        player.winMap = tip.winMap;
        EXRoom room = player.getRoom();

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

        player.niao_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);

        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
            {
                if (cardType.type != WinCardType.JIHU && cardType.type != WinCardType.PINGHU)
                {
                    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(PROtherGongSelfWinState.class));
    }

}
