#!/usr/bin/env bash

# ? This Script sets the hypr meta data for the current theme
# ? This way users have a way to look up the current theme
# ? See theme.switch.sh and dconf.set.sh for the actual theme settings

[[ "${HYDE_SHELL_INIT}" -ne 1 ]] && eval "$(hyde-shell init)"

if [[ "${WALLBASH_STARTUP:-0}" -eq 1 ]]; then
    exit 0
fi

confDir="${confDir:-$XDG_CONFIG_HOME}"
cacheDir="${cacheDir:-$XDG_CACHE_HOME/hyde}"
HYDE_THEME="${HYDE_THEME:-}"
HYDE_THEME_DIR="${HYDE_THEME_DIR:-$confDir/hyde/themes/$HYDE_THEME}"
enableWallDcol="${enableWallDcol:-0}"

eval "$(
    hyq "${HYDE_THEME_DIR}/hypr.theme" \
        --export env \
        -Q "\$GTK_THEME[string]" \
        -Q "\$COLOR_SCHEME[string]" \
        -Q "\$ICON_THEME[string]" \
        -Q "\$CURSOR_THEME[string]" \
        -Q "\$CURSOR_SIZE[int]" \
        -Q "\$FONT[string]" \
        -Q "\$FONT_SIZE[int]" \
        -Q "\$DOCUMENT_FONT[string]" \
        -Q "\$DOCUMENT_FONT_SIZE[int]" \
        -Q "\$MONOSPACE_FONT[string]" \
        -Q "\$MONOSPACE_FONT_SIZE[int]" \
        -Q "\$CODE_THEME[string]"
)"

# Validate the theme configuration file
cat <<WALLBASH >"${confDir}/hypr/themes/wallbash.conf"
# Auto-generated by HyDE // Read-only
# // ----------------------------
# HyDE Theme: ${HYDE_THEME}
# Configuration File: "${HYDE_THEME_DIR}/hypr.theme"
# Wallbash Mode : $(sed -e 's/^0$/theme/' -e 's/^1$/auto/' -e 's/^2$/dark/' -e 's/^3$/light/' <<<"${enableWallDcol}")
# // ----------------------------

\$HYDE_THEME=${HYDE_THEME}
\$GTK_THEME=${__GTK_THEME:-\$GTK_THEME}
\$COLOR_SCHEME=${__COLOR_SCHEME:-\$COLOR_SCHEME}
\$ICON_THEME=${__ICON_THEME}

\$CURSOR_THEME=${__CURSOR_THEME:-\$CURSOR_THEME}
\$CURSOR_SIZE=${__CURSOR_SIZE:-\$CURSOR_SIZE}

\$FONT=${__FONT:-\$FONT}
\$FONT_SIZE=${__FONT_SIZE:-\$FONT_SIZE}
\$DOCUMENT_FONT=${__DOCUMENT_FONT:-\$DOCUMENT_FONT}
\$DOCUMENT_FONT_SIZE=${__DOCUMENT_FONT_SIZE:-\$DOCUMENT_FONT_SIZE}
\$MONOSPACE_FONT=${__MONOSPACE_FONT:-\$MONOSPACE_FONT}
\$MONOSPACE_FONT_SIZE=${__MONOSPACE_FONT_SIZE:-\$MONOSPACE_FONT_SIZE}

\$CODE_THEME=${__CODE_THEME:-\$CODE_THEME}  
\$SDDM_THEME=${__SDDM_THEME:-\$CODE_THEME}

# // ----------------------------
# README:
# Values above are derived and sanitized from the theme.conf file,
# This is to ensure themes won't have any 'exec' or 'source'
# commands that could potentially harm the system
#  or undesired behavior.
#
# Dear Theme Owner:
# You can still add your own custom 'exec' or 'source' commands
#  by adding it as variable, examples (you can name the variable anything):
# Note that you should indicate it in your README.md
#
#
# -- ⌨️ theme.conf --
# \$RUN_CMD="some_command"
# \$SOURCE_FILE="/some/files"
#
#
# -- ⌨️ userprefs.conf --
# exec = \${RUN_CMD}"
# source = \${SOURCE_FILE}
# exec = hyde-shell walbash code \$CODE_THEME # Setting the code theme

# // ----------------------------
WALLBASH

if grep -q "#//---Wallbash mode enabled---" "${confDir}/hypr/themes/wallbash.conf"; then
    # Remove lines below the detected line
    sed -i '/#\/\/---Wallbash mode enabled---/,$d' "${confDir}/hypr/themes/wallbash.conf"
fi

if [[ "${enableWallDcol}" -gt 0 ]]; then
    cat <<ON_WALLBASH >>"${confDir}/hypr/themes/wallbash.conf"

#//---Wallbash mode enabled---
# Overriding values above

\$GTK_THEME = Wallbash-Gtk
\$COLOR_SCHEME = ${COLOR_SCHEME}

general {
    col.active_border = rgba(\$wallbash_pry4ff) rgba(\$wallbash_4xa1ff) 45deg
    col.inactive_border = rgba(\$wallbash_pry1ff) rgba(\$wallbash_pry2ff) 45deg
}

group {
    col.border_active = rgba(\$wallbash_pry4ff) rgba(\$wallbash_4xa1ff) 45deg
    col.border_inactive = rgba(\$wallbash_pry1cc) rgba(\$wallbash_pry2cc) 45deg
    col.border_locked_active = rgba(\$wallbash_txt3ff) rgba(\$wallbash_txt4ff) 45deg
    col.border_locked_inactive = rgba(\$wallbash_txt1cc) rgba(\$wallbash_txt2cc) 45deg
}

ON_WALLBASH

fi
