#!/usr/bin/env sh

# fzf
if command -v fzf >/dev/null 2>&1; then

  PROJECTS="${PROJECTS:-${HOME%/}/projects}"
  export FZF_DEFAULT_OPTS="--height=40% --border \
  --padding=2 --layout=reverse --info=inline \
  --color bg:-1,bg+:#262626,border:#303030,fg:#b2b2b2,fg+:#dadada \
  --color gutter:#262626,header:#80a0ff,hl:#f09479,hl+:#f09479 \
  --color info:#cfcfb0,marker:#f09479,pointer:#ff5189,prompt:#87afff \
  --color spinner:#36c692 \
  --multi"

  # checking if the tmux session exists
  session_exists() {
    tmux has-session -t "${session_name}" 2>/dev/null
  }
  not_in_tmux() {
    [ -z "${TMUX}" ]
  }

  create_detached_session() {
    case "$#" in
      --current)
        TMUX=''
        tmux new-session -Ad -s "${session_name}" -c "${path_name}"
        tmux switch-client -t "${session_name}"
        tmux renamew "${session_name}"
        ;;
      *)
        TMUX=''
        tmux new-session -Ad -s "${session_name}" -c "${path_name}"
        tmux split-window -v -t "${session_name}" -c "${path_name}" -l '85%'
        tmux send-keys -t "${session_name}" "vim '+ZGFiles'" Enter
        tmux switch-client -t "${session_name}"
        tmux renamew "${session_name}"
        ;;
    esac
  }

  switch_session() {
    session=$(tmux list-sessions -F\#S \
      | fzf --height=100% --prompt='switch session: ' --select-1)
    tmux switch-client -t "${session}"
  }

  # creating a new session if needed
  new_session() {
    if not_in_tmux; then
      tmux new-session -As "${session_name}" -c "${path_name}"
    else
      if ! session_exists; then
        create_detached_session "${*:-}"
      else
        tmux switch-client -t "${session_name}"
      fi
    fi
  }

  # if --current was provided, we start a new session with the current directory
  case "$1" in
    --current)
      path_name=$(basename "${PWD}")
      session_name=${path_name}
      new_session
      ;;
    --session)
      switch_session
      ;;
    --start)
      path_name="$(basename "${PWD}" | tr . -)"
      session_name=$(echo "${path_name}" | sed -e 's/\./-/g' 's/ /\-/g')
      ;;
    *)
      cd "${PROJECTS}" || exit 1
      # session_name=$(find . -d "${PROJECTS}"/* \
      #   | basename -a "${PROJECTS}"/* \
      #   | fzf --height=100% --ansi --header="Select your project:")
      # session_name=$(find . "${PROJECTS}"/* -type d \
      #   | basename -a "${PROJECTS}"/* \
      #   | fzf --ansi --header="Select your project:")
      # session_name=$(cd "${PROJECTS}" && lr -d */ \
      session_name=$(cd "${PROJECTS}" && fd -d1 -td \
        | sed "s|/||g" \
        | fzf --reverse --header="Select project from $(basename "${PROJECTS}") >")
      path_name="${PROJECTS}/${session_name}"
      [ -n "${session_name}" ] && new_session "${*:-}"
      ;;
  esac

# gum
elif command -v gum >/dev/null 2>&1; then
  if ! tmux has-session 2>/dev/null; then
    echo "tmux is not running. Exiting."
    exit 0
  fi
  session=$(tmux list-sessions -F \#S \
    | gum filter --placeholder "Pick any session!")
  subcommand=attach
  if [ -z "${session}" ]; then
    echo "No session selected. Exiting."
    exit 0
  fi
  if [ "$#" -gt 0 ]; then
    subcommand="$1"
  elif test "${TMUX+x}"; then
    subcommand="switch"
  fi
  tmux "${subcommand}" -t "${session}"

else
  tmux has-session -t "$1" 2>/dev/null
  ret="$?"

  if [ "${ret}" != 0 ]; then
    tmux new-session -d -s "$1"
  fi

  tmux attach-session -d -t "$1"
fi
