package wtf.beatrice.retrorender.engine.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;

import java.util.ArrayList;
import java.util.List;

public class PauseMenu {

    private final SpriteBatch batch;
    private final BitmapFont font;
    private final Texture pixel;

    // simple action enum for the menu
    public enum MenuAction {
        NONE,
        RESUME,
        SETTINGS,
        QUIT
    }

    // simple button model
    private static class Button {
        String label;
        MenuAction action;
        float x, y, w, h;

        Button(String label, MenuAction action) {
            this.label = label;
            this.action = action;
        }
    }

    private final List<Button> buttons = new ArrayList<>();

    public PauseMenu() {
        batch = new SpriteBatch();

        // Same style as DebugHud
        FreeTypeFontGenerator generator =
            new FreeTypeFontGenerator(Gdx.files.internal("fonts/red-hat-mono.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter param =
            new FreeTypeFontGenerator.FreeTypeFontParameter();

        param.size = 12;
        param.color = Color.WHITE;
        param.mono = true;
        param.hinting = FreeTypeFontGenerator.Hinting.None;
        param.borderWidth = 0f;
        param.borderColor = Color.WHITE;
        param.shadowOffsetX = 0;
        param.shadowOffsetY = 0;
        param.minFilter = Texture.TextureFilter.Nearest;
        param.magFilter = Texture.TextureFilter.Nearest;

        font = generator.generateFont(param);
        generator.dispose();

        // create buttons (ordered left → right)
        buttons.add(new Button(" Resume ",   MenuAction.RESUME));
        buttons.add(new Button(" Settings ", MenuAction.SETTINGS));
        buttons.add(new Button(" Quit ",     MenuAction.QUIT));

        // 1x1 pixel texture (white, we tint it)
        Pixmap pm = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        pm.setColor(1f, 1f, 1f, 1f);
        pm.fill();
        pixel = new Texture(pm);
        pm.dispose();
    }

    public void render(int width, int height) {
        batch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
        batch.begin();

        // --- black bar at bottom ---
        float barH = 80f;       // in retro pixels
        float barY = 0f;

        batch.setColor(0f, 0f, 0f, 0.9f);
        batch.draw(pixel, 0f, barY, width, barH);

        // --- buttons ---
        float spacing = 10f; // horizontal space between buttons

        // measure each button label
        GlyphLayout layout = new GlyphLayout();
        float totalWidth = 0f;
        float maxButtonH = 0f;

        for (Button b : buttons) {
            layout.setText(font, b.label);
            float bw = layout.width + 16f;  // padding
            float bh = layout.height + 12f;
            b.w = bw;
            b.h = bh;
            totalWidth += bw;
            if (bh > maxButtonH) maxButtonH = bh;
        }

        // add spacing between buttons
        if (!buttons.isEmpty()) {
            totalWidth += spacing * (buttons.size() - 1);
        }

        float startX = (width - totalWidth) / 2f;
        float centerY = barY + (barH - maxButtonH) / 2f;

        // draw each button
        float currentX = startX;
        for (Button b : buttons) {
            b.x = currentX;
            b.y = centerY;

            // button background
            batch.setColor(0.1f, 0.1f, 0.1f, 1f);
            batch.draw(pixel, b.x, b.y, b.w, b.h);

            // simple 1px white border
            batch.setColor(1f, 1f, 1f, 1f);
            float border = 1f;
            // top
            batch.draw(pixel, b.x, b.y + b.h - border, b.w, border);
            // bottom
            batch.draw(pixel, b.x, b.y, b.w, border);
            // left
            batch.draw(pixel, b.x, b.y, border, b.h);
            // right
            batch.draw(pixel, b.x + b.w - border, b.y, border, b.h);

            // text
            layout.setText(font, b.label);
            float textX = b.x + (b.w - layout.width) / 2f;
            float textY = b.y + b.h - (b.h - layout.height) / 2f - 2f;
            font.draw(batch, layout, textX, textY);

            currentX += b.w + spacing;
        }

        batch.end();
    }

    public MenuAction getActionAt(float x, float y) {
        for (Button b : buttons) {
            if (x >= b.x && x <= b.x + b.w &&
                y >= b.y && y <= b.y + b.h) {
                return b.action;
            }
        }
        return MenuAction.NONE;
    }

    public void dispose() {
        batch.dispose();
        font.dispose();
        pixel.dispose();
    }
}
