# Check for bash
[ -z "$BASH_VERSION" ] && return

__toolbox_containers() {
  podman ps --all --format '{{.Names}}'
}

__toolbox_images() {
  podman images --format '{{.Repository}}:{{.Tag}}'
}

__toolbox() {
  local MIN_VERSION=29
  local RAWHIDE_VERSION=31

  local verbose_commands="create enter help init-container list run"
  local commands="$verbose_commands rm rmi"

  declare -A options
  local options=([create]="--candidate-registry --container --image --release" \
                 [enter]="--container --release" \
                 [help]="$commands" \
                 [init-container]="--home --home-link --monitor-host --shell --uid --user" \
		 [list]="--containers --images" \
		 [rm]="--all --force" \
		 [rmi]="--all --force" \
		 [run]="--container --release")

  _init_completion -s || return

  if [ "${COMP_CWORD}" -eq 1 ]; then
    mapfile -t COMPREPLY < <(compgen -W "--assumeyes --help --verbose $commands" -- "$2")
    return 0
  fi

  case "$prev" in
    --verbose)
      mapfile -t COMPREPLY < <(compgen -W "$verbose_commands" -- "$2")
      return 0
      ;;
    --container)
      mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_containers)" -- "$2")
      return 0
      ;;
    --image)
      mapfile -t COMPREPLY < <(compgen -W "$(__toolbox_images)" -- "$2")
      return 0
      ;;
    --release)
      mapfile -t COMPREPLY < <(compgen -W "$(seq $MIN_VERSION $RAWHIDE_VERSION)" -- "$2")
      return 0
      ;;
  esac

  local command
  if [ "${COMP_WORDS[1]}" = --verbose ]; then
    command=${COMP_WORDS[2]}
  else
    command=${COMP_WORDS[1]}
  fi

  local extra_comps
  case "$command" in
    rm)
      extra_comps="$(__toolbox_containers)"
      ;;&
    rmi)
      extra_comps="$(__toolbox_images)"
      ;;&
    *)
      mapfile -t COMPREPLY < <(compgen -W "${options[$command]} $extra_comps" -- "$2")
      return 0;
      ;;
  esac
}

complete -F __toolbox toolbox
