package p.studio.utilities;

import javafx.scene.text.Font;

import java.util.Objects;

public class ThemeService {
    private static final String THEME_PATH = "/themes/";
    private static final String DEFAULT_THEME = THEME_PATH + "default-prometeu.css";
    private static final String FONT_PATH = "/fonts/jetbrain-mono-3.2/fonts/ttf/";
    private static final String[] BUNDLED_FONTS = {
            "JetBrainsMono-Regular.ttf",
            "JetBrainsMono-Medium.ttf",
            "JetBrainsMono-Bold.ttf",
            "JetBrainsMono-Italic.ttf",
            "JetBrainsMono-MediumItalic.ttf",
            "JetBrainsMono-BoldItalic.ttf"
    };
    private static boolean fontsLoaded;

    public String getDefaultTheme() {
        ensureBundledFontsLoaded();
        return Objects.requireNonNull(getClass().getResource(DEFAULT_THEME)).toExternalForm();
    }

    private static synchronized void ensureBundledFontsLoaded() {
        if (fontsLoaded) {
            return;
        }
        for (final String fontFile : BUNDLED_FONTS) {
            try (final var stream = ThemeService.class.getResourceAsStream(FONT_PATH + fontFile)) {
                Font.loadFont(Objects.requireNonNull(stream, "missing bundled font: " + fontFile), 14);
            } catch (final Exception exception) {
                throw new IllegalStateException("unable to load bundled font: " + fontFile, exception);
            }
        }
        fontsLoaded = true;
    }
}
