package extend.mj.player.rulestate;


import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.game.state.StateBase;
import extend.mj.*;

import java.util.Map;

/**
 * ��Ӧ��������״̬
 *
 *
 * 2017��8��30��
 * PRSelfWinState.java
 */
public class PROtherGongSelfWinState extends StateBase<EXPlayer> {

    @Override
    public void enter(EXPlayer owner) {

        EXRoom room = owner.getRoom();
        owner.winCard = owner.drawCard;
        owner.winer = 1;
        Util.removeCard(owner.cardInhand, owner.winCard, 1);
        if (owner.opCardList.size() > 0)
        {
            OpCard opCard = owner.opCardList.get(owner.opCardList.size()-1);
            if (opCard.type == RuleWeight.TYPE_KONG && opCard.card_seat > 0)
            {


                owner.room.activeSeat = opCard.card_seat;
                EXPlayer activePlayer =  (EXPlayer)room.playerMapBySeat.get(owner.room.activeSeat);


                for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
                    if (entry.getKey().equals(owner.playerid)) {
                        continue;
                    }
                    EXPlayer fromPlayer = (EXPlayer) entry.getValue();
                    fromPlayer.winer = 2;
                    fromPlayer.last_winer = 2;
                }

                int score = 1;
                for (Map.Entry<Integer, WinCardType> entry : owner.winMap.entrySet()) {
                    WinCardType wct = entry.getValue();
                    for(int i = 0;i<wct.value; ++i) {
                        Global.logger.info("wct.type:"+wct.type);
                        Global.logger.info("wct.score:"+wct.score);
                        if (wct.type!=WinCardType.YING_MO){
                            score = wct.score * score;
                        }

                        Global.logger.info("score:"+score);
                    }
                }

                if (score>1){
                    room.addAllScore(owner,score,EXScore.WIN);
                }


                EXMainServer.gameCtr.winEvent(owner,owner.seat,owner.winCard);
                if (room.lastBankerSeat == 0)
                {
                    room.lastBankerSeat = room.bankerSeat;
                }
                owner.room.bankerSeat = owner.seat;
                owner.settleLog.add(Config.SETTLE_ZIMO);
               // room.niao_dianPao_quanbao(owner ,owner.winCard, qg_type, true);

                room.endGame();
                return;
            }
            else if (opCard.type == RuleWeight.TYPE_SELF_KONG || (opCard.type == RuleWeight.TYPE_KONG && opCard.card_seat == 0))
            {

                for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
                    if (entry.getKey().equals(owner.playerid)) {
                        continue;
                    }
                    EXPlayer fromPlayer = (EXPlayer) entry.getValue();
                    fromPlayer.winer = 2;
                    fromPlayer.last_winer = 2;
                }

                int score = 1;
                for (Map.Entry<Integer, WinCardType> entry : owner.winMap.entrySet()) {
                    WinCardType wct = entry.getValue();
                    for(int i = 0;i<wct.value; ++i) {
                        Global.logger.info("wct.type:"+wct.type);
                        Global.logger.info("wct.score:"+wct.score);
                        if (wct.type!=WinCardType.YING_MO) {
                            score = wct.score * score;
                        }
                        Global.logger.info("score:"+score);
                    }
                }

                if (score>1){
                    room.addAllScore(owner,score,EXScore.WIN);
                }

                EXMainServer.gameCtr.winEvent(owner,owner.seat,owner.winCard);
                if (room.lastBankerSeat == 0)
                {
                    room.lastBankerSeat = room.bankerSeat;
                }

                owner.room.bankerSeat = owner.seat;
                owner.settleLog.add(Config.SETTLE_ZIMO);

                room.endGame();
                return;
            }
        }
    }

    @Override
    public void toNextState(EXPlayer owner) {
        // TODO Auto-generated method stub

    }

    @Override
    public void exit(EXPlayer owner) {
        // TODO Auto-generated method stub

    }

    @Override
    public void execute(EXPlayer owner, String cmd, int gid, Object param) {
        // TODO Auto-generated method stub

    }

}
