#!/usr/bin/env bash
readonly ID_PREVIEW="preview"

# Preview an image file directly
function draw() {
  declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                     [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                     [path]="${PWD}/$6") \
    > "$FIFO_UEBERZUG"
}

function font_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    fontpreview -i "$6" -o "/tmp${PWD}/$6.png"
  fi
  declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                     [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                     [path]="/tmp${PWD}/$6.png") \
    > "$FIFO_UEBERZUG"
}

function pdf_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    pdftoppm -png -singlefile "$6" "/tmp${PWD}/$6"
  fi
  declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                     [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                     [path]="/tmp${PWD}/$6.png") \
    > "$FIFO_UEBERZUG"
}

function audio_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    ffmpeg -i "$6" "/tmp${PWD}/$6.png" -y &> /dev/null
  fi
  declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                     [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                     [path]="/tmp${PWD}/$6.png") \
    > "$FIFO_UEBERZUG"
}

function video_preview() {
  # if no preview found, generate one
  if [ ! -f "/tmp${PWD}/$6.png" ]; then
    ffmpegthumbnailer -i "${PWD}/$6" -o "/tmp${PWD}/$6.png" -s 0 -q 10
  fi
  declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
                     [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
                     [path]="/tmp${PWD}/$6.png") \
    > "$FIFO_UEBERZUG"
}

function epub_preview() {
    if [ ! -f "/tmp$PWD/$6.png" ]; then
        epub-thumbnailer "$6" "/tmp$PWD/$6.png" 1024
    fi
    declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW"
        [x]="$2" [y]="$3" [width]="$4" [height]="$5" \
        [path]="/tmp$PWD/$6.png") \
        > "$FIFO_UEBERZUG"
}

# clear preview image
function clear_preview() {
  declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
    > "$FIFO_UEBERZUG"
}

# Check all the dependencies that are needed to show all the file previews.
# The reason why we are sending a notification incase a dependency does not
# exist is because that is the easiest way to get the user's attention.
dependencies=(ffmpeg ffmpegthumbnailer fontpreview pdftoppm epub-thumbnailer)
for dependency in "${dependencies[@]}"; do
    type -p "$dependency" &>/dev/null || {
        notify-send "vifm" "Could not find '${dependency}', is it installed?"
        exit 1
    }
done


# Make sure ueberzug's fifo exists
if [ -e "$FIFO_UEBERZUG" ]; then
  # Make a temp dir for rendering if not exists
  [ ! -d "/tmp${PWD}/" ] && mkdir -p "/tmp${PWD}/"
  case "$1" in
    draw) draw "$@" ;;
    font) font_preview "$@" ;;
    pdf) pdf_preview "$@" ;;
    video) video_preview "$@" ;;
    clear) clear_preview ;;
    audio) audio_preview "$@" ;;
    epub) epub_preview "$@" ;;
  esac
fi