#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"

cp "$SCRIPT_DIR/colors/Colors.qml.template" "$SCRIPT_DIR/colors/Colors.qml"

# ----------------------------
# UI helpers
# ----------------------------
info()  { echo -e "\033[1;34m[INFO]\033[0m $*"; }
ok()    { echo -e "\033[1;32m[ OK ]\033[0m $*"; }
warn()  { echo -e "\033[1;33m[WARN]\033[0m $*"; }
fail()  { echo -e "\033[1;31m[FAIL]\033[0m $*"; }

download() {
  local url="$1"
  local out="$2"
  local name="${3:-$(basename "$out")}"

  info "Downloading $name"

  if curl -fL --progress-bar "$url" -o "$out"; then
    ok "Saved $name"
  else
    fail "Failed $name"
    return 1
  fi
}

transform_svg_white() {
  local url="$1"
  local out="$2"
  local name="${3:-$(basename "$out")}"

  info "Fetching + whitening $name"

  if curl -fsSL "$url" \
    | sed -E 's/#[0-9a-fA-F]{3,6}/#ffffff/g' \
    > "$out"; then
    ok "Generated $name"
  else
    fail "Failed $name"
    return 1
  fi
}

# ----------------------------
# Start
# ----------------------------
echo
info "Preparing wlogout icons..."
mkdir -p "$SCRIPT_DIR/modules/wlogout/icons"
mkdir -p "$SCRIPT_DIR/colors/icons"

# ----------------------------
# Icons (wlogout)
# ----------------------------
transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/src/actions/symbolic/system-hibernate-symbolic.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/hibernate.svg" \
  "hibernate.svg"

transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/bold/actions/symbolic/system-lock-screen-symbolic.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/lock.svg" \
  "lock.svg"

transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/src/status/symbolic/logout-symbolic.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/logout.svg" \
  "logout.svg"

transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/src/actions/symbolic/system-reboot-symbolic.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/reboot.svg" \
  "reboot.svg"

transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/src/actions/symbolic/system-shutdown-symbolic.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/shutdown.svg" \
  "shutdown.svg"

transform_svg_white \
  "https://raw.githubusercontent.com/vinceliuice/Colloid-icon-theme/main/src/actions/24/system-suspend.svg" \
  "$SCRIPT_DIR/modules/wlogout/icons/suspend.svg" \
  "suspend.svg"

# ----------------------------
# UI icons
# ----------------------------
echo
info "Preparing global icons..."
mkdir -p "$SCRIPT_DIR/colors/icons"

download \
  "https://raw.githubusercontent.com/Tekh-ops/Garuda-Linux-Icons/refs/heads/master/actions/scalable/window-close.svg" \
  "$SCRIPT_DIR/colors/icons/wrong.svg" \
  "wrong.svg"

download \
  "https://raw.githubusercontent.com/Tekh-ops/Garuda-Linux-Icons/refs/heads/master/actions/scalable/dialog-ok-apply.svg" \
  "$SCRIPT_DIR/colors/icons/correct.svg" \
  "correct.svg"

# ----------------------------
# Example Wallpaper
# ----------------------------
echo
info "Preparing wallpaper..."

WALLPAPER_DIR=$(scripts/wallpaper_dir.sh)

# 5. Ensure it exists
mkdir -p "$WALLPAPER_DIR"

info "Copying example Wallpaper..."

# 6. Copy example wallpaper
cp $SCRIPT_DIR/colors/example_background.png "$WALLPAPER_DIR/japan_old_emperor_stream_with_bridge.png"

echo
ok "All assets prepared successfully."