package extend.zp.player.state;

import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.player.state.PlayerPauseState;
import com.game.state.StateBase;
import com.taurus.permanent.TPServer;

import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.PlayerRuleManager;


public class EXPlayerTianHuState extends StateBase<EXPlayer> {
    @Override
    public void enter(EXPlayer owner) {

        EXRoom room = owner.getRoom();

        room.isAllPass = false;

        //启动定时器
        timer(owner);
    }

    @Override
    public void toNextState(EXPlayer owner) {

        if (!owner.getRoom().isAllPass) {

            //根据规则判定自己能不能胡
            EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.HU_RULE, owner);

            // 如果有玩家能胡
            if (owner.getRoom().tipMap.size() != 0) {

                if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
                    owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
                }
                return;
            }
        }

        owner.getRoom().isTianHu = false;

        owner.stateMachine.changeState(Global.getState(EXPlayerPreDrawState.class));
    }

    public static void timer(EXPlayer owner) {

        TPServer.me().getTimerPool().schedule(new Runnable() {

            @Override
            public void run() {

                if (owner.room == null) {
                    Global.logger.error("EXPlayerDiscardState room is null");
                    return;
                }

                if (owner.room.isDestroy)
                    return;

                owner.room.enqueueRunnable(new Runnable() {

                    @Override
                    public void run() {
                        owner.stateMachine.toNextState();
                    }
                });
            }
        }, 500, TimeUnit.MILLISECONDS);
    }
}
