import QtQuick
import Quickshell
import Quickshell.Io
import qs.services
import qs.modules.interface.settings

Scope {
    id: global

    // Track if notification has already been shown
    property bool themeNotificationShown: false

    // Function to show notification once
    function notifyPredefinedTheme() {
        if (!themeNotificationShown) {
            themeNotificationShown = true
            Quickshell.execDetached([
                "notify-send",
                "Nucleus Shell",
                `You are using a predefined theme ${Config.runtime.appearance.colors.scheme}. Color generation/Light Theme is not supported for this theme.`,
                "--urgency=normal",
                "--expire-time=5000"
            ])
        }
    }

    // Handle Global IPCs
    IpcHandler {
        target: "global"

        function toggleTheme() {
            const currentTheme = Config.runtime.appearance.theme
            const newTheme = currentTheme === "light" ? "dark" : "light"

            // Predefined themes: validate variant BEFORE changing theme
            if (!Config.runtime.appearance.colors.autogenerated) {
                const scheme = Config.runtime.appearance.colors.scheme
                const file = Theme.map[scheme]?.[newTheme]

                if (!file) {
                    Theme.notifyMissingVariant(scheme, newTheme)
                    return
                }

                Config.updateKey("appearance.theme", newTheme)

                Quickshell.execDetached([
                    "bash",
                    Directories.scriptsPath + "/interface/switchTheme.sh",
                    file
                ])
                return
            }

            // Autogenerated themes: safe to toggle freely
            Config.updateKey("appearance.theme", newTheme)
            genThemeColors.running = true
        }

        function regenColors() {
            if (Config.runtime.appearance.colors.autogenerated) {
                genThemeColors.running = true
            } else {
                notifyPredefinedTheme()
            }
        }
    }

    property var genColorsCmd: Config.runtime.appearance.colors.runMatugenUserWide
        ? [
            "bash",
            Directories.scriptsPath + "/interface/gencolors.sh",
            "--user-wide",
            Config.runtime.appearance.background.path,
            Config.runtime.appearance.colors.matugenScheme,
            Config.runtime.appearance.theme,
            Quickshell.shellPath("extras/matugen/config.toml")
        ]
        : [
            "bash",
            Directories.scriptsPath + "/interface/gencolors.sh",
            Config.runtime.appearance.background.path,
            Config.runtime.appearance.colors.matugenScheme,
            Config.runtime.appearance.theme,
            Quickshell.shellPath("extras/matugen/config.toml")
        ];



    // Process to generate colors
    Process {
        id: genThemeColors

        command: genColorsCmd
    }

}
