#!/usr/bin/env bash

# rofi_ocr_search.sh - Search and open OCR'd screenshot files using rofi
# This script displays OCR'd screenshot data in rofi and allows opening selected files

# Constants
DATABASE_PATH="${XDG_DATA_HOME:-$HOME/.local/share}/orc-gallery/screenshot_ocr.db"
SCREENSHOTS_DIR="$HOME/Screenshots"
ROFI_PROMPT="Screenshot OCR"
MAX_DISPLAY_LENGTH=100

# Check if required programs are installed
check_dependencies() {
  local missing=0

  if ! command -v rofi >/dev/null 2>&1; then
    echo "Error: rofi is not installed. Please install it with:"
    echo "  sudo pacman -S rofi"
    missing=1
  fi

  if ! command -v sqlite3 >/dev/null 2>&1; then
    echo "Error: sqlite3 is not installed. Please install it with:"
    echo "  sudo pacman -S sqlite"
    missing=1
  fi

  if ! command -v xdg-open >/dev/null 2>&1; then
    echo "Error: xdg-open is not installed. Please install it with:"
    echo "  sudo pacman -S xdg-utils"
    missing=1
  fi

  if [ ! -f "$DATABASE_PATH" ]; then
    echo "Error: Database not found at $DATABASE_PATH"
    echo "Run the OCR script first to create and populate the database."
    missing=1
  fi

  if [ $missing -eq 1 ]; then
    exit 1
  fi
}

# Format OCR text for display in rofi
format_ocr_text() {
  local text="$1"

  # Replace newlines with spaces
  text="${text//$'\n'/ }"

  # Remove multiple spaces
  text=$(echo "$text" | tr -s ' ')

  # Truncate if too long
  if [ ${#text} -gt $MAX_DISPLAY_LENGTH ]; then
    text="${text:0:$MAX_DISPLAY_LENGTH}..."
  fi

  echo "$text"
}

# Get entries from database and format for rofi
get_entries_for_rofi() {
  sqlite3 -separator '|' "$DATABASE_PATH" "
    SELECT filename, ocr_text, full_path
    FROM ocr_results
    ORDER BY filename
  " | while IFS='|' read -r filename ocr_text path; do
    formatted_text=$(format_ocr_text "$ocr_text")
    echo "$filename | $formatted_text"
    echo "$path" >> /tmp/ocr_paths.$$
  done
}

# Main function
main() {
  check_dependencies

  # Create temporary file for paths
  rm -f /tmp/ocr_paths.$$ 2>/dev/null
  touch /tmp/ocr_paths.$$

  # Get all entries and display in rofi
  selection=$(get_entries_for_rofi | rofi -dmenu -i -p "$ROFI_PROMPT" \
    -width 80 \
    -lines 15 \
    -font "mono 10" \
    -matching fuzzy)

  # Exit if no selection made
  if [ -z "$selection" ]; then
    rm -f /tmp/ocr_paths.$$ 2>/dev/null
    exit 0
  fi

  # Extract filename from selection
  filename=$(echo "$selection" | cut -d '|' -f1 | xargs)

  # Find the corresponding line number
  line_number=1
  sqlite3 -separator '|' "$DATABASE_PATH" "
    SELECT filename
    FROM ocr_results
    ORDER BY filename
  " | while read -r db_filename; do
    if [ "$db_filename" = "$filename" ]; then
      break
    fi
    line_number=$((line_number + 1))
  done

  # Get the full path from our temporary file
  file_path=$(sed -n "${line_number}p" /tmp/ocr_paths.$$)

  # Open the file if path exists
  if [ -n "$file_path" ] && [ -f "$file_path" ]; then
    xdg-open "$file_path" &
    echo "Opening: $file_path"
  else
    echo "Error: Could not find file path for $filename"
  fi

  # Clean up
  rm -f /tmp/ocr_paths.$$ 2>/dev/null
}

# Run the main function
main
