package extend.pk.player.state;

import com.game.ActionEvent;
import com.game.Global;
import com.game.data.Player;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITObject;
import extend.pk.*;


import java.util.Map;


/**
 *
 */
public class EXPlayerBaoChunState extends StateBase<EXPlayer> {

    @Override
    public void enter(EXPlayer owner) {
        Global.logger.error("EXPlayerBaoChunState enter");

        if(!owner.isEntrust()) {
            EXMainServer.gameCtr.baoChunTipEvent(owner,0);
        }
        owner.startActionTimer();
    }

    /**
     * 重连
     *
     * @param owner
     */
    @Override
    public void reload(EXPlayer owner) {

        EXMainServer.gameCtr.baoChunTipEvent(owner,1);

        owner.startActionTimer();
    }

    @Override
    public void exit(EXPlayer owner) {
        owner.stopActionTimer();
    }






    private void _action(EXPlayer owner,int id,int gid) {
        owner.baochun = id;
        EXRoom room = owner.getRoom();

        int tmp = 0;
        if (id==0){
            //不报春
        }else {
            //获得报春最近的
            if(room.baochunSeat==0){
                room.baochunSeat = owner.seat;
            }else{
                if(room.bankerSeat==room.maxPlayers){
                    if((room.bankerSeat-room.baochunSeat)>(room.bankerSeat-owner.seat)){
                        room.baochunSeat = owner.seat;
                    }
                }else{
                    if((room.baochunSeat-room.bankerSeat)<(owner.seat-room.bankerSeat)){
                        room.baochunSeat = owner.seat;
                    }
                }
            }
        }
        Global.logger.info("EXRoom room maxPlayers:"+ room.maxPlayers);
        Global.logger.info("EXRoom room bankerSeat:"+ room.bankerSeat);
        Global.logger.info("EXRoom room activeSeat:"+ room.activeSeat);
        Global.logger.info("Player room nextSeat:"+ owner.nextSeat);

        for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
            EXPlayer player = (EXPlayer) entry.getValue();
            if(player.seat!=room.bankerSeat){
                Global.logger.info("baochun Player id:"+ player.playerid);
                if (player.baochun==-1){
                    tmp = -1;
                }
            }
        }
       //

        //owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
        Global.logger.error("EXPlayerBaoChunState id:"+id);
        if (tmp==0){
            //全部选择完
            EXPlayer zjplayer = (EXPlayer) room.playerMapBySeat.get(room.bankerSeat);
            EXMainServer.gameCtr.changeActiveSeat(room, zjplayer.seat);
            if (room.baochunSeat>0){
                //有人报春，庄家是否抢？
                Global.logger.info("EXPlayerBaoChunState baochunSeat:"+room.baochunSeat);
                zjplayer.stateMachine.changeState(Global.getState(EXPlayerTongYiBaoChunState.class));
            }else{
                //无人报春，庄家先出
                Global.logger.info("EXPlayerBaoChunState 开牌:"+room.baochunSeat);
                zjplayer.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
            }
        }
    }

    private void _tyaction(EXPlayer owner,int id,int gid) {
        EXRoom room = owner.getRoom();
        if (id==0){
            //庄家自己报春，
            room.baochunSeat = room.bankerSeat;
            EXPlayer zjplayer = (EXPlayer) room.playerMapBySeat.get(room.bankerSeat);
            EXMainServer.gameCtr.changeActiveSeat(room, zjplayer.seat);
            zjplayer.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
        }else {
            //报春者报春
            for (Map.Entry<Integer, Player> entry : room.playerMapById.entrySet()) {
                EXPlayer player = (EXPlayer) entry.getValue();
                if(player.seat==room.baochunSeat){
                    EXMainServer.gameCtr.changeActiveSeat(room, player.seat);
                    player.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
                }
            }
        }
        owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
    }

    @Override
    public void execute(EXPlayer owner, String cmd, int gid, Object param) {
        super.execute(owner, cmd, gid, param);
        Global.logger.error("EXPlayerBaoChunState cmd: "+cmd);
        if (cmd.equals(EXActionEvent.EVENT_BAO_CHUN)) {
            ITObject netParam = (ITObject) param;
            int id = netParam.getInt("baochun");
            _action(owner, id, gid);
        }else if(cmd.equals(EXActionEvent.EVENT_TONGYI_BAO_CHUN)){
            ITObject netParam = (ITObject) param;
            int id = netParam.getInt("tongyi");
            _tyaction(owner, id, gid);
        }else if (cmd.equals(ActionEvent.EVENT_TIMER_AUTO) || cmd.equals(ActionEvent.EVENT_ENTRUST)) {
            _action(owner, 0, gid);
        } else if (cmd.equals(ActionEvent.EVENT_OFFLINE)) {
            owner.startActionTimer();
        }
    }
}