#!/usr/bin/env bash

# if variable zellij is not set, then set it to /$HOME/.local/bin/zellij
if [ -z "$zellij" ]; then
  # zellij="$HOME/.local/bin/zellij"
  PATH="$HOME/.local/bin:$PATH"
fi

# adopted from https://zellij.dev/documentation/integration.html
ZJ_SESSIONS=$(zellij list-sessions)
# echo "$ZJ_SESSIONS"


attach=false

while (( "$#" )); do
  case "$1" in
    --attach)
      attach=true
      shift
      ;;
    *)
      shift
      ;;
  esac
done

if $attach; then
  echo "attached"
fi


NO_SESSIONS=$(echo "${ZJ_SESSIONS}" | wc -l)
# if ZJ_SESSIONS is empty, then set NO_SESSIONS to 0
if [ -z "${ZJ_SESSIONS}" ]; then
    NO_SESSIONS=0
fi

# check whether the string "(current)" is in ZJ_SESSIONS
if [[ "${ZJ_SESSIONS}" == *"(current)"* ]]; then
    # if so, then we are in a zellijsession
    echo "already inside a session, existing."
    COMMANDS=$(ls $HOME/.config/machineconfig/settings/zellij/commands)
    # fzf the results
    res="$(echo -e "${COMMANDS}" | fzf --ansi)"
    # run the bash fiZJ_SESSIONSle chosen
    bash $HOME/.config/machineconfig/settings/zellij/commands/$res

else # ==> we are not in a zellijsession
  if [ "${NO_SESSIONS}" -ge 1 ]; then  # sessions do exist

      # remove sessions that have 'EXITED' in them
      ZJ_SESSIONS=$(echo -e "${ZJ_SESSIONS}" | grep -v "EXITED")

      echo "zj_sessions:$ZJ_SESSIONS:end of zj_sessions"

      # if the result has only 1 line in it and $attach is raised, then attach to it and exit the if statement and the script
      NO_SESSIONS=$(echo "${ZJ_SESSIONS}" | wc -l)

      echo "NO_SESSIONS: $NO_SESSIONS"

      # if ZJ_SESSIONS is empty, then set NO_SESSIONS to 0
      if [ -z "${ZJ_SESSIONS}" ]; then
          NO_SESSIONS=0
          zellij --layout st2
          exit 0
      fi
      echo "NO_SESSIONS: $NO_SESSIONS"

      if [ "${NO_SESSIONS}" -eq 1 ] && $attach; then
        chosen_session=$(echo -e "${ZJ_SESSIONS}" | cut -d' ' -f1)
        # remove the ansi colors from chosen_session
        chosen_session=$(echo $chosen_session | sed 's/\x1b\[[0-9;]*m//g')
        # echo "attaching to $chosen_session exclusively."
        zellij attach "$chosen_session"
        exit 0
      fi

      # add my options to the list of sessions
      # if number of sessions is zero, then exclude ZJ_SESSIONS from the fzf options
      if [ "${NO_SESSIONS}" -eq 0 ]; then
          ZJ_SESSIONS="new_session\nkill_all_and_create_fresh_one\nexit_zellij"
        else
          ZJ_SESSIONS="$ZJ_SESSIONS\nnew_session\nkill_all_and_create_fresh_one\nexit_zellij"
      fi

      res="$(echo -e "${ZJ_SESSIONS}" | fzf --ansi)"
      # split `res` at the first space, and take the first element
      res=$(echo $res | cut -d' ' -f1)
      if [ "${res}" = "exit_zellij" ]; then
          echo "existing zellij."
      elif [ "${res}" = "new_session" ]; then
        zellij --layout st2  # can't specify name here. I can call it "main" only if it is the first, otherwise name conflict! also, can't use attach -c syntax because layout can't be specified.
      elif [ "${res}" = "kill_all_and_create_fresh_one" ]; then
        zellij ka -y
        zellij --layout st2
      else
        zellij attach $res # options --mirror-session false
      fi
  else  # no sessions, create one called main
     zellij --layout st2
  fi

fi
