import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.UrlLocator;
import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapText;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

/** Hello Assets: load a glTF model, texture, and font with the AssetManager. */
public class HelloAssets extends SimpleApplication {
    public static void main(String[] args) {
        HelloAssets app = new HelloAssets();
        app.setShowSettings(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        setDisplayStatView(false);
        setDisplayFps(false);
        viewPort.setBackgroundColor(new ColorRGBA(.12f, .16f, .22f, 1));
        cam.setLocation(new Vector3f(0, 1.5f, 8));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);

        // The demo loads an official sample remotely; package assets in a game.
        assetManager.registerLocator(
                "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/refs/heads/main/",
                UrlLocator.class);
        Spatial model = assetManager.loadModel("Models/BoomBox/glTF-Binary/BoomBox.glb");
        model.updateGeometricState();
        BoundingBox bounds = new BoundingBox();
        bounds.mergeLocal(model.getWorldBound());
        float height = Math.max(0.001f, bounds.getYExtent() * 2f);
        model.setLocalScale(2.5f / height);
        rootNode.attachChild(model);

        // A textured box demonstrates loading an image from the classpath.
        Geometry wall = new Geometry("Wall", new Box(1, 1, .1f));
        Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setTexture("ColorMap", assetManager.loadTexture("Common/Textures/dot.png"));
        wall.setMaterial(material);
        wall.setLocalTranslation(-2.5f, 0, -1);
        rootNode.attachChild(wall);

        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-1, -2, -1).normalizeLocal());
        sun.setColor(ColorRGBA.White);
        rootNode.addLight(sun);
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(ColorRGBA.White.mult(.4f));
        rootNode.addLight(ambient);

        BitmapText text = new BitmapText(guiFont);
        text.setText("Hello Assets");
        text.setLocalTranslation(20, text.getLineHeight() + 20, 0);
        guiNode.attachChild(text);
    }
}
