package de.blight.game;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.system.AppSettings;
import de.blight.game.config.*;
import de.blight.game.scene.WorldScene;

public class BlightApp extends SimpleApplication {

    private KeyBindings      keyBindings;
    private GraphicsSettings graphicsSettings;
    private WorldScene       worldScene;
    private ConfigScreen     configScreen;
    private GraphicsScreen   graphicsScreen;
    private PauseMenu        pauseMenu;

    public static void main(String[] args) {
        BlightApp app = new BlightApp();

        GraphicsSettings gs = GraphicsStore.load();
        AppSettings settings = new AppSettings(true);
        settings.setTitle("Blight");
        settings.setResolution(gs.width, gs.height);
        settings.setFullscreen(gs.fullscreen);
        settings.setVSync(gs.vsync);
        settings.setSamples(gs.samples);

        app.setSettings(settings);
        app.setShowSettings(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        flyCam.setEnabled(false);
        inputManager.deleteMapping(INPUT_MAPPING_EXIT);

        keyBindings      = KeyBindingStore.load();
        graphicsSettings = GraphicsStore.load();

        worldScene = new WorldScene(keyBindings);
        stateManager.attach(worldScene);

        configScreen = new ConfigScreen(keyBindings, () -> worldScene.reloadBindings(keyBindings));
        configScreen.setOnClose(() -> pauseMenu.setEnabled(true));
        stateManager.attach(configScreen);
        configScreen.setEnabled(false);

        graphicsScreen = new GraphicsScreen(graphicsSettings, () -> pauseMenu.setEnabled(true));
        stateManager.attach(graphicsScreen);
        graphicsScreen.setEnabled(false);

        pauseMenu = new PauseMenu(
            () -> { pauseMenu.setEnabled(false); graphicsScreen.setEnabled(true); },
            () -> { pauseMenu.setEnabled(false); configScreen.setEnabled(true); }
        );
        stateManager.attach(pauseMenu);
        pauseMenu.setEnabled(false);

        inputManager.addMapping("ToggleMenu", new KeyTrigger(KeyInput.KEY_ESCAPE));
        inputManager.addListener((ActionListener) (name, isPressed, tpf) -> {
            if (!isPressed) return;

            if (graphicsScreen.isEnabled()) {
                // GraphicsScreen wird nur über seine eigenen Buttons geschlossen
                return;
            }
            if (configScreen.isEnabled()) {
                if (configScreen.isWaiting()) {
                    configScreen.cancelWaiting();
                } else {
                    configScreen.setEnabled(false);
                    pauseMenu.setEnabled(true);
                }
                return;
            }
            if (pauseMenu.isEnabled()) {
                pauseMenu.setEnabled(false);
                worldScene.setPaused(false);
                return;
            }
            pauseMenu.setEnabled(true);
            worldScene.setPaused(true);
        }, "ToggleMenu");
    }

    @Override
    public void simpleUpdate(float tpf) {}
}
