package pp.monopoly.client;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;

import pp.monopoly.game.client.ClientGameLogic;

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

    /**
     * Creates a new MonopolyAppState that is initially disabled.
     */
    protected MonopolyAppState() {
        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 = (MonopolyApp) application;
        if (isEnabled()) {
            enableState();
        }
    }

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

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

    /**
     * Sets the enabled state of the MonopolyAppState.
     * 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();
            }
        }
    }

    /**
     * Called when the state is enabled. Override to define specific behavior.
     */
    protected abstract void enableState();

    /**
     * Called when the state is disabled. Override to define specific behavior.
     */
    protected abstract void disableState();
}
