#!/usr/bin/env bash
# Generate theme.png preview images for each theme using ImageMagick.
# Reads the theme's SCSS color variables and renders a simple page mockup.
set -euo pipefail

cd "$(dirname "$0")/../themes"

gen_preview() {
    local dir="$1"
    local scss="$dir/css/theme.scss"
    if [[ ! -f "$scss" ]]; then
        echo "Skip $dir (no theme.scss)"
        return
    fi

    local header_bg header_font nav_bg nav_font sidebar_bg sidebar_border
    header_bg=$(grep -oE '^\$header-bg:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#0a369d")
    header_font=$(grep -oE '^\$header-font:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#ffffff")
    nav_bg=$(grep -oE '^\$nav-bg:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#2754b4")
    nav_font=$(grep -oE '^\$nav-font:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#ffffff")
    sidebar_bg=$(grep -oE '^\$sidebar-bg:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#f8f9fa")
    sidebar_border=$(grep -oE '^\$sidebar-border:\s*[^;]+;' "$scss" | grep -oE '#[0-9a-fA-F]{6}' || echo "#dee2e6")

    local size=640x400
    local body_bg="#ffffff"

    # Header bar
    convert -size "$size" xc:"$body_bg" \
        -fill "$header_bg" -draw "rectangle 0,0 639,70" \
        -fill "$nav_bg"    -draw "rectangle 0,70 639,95" \
        -fill "$sidebar_bg" -draw "rectangle 0,95 180,399" \
        -fill "$header_font" -font DejaVu-Sans-Bold -pointsize 20 -annotate +15+22 "$(basename "$dir")" \
        -fill "$header_font" -font DejaVu-Sans -pointsize 13 -annotate +15+45 "CodePress" \
        -fill "$nav_font" -font DejaVu-Sans -pointsize 12 -annotate +15+85 "Home   |   Over   |   Blog" \
        -fill "#666666" -font DejaVu-Sans -pointsize 14 -annotate +200+130 "Welkom bij $(basename "$dir")" \
        -fill "#999999" -font DejaVu-Sans -pointsize 11 -annotate +200+155 "Dit is een voorbeeld van het thema." \
        -fill "#cccccc" -draw "rectangle 200,400 639,399" \
        "$dir/theme.png"

    echo "Generated $dir/theme.png"
}

for d in */; do
    d="${d%/}"
    gen_preview "$d"
done
