package extend.pk.player.state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.game.ActionEvent;
import com.game.Global;
import com.game.Router;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;

import com.taurus.core.entity.TObject;
import extend.pk.CardGroup;
import extend.pk.CardObj;
import extend.pk.Config;
import extend.pk.EXActionEvent;
import extend.pk.EXMainServer;
import extend.pk.EXPlayer;
import extend.pk.EXRoom;
import extend.pk.RoomCard;
import extend.pk.room.state.EXRoomSetpState;
import extend.pk.uitl.CardCheck;
import extend.pk.uitl.CardConfig;
import extend.pk.uitl.CardUtil;

public class EXPlayerQiangZhuangState extends StateBase<EXPlayer> {

    @Override
    public void enter(EXPlayer owner) {
        if (owner.qiangZhuang != -1)
        {
            owner.getRoom().activeSeat = 0;
            owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
            owner.getRoom().stateMachine.changeState(Global.getState(EXRoomSetpState.class));
            return;
        }
        if(!owner.isEntrust()) {
            EXMainServer.gameCtr.qiangZhuangTipEvent(owner,0);
        }
        owner.startActionTimer();
    }

    /**
     * 重连
     *
     * @param owner
     */
    @Override
    public void reload(EXPlayer owner) {
        EXMainServer.gameCtr.qiangZhuangTipEvent(owner,1);
        owner.startActionTimer();
    }

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

    private void _action(EXPlayer owner,int id,int gid) {
        owner.qiangZhuang = id;

        owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
        //EXMainServer.gameCtr.qiangZhuangEvent(owner);

        if (id == 0)
        {
            owner.getRoom().stateMachine.execute(EXActionEvent.EVENT_QIANG_ZHUANG, gid, null);
        }
        else if (id > 0)
        {
            owner.getRoom().activeSeat = 0;
            owner.getRoom().bankerSeat = owner.seat;
            EXMainServer.gameCtr.qiangZhuangEvent(owner);
            owner.getRoom().stateMachine.changeState(Global.getState(EXRoomSetpState.class));
        }
    }

    @Override
    public void execute(EXPlayer owner, String cmd, int gid, Object param) {
        super.execute(owner, cmd, gid, param);
        if (cmd.equals(EXActionEvent.EVENT_QIANG_ZHUANG)) {
            ITObject netParam = (ITObject) param;
            int id = netParam.getInt("qiang_zhuang");
            _action(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();
        }
    }
}