#!/usr/bin/env sh
#
# men - grand unification of manuals
# dependencies: fzf
#
# ---
# source: https://github.com/aslamosama/dotfiles

if command -v nvim >/dev/null 2>&1; then
  editor=nvim
else
  editor=vim
fi

if command -v gapropos >/dev/null 2>&1; then
  gnuapropos="gapropos"
else
  gnuapropos="apropos"
fi

man_pages() {
  ${gnuapropos} . \
    | grep -v -E '^.+ \(0\)' \
    | awk '{ print $2 "	" $1 }' \
    | sort \
    | fzf --bind='?:toggle-preview' \
      --height=100% \
      --pointer=" " \
      --ellipsis= \
      --border=block \
      --margin=5%,5%,5%,5% \
      --ansi \
      --info=inline-right \
      --reverse \
      --tiebreak=begin,index \
      --preview-window="70%,right,border-left" \
      --preview="echo {1} | sed -E \"s/^\((.+)\)/\1/\" | xargs -I{S} man {S} {2} 2>/dev/null | col -bx | bat -l man -p --color always" \
    | sed -E 's/^\((.+)\)/\1/' \
    | xargs -r man
}

tldr_pages() {
  selected=$(tldr --config "${HOME%/}"/.config/tlrc/config.toml --list \
    | fzf --bind='?:toggle-preview' \
      --preview "tldr {1} --color=always" \
      --preview-window=right,70%,border-left \
      --margin=5% \
      --height=100% \
      --info=inline-right)

  if [ -n "${selected}" ]; then
    temp_file=$(mktemp "${TMPDIR:-/tmp}"/tldr.XXXXXX.md)
    tldr "${selected}" --raw --color=always >"${temp_file}"
    "${editor}" -R "${temp_file}"
    rm -- "${temp_file}"
  fi
}

cht_pages() {
  languages=$(curl -s https://cht.sh/:languages | awk '{ print $1 }')
  topics=$(curl -s https://cht.sh/:list | awk '{ print $1 }')
  options=$(printf '%s\n%s' "${languages}" "${topics}")

  selected=$(printf '%s\n' "${options}" \
    | fzf --bind='?:toggle-preview' \
      --preview "curl -s cht.sh/{1} | head -100 | bat -p --color always" \
      --preview-window=right,70%,border-left \
      --margin=5% \
      --height=100% \
      --info=inline-right)

  if [ -n "${selected}" ]; then
    temp_file=$(mktemp "${TMPDIR:-/tmp}"/cht.XXXXXX)
    curl -s "cht.sh/${selected}" >"${temp_file}"
    # "${editor}" - -c "lua Snacks.terminal.colorize()" "${temp_file}"
    bat -l man "${temp_file}"
    rm -- "${temp_file}"
  fi
}

nvim_docs() {
  # nvim "+lua require('fzf-lua').helptags()"
  nvim "+Helptags"
}

main_menu() {
  p_options() {
    printf '%s\n' "  Man Pages" "  TLDR Pages" "  cht.sh" "  Nvim Docs"
  }
  # printf '%s\n' "  Man Pages" "  TLDR Pages" "  cht.sh"
  # choice=$(printf '%s\n' "${options[@]}" \
  #   | fzf --height=100% --no-info --border=block --margin=30%,35% --prompt="")

  choice=$(p_options \
    | fzf --height=100% --no-info --border=block --margin=30%,35% --prompt="")

  # shellcheck disable=SC2249
  case ${choice} in
    "  Man Pages") man_pages ;;
    "  TLDR Pages") tldr_pages ;;
    "  Nvim Docs") nvim_docs ;;
    "  cht.sh") cht_pages ;;
  esac
}

main_menu
