#!/bin/bash

# omarchy:summary=Open the Omarchy theme switcher

print_rows=false

# The shell's picker opens from rows it already holds and runs this to refresh
# them, so the theme list never waits on the index below.
if [[ $1 == "--print-rows" ]]; then
  print_rows=true
  shift
fi

USER_THEMES_PATH="$HOME/.config/omarchy/themes"
OMARCHY_THEMES_PATH="$OMARCHY_PATH/themes"
CACHE_PATH="${XDG_CACHE_HOME:-$HOME/.cache}/omarchy/theme-selector"
preview_dir="$CACHE_PATH/previews"
fast_signature_file="$CACHE_PATH/fast-signature"

mkdir -p "$preview_dir"

find_preview() {
  local theme_path="$1"
  local ext match best=""
  local -a matches=()

  shopt -s nullglob nocaseglob
  for ext in png jpg jpeg webp gif bmp mp4 m4v mov webm mkv avi; do
    matches=("$theme_path/"[p]review."$ext")
    for match in "${matches[@]}"; do
      if [[ -f $match ]]; then
        shopt -u nocaseglob
        printf '%s\n' "$match"
        return
      fi
    done
  done

  if [[ -d $theme_path/backgrounds ]]; then
    for ext in jpg jpeg png gif bmp webp mp4 m4v mov webm mkv avi; do
      for match in "$theme_path/backgrounds"/*."$ext"; do
        [[ -f $match ]] || continue
        if [[ -z $best || $match < $best ]]; then
          best="$match"
        fi
      done
    done
  fi
  shopt -u nocaseglob

  [[ -n $best ]] && printf '%s\n' "$best"
}

add_theme_preview() {
  local theme_name="$1"
  local preview="$2"
  local extension="${preview##*.}"
  extension="${extension,,}"

  [[ -n $preview ]] || return
  [[ -e $preview_dir/$theme_name.$extension ]] && return

  ln -s "$preview" "$preview_dir/$theme_name.$extension"
}

shopt -s nullglob
sig_dirs=()
for theme_dir in "$USER_THEMES_PATH" "$OMARCHY_THEMES_PATH"; do
  if [[ -d $theme_dir ]]; then
    sig_dirs+=("$theme_dir")
    for theme_path in "$theme_dir"/*; do
      if [[ -d $theme_path || -L $theme_path ]]; then
        sig_dirs+=("$theme_path")
      fi
    done
  fi
done

fast_signature="v2"$'\n'
if (( ${#sig_dirs[@]} > 0 )); then
  fast_signature+="$(stat -Lc '%n:%Y' "${sig_dirs[@]}" 2>/dev/null)"$'\n'
fi

if [[ ! -f $fast_signature_file ]] || ! cmp -s "$fast_signature_file" <(printf '%s' "$fast_signature"); then
  rm -rf "$preview_dir"
  mkdir -p "$preview_dir"

  # A user's copy of a theme supplies the preview ahead of the built-in one.
  while read -r theme_name; do
    preview=$(find_preview "$USER_THEMES_PATH/$theme_name")

    if [[ -z $preview ]]; then
      preview=$(find_preview "$OMARCHY_THEMES_PATH/$theme_name")
    fi

    add_theme_preview "$theme_name" "$preview"
  done < <(omarchy-theme-list --folders)

  printf '%s' "$fast_signature" >"$fast_signature_file"
fi

current_theme=$(cat "$HOME/.local/state/omarchy/current/theme.name" 2>/dev/null)
selected_preview=""
for extension in png jpg jpeg webp gif bmp mp4 m4v mov webm mkv avi; do
  if [[ -e $preview_dir/$current_theme.$extension ]]; then
    selected_preview="$preview_dir/$current_theme.$extension"
    break
  fi
done
menu_args=(
  --print-name
  --show-labels
  --filterable
  --lazy-thumbnails
  --selected "$selected_preview"
)

if [[ $print_rows == true ]]; then
  menu_args+=(--print-rows)
fi

exec omarchy-menu-images "${menu_args[@]}" "$preview_dir"
