package wtf.beatrice.retrorender.engine.world;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.*;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;

public class ModelLibrary {

    private final ModelBuilder builder = new ModelBuilder();

    public final Texture groundTexture;
    public final Texture pathTexture;
    public final Texture houseTexture;   // <-- new

    public final Model   groundModel;
    public final Model   unitCubeModel;

    // town-related primitives
    public final Model   houseBlockModel;
    public final Model   pathTileModel;

    public ModelLibrary() {
        // --- ground texture + model ---
        groundTexture = new Texture(Gdx.files.internal("textures/rocky_grass_2.png"));
        groundTexture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
        groundTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

        float halfSize  = 32f;
        float tileScale = 16f;

        builder.begin();
        Material groundMat = new Material(
            ColorAttribute.createDiffuse(1f, 1f, 1f, 1f),
            TextureAttribute.createDiffuse(groundTexture)
        );

        MeshPartBuilder mpb = builder.part(
            "ground",
            GL20.GL_TRIANGLES,
            VertexAttributes.Usage.Position
                | VertexAttributes.Usage.Normal
                | VertexAttributes.Usage.TextureCoordinates,
            groundMat
        );
        mpb.setUVRange(0f, 0f, tileScale, tileScale);
        mpb.rect(
            -halfSize, 0f,  halfSize,
            halfSize, 0f,  halfSize,
            halfSize, 0f, -halfSize,
            -halfSize, 0f, -halfSize,
            0f, 1f, 0f
        );
        groundModel = builder.end();

        // generic cube (still untextured)
        unitCubeModel = builder.createBox(
            1f, 1f, 1f,
            new Material(ColorAttribute.createDiffuse(1f, 1f, 1f, 1f)),
            VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal
        );

        // --- house texture + model ---
        // assumes textures/texture_09.png exists
        houseTexture = new Texture(Gdx.files.internal("textures/texture_03.png"));
        houseTexture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
        houseTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

        houseBlockModel = builder.createBox(
            4f, 3f, 4f,
            new Material(
                TextureAttribute.createDiffuse(houseTexture)
                // optionally tint:
                // ColorAttribute.createDiffuse(0.9f, 0.8f, 0.7f, 1f)
            ),
            VertexAttributes.Usage.Position
                | VertexAttributes.Usage.Normal
                | VertexAttributes.Usage.TextureCoordinates   // <-- important
        );

        // --- path tile texture + model ---
        pathTexture = new Texture(Gdx.files.internal("textures/paving_2.png"));
        pathTexture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
        pathTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

        builder.begin();
        Material pathMat = new Material(
            TextureAttribute.createDiffuse(pathTexture)
        );

        MeshPartBuilder pathMpb = builder.part(
            "pathTile",
            GL20.GL_TRIANGLES,
            VertexAttributes.Usage.Position
                | VertexAttributes.Usage.Normal
                | VertexAttributes.Usage.TextureCoordinates,
            pathMat
        );

        float tHalf = 1f;   // 2x2 tile
        float y     = 0.01f;

        pathMpb.setUVRange(0f, 0f, 0.5f, 0.5f);
        pathMpb.rect(
            -tHalf, y,  tHalf,
            tHalf, y,  tHalf,
            tHalf, y, -tHalf,
            -tHalf, y, -tHalf,
            0f, 1f, 0f
        );
        pathTileModel = builder.end();
    }

    public void dispose() {
        groundModel.dispose();
        unitCubeModel.dispose();
        houseBlockModel.dispose();
        pathTileModel.dispose();

        groundTexture.dispose();
        pathTexture.dispose();
        houseTexture.dispose(); // <-- new
    }
}
