package extend.pk.player.state;

import com.game.ActionEvent;
import com.game.Global;
import com.game.data.Player;
import com.game.state.StateBase;
import com.taurus.core.entity.ITObject;
import extend.pk.EXActionEvent;
import extend.pk.EXMainServer;
import extend.pk.EXPlayer;
import extend.pk.EXRoom;

import java.util.Map;


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

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

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

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

        EXMainServer.gameCtr.tongYiBaoChunTipEvent(owner,1);

        owner.startActionTimer();
    }

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


    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("EXPlayerTongYiBaoChunState cmd: "+cmd);
        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_OFFLINE)) {
            owner.startActionTimer();
        }
    }
}