package de.blight.editor;

import com.jme3.app.SimpleApplication;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeContext;
import de.blight.editor.state.TerrainEditorState;
import javafx.scene.image.WritableImage;

public class JmeEditorApp extends SimpleApplication {

    private final SharedInput  input;
    private final WritableImage jfxImage;

    public JmeEditorApp(SharedInput input, WritableImage jfxImage) {
        this.input    = input;
        this.jfxImage = jfxImage;
    }

    /** Startet JME3 in einem Daemon-Thread (blockierend bis App endet). */
    public static JmeEditorApp launch(SharedInput input, WritableImage jfxImage,
                                     int vpWidth, int vpHeight) {
        JmeEditorApp app = new JmeEditorApp(input, jfxImage);

        AppSettings settings = new AppSettings(true);
        settings.setTitle("Blight Editor – JME3");
        settings.setResolution(vpWidth, vpHeight);
        settings.setRenderer(AppSettings.LWJGL_OPENGL32);
        settings.setAudioRenderer(null);
        settings.setSamples(4);

        app.setSettings(settings);
        app.setShowSettings(false);
        app.setPauseOnLostFocus(false);

        Thread t = new Thread(() -> app.start(JmeContext.Type.OffscreenSurface), "jme3-editor");
        t.setDaemon(true);
        t.start();
        return app;
    }

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

        // Frame-Export in das JavaFX-WritableImage
        viewPort.addProcessor(new FrameTransfer(jfxImage));

        stateManager.attach(new TerrainEditorState(input));
    }

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