##
#   My Bash Configs
##

# Set EDITOR to nvim
export EDITOR=nvim


# Check if the current shell is an SSH session
is_ssh_session() {
  if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
    return 0
  else
    return 1
  fi
}

#TODO: Change ps1 to project root in nix shells
# PS1 Config
function set_ps1_prompt() {
  if [ -n "${IN_NIX_SHELL:+x}" ]; then
    PS1="\[\033[01;34m\]\w\[\033[00m\]\n\[\033[01;32m\]nixShell > \[\033[00m\]"
  else
    if ! is_ssh_session; then
      PS1="\n\[\033[01;34m\]\w\[\033[00m\]\n\[\033[01;32m\]> \[\033[00m\]"
    else
      PS1="\n\[\033[01;34m\]\w\[\033[00m\]\n\[\033[01;32m\]\u@\h:\[\033[00m\] "
    fi
  fi
}
PROMPT_COMMAND="set_ps1_prompt; $PROMPT_COMMAND"

# Locate and source the bash-completion scripts
bash_completion_dir="$HOME/.nix-profiles/share/bash-completion/completions"
if [ -d "$bash_completion_dir" ]; then
  for file in "$bash_completion_dir"/*; do
    source "$file" 2>/dev/null
  done
fi


# Alias List
  alias ls='lsd'


##
#     Custom Functions
##

# penpot {run|stop|update|help} alias function
function penpot() {
  case "$1" in
    run)
      sudo docker compose -p penpot -f ~/Documents/tools/penpot/docker-compose.yaml up -d >/dev/null 2>&1
      nohup bash -c '(sleep 10 && if [[ "$OSTYPE" == "linux-gnu"* ]]; then
                        xdg-open "http://localhost:9001"
                      elif [[ "$OSTYPE" == "darwin"* ]]; then
                        open "http://localhost:9001"
                      fi)' >/dev/null 2>&1 &
      echo "Started penpot on http://localhost:9001"
      ;;
    stop)
      echo "Stopping penpot"
      sudo docker compose -p penpot -f ~/Documents/tools/penpot/docker-compose.yaml down >/dev/null 2>&1
      ;;
    update)
      sudo docker compose -f ~/Documents/tools/penpot/docker-compose.yaml pull
      echo "Updated penpot!"
      ;;
    help)
      xdg-open "https://help.penpot.app/"
      echo "Opened penpot help page in your browser."
      ;;
    *)
      echo "Usage: penpot {run|stop|update|help}"
      ;;
  esac
}

# Developer Environment Function
function dev() {
  echo "Select an environment:"
  echo "1. Web"
  echo "2. Elixir"
  echo "3. Haskell"
  # Add more options here...

  read -p "Enter the number of your choice: " choice

  case $choice in
    1)
      (cd ~/Documents/developerEnvs/webDev && nix develop)
      ;;
    2)
      (cd ~/Documents/developerEnvs/elixirDev && nix develop)
      ;;
    3)
      (cd ~/Documents/developerEnvs/haskellDev && nix develop)
      ;;
    # Add more cases here...
    *)
      echo "Invalid choice"
      ;;
  esac
}
