package wtf.beatrice.retrorender.engine.ui;

import wtf.beatrice.retrorender.engine.config.GameSettings;

public class GameUi {

    public enum UiResult {
        NONE,
        RESUME,
        OPEN_SETTINGS,
        CLOSE_SETTINGS,
        QUIT
    }

    private final PauseMenu pauseMenu;
    private final SettingsMenu settingsMenu;
    private final GameSettings settings;

    public GameUi(GameSettings settings) {
        this.settings = settings;
        this.pauseMenu = new PauseMenu();
        this.settingsMenu = new SettingsMenu(settings);
    }

    public UiMode onEsc(UiMode current) {
        switch (current) {
            case GAMEPLAY:
                return UiMode.PAUSE;
            case PAUSE:
            case SETTINGS:
                return UiMode.GAMEPLAY;
            default:
                return UiMode.GAMEPLAY;
        }
    }

    public void render(int retroW, int retroH, UiMode mode) {
        switch (mode) {
            case PAUSE:
                pauseMenu.render(retroW, retroH);
                break;
            case SETTINGS:
                settingsMenu.render(retroW, retroH);
                break;
            case GAMEPLAY:
            default:
                break;
        }
    }

    public UiResult handleClick(float retroX, float retroY, UiMode mode) {
        if (mode == UiMode.SETTINGS) {
            boolean close = settingsMenu.handleClick(retroX, retroY);
            // always sync FOV to settings
            settings.fov = settingsMenu.getFov();
            settings.bobbingEnabled = settingsMenu.isBobbingEnabled();

            if (close) {
                // Settings “Close” button clicked -> go back to PAUSE
                return UiResult.CLOSE_SETTINGS;
            }
            return UiResult.NONE;
        }

        if (mode == UiMode.PAUSE) {
            PauseMenu.MenuAction action = pauseMenu.getActionAt(retroX, retroY);
            return switch (action)
            {
                case RESUME -> UiResult.RESUME;
                case QUIT -> UiResult.QUIT;
                case SETTINGS -> UiResult.OPEN_SETTINGS;
                default -> UiResult.NONE;
            };
        }

        return UiResult.NONE;
    }

    public void dispose() {
        pauseMenu.dispose();
        settingsMenu.dispose();
    }

    public SettingsMenu getSettingsMenu() {
        return settingsMenu;
    }
}
