#!/usr/bin/env bash
# chroma — the rice's control script.
#   chroma start         start wallpaper daemon, shell, idle, clipboard (run at login)
#   chroma restart       restart the quickshell shell
#   chroma wall <image>  set wallpaper + regenerate the whole color scheme
#   chroma look          re-apply hypr/look.lua + wallpaper border colors
#   chroma fetch logo|picture|toggle   what fastfetch shows next to the info

CHROMA="$HOME/.config/chroma"
STATE="$HOME/.local/state/chroma"
WALLDIR="$HOME/Pictures/Themes"
mkdir -p "$STATE"

hl_exec() { hyprctl eval "hl.exec_cmd([[$1]])" >/dev/null; }   # let Hyprland own the process

current_wall() {
    local w; w=$(cat "$STATE/wallpaper" 2>/dev/null)
    [[ -f "$w" ]] || w=$(find "$WALLDIR" -maxdepth 1 -type f \( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.webp' \) | sort | head -1)
    echo "$w"
}

# Regenerate colors from the wallpaper (matugen runs the template post-hooks)
gen_colors() {
    matugen image "$1" -c "$CHROMA/matugen/config.toml" -m dark --source-color-index 0 >/dev/null 2>"$STATE/matugen.log"
}

# hyprctl eval treats a leading "--" as a flag, hence the leading space
apply_look()   { hyprctl eval " $(cat "$CHROMA/hypr/look.lua")" >/dev/null; apply_colors; }
apply_colors() { [[ -f "$STATE/hypr-colors.lua" ]] && hyprctl eval " $(cat "$STATE/hypr-colors.lua")" >/dev/null; }

set_wall() {
    local img; img=$(realpath -s "$1")
    echo "$img" > "$STATE/wallpaper"
    awww img "$img" --transition-type wipe --transition-angle 30 \
        --transition-step 90 --transition-fps 165 --transition-duration 1.2 >/dev/null 2>&1
    gen_colors "$img"
    "$CHROMA/bin/chroma-analyze" "$img" >/dev/null 2>>"$STATE/matugen.log"
    apply_colors
}

start_shell() {
    quickshell -p "$CHROMA/quickshell" kill >/dev/null 2>&1 || true
    hl_exec "env QS_ICON_THEME=breeze-dark quickshell -p $CHROMA/quickshell"
}

start() {
    "$CHROMA/bin/chroma-init"
    pgrep -u "$(id -u)" -x awww-daemon >/dev/null || hl_exec "awww-daemon"
    for _ in $(seq 50); do awww query >/dev/null 2>&1 && break; sleep 0.1; done

    local w; w=$(current_wall)
    # The release ships the exact captured palette; do not regenerate it at login.
    [[ -n "$w" ]] && awww img "$w" --transition-type none
    apply_colors

    start_shell
    # The live user has no password; enable idle locking after installation.
    if [[ ! -d /run/archiso/bootmnt ]]; then
        pgrep -u "$(id -u)" -x hypridle >/dev/null || hl_exec "hypridle -c $CHROMA/hypr/hypridle.conf"
    fi
    pgrep -u "$(id -u)" -f "watch cliphist store" >/dev/null || {
        hl_exec "wl-paste --type text --watch cliphist store"
        hl_exec "wl-paste --type image --watch cliphist store"
    }
}

case "$1" in
    start)   start ;;
    restart) start_shell ;;
    wall)    [[ -f "$2" ]] || { echo "usage: chroma wall <image>"; exit 1; }; set_wall "$2" ;;
    look)    apply_look ;;
    fetch)   cur=$(cat "$STATE/fetch-mode" 2>/dev/null || echo logo)
             case "$2" in
                 logo|picture) echo "$2" > "$STATE/fetch-mode" ;;
                 toggle|"")    [[ $cur == logo ]] && echo picture > "$STATE/fetch-mode" || echo logo > "$STATE/fetch-mode" ;;
                 *) echo "usage: chroma fetch logo|picture|toggle"; exit 1 ;;
             esac
             echo "fastfetch shows: $(cat "$STATE/fetch-mode")" ;;
    *)       sed -n '2,7p' "$0" | sed 's/^# \{0,1\}//'; exit 1 ;;
esac
