////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////

package pp.battleship.client;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import pp.battleship.game.client.ClientGameLogic;

/**
 * Abstract class representing a state in the Battleship game.
 * Extends the AbstractAppState from jMonkeyEngine to manage state behavior.
 */
public abstract class BattleshipAppState extends AbstractAppState {
    private BattleshipApp app;

    /**
     * Creates a new BattleshipAppState that is initially disabled.
     *
     * @see #setEnabled(boolean)
     */
    protected BattleshipAppState() {
        setEnabled(false);
    }

    /**
     * Initializes the state manager and application.
     *
     * @param stateManager The state manager
     * @param application  The application instance
     */
    @Override
    public void initialize(AppStateManager stateManager, Application application) {
        super.initialize(stateManager, application);
        this.app = (BattleshipApp) application;
        if (isEnabled()) enableState();
    }

    /**
     * Returns the BattleshipApp instance associated with this BattleshipAppState.
     *
     * @return The BattleshipApp instance.
     */
    public BattleshipApp getApp() {
        return app;
    }

    /**
     * Returns the client game logic handler.
     *
     * @return the client game logic handler
     */
    public ClientGameLogic getGameLogic() {
        return app.getGameLogic();
    }

    /**
     * Checks if any dialog is currently displayed.
     *
     * @return true if any dialog is currently shown, false otherwise
     */
    public boolean showsDialog() {
        return app.getDialogManager().showsDialog();
    }

    /**
     * Sets the enabled state of the BattleshipAppState.
     * If the new state is the same as the current state, the method returns.
     *
     * @param enabled The new enabled state.
     */
    @Override
    public void setEnabled(boolean enabled) {
        if (isEnabled() == enabled) return;
        super.setEnabled(enabled);
        if (app != null) {
            if (enabled)
                enableState();
            else
                disableState();
        }
    }

    /**
     * This method is called when the state is enabled.
     * It is meant to be overridden by subclasses to perform
     * specific actions when the state is enabled.
     */
    protected abstract void enableState();

    /**
     * This method is called when the state is disabled.
     * It is meant to be overridden by subclasses to perform
     * specific actions when the state is disabled.
     */
    protected abstract void disableState();
}
