#autoload

# Usage: _deb_packages expl... avail|installed|uninstalled

_deb_packages_update_avail () {
  if ( [[ ${+_deb_packages_cache_avail} -eq 0 ]] ||
      _cache_invalid DEBS_avail ) && ! _retrieve_cache DEBS_avail;
  then
    _deb_packages_cache_avail=(
      ${(f)"$(apt-cache --generate pkgnames 2>/dev/null)"}
    )

    _store_cache DEBS_avail _deb_packages_cache_avail
  fi
  cachevar=_deb_packages_cache_avail
}

_deb_packages_update_installed () {
  if ( [[ ${+_deb_packages_cache_installed} -eq 0 ]] ||
      _cache_invalid DEBS_installed ) && ! _retrieve_cache DEBS_installed;
  then
    _deb_packages_cache_installed=()
    dpkg --get-selections | while read package state ; do
        [[ $state = (install|hold) ]] && _deb_packages_cache_installed+=$package
    done
    _store_cache DEBS_installed _deb_packages_cache_installed
  fi
  cachevar=_deb_packages_cache_installed
}

_deb_packages_update_held () {
  if ( [[ ${+_deb_packages_cache_held} -eq 0 ]] ||
      _cache_invalid DEBS_held ) && ! _retrieve_cache DEBS_held;
  then
    _deb_packages_cache_held=()
    dpkg --get-selections | while read package state ; do
        [[ $state = hold ]] && _deb_packages_cache_held+=$package
    done
    _store_cache DEBS_held _deb_packages_cache_held
  fi
  cachevar=_deb_packages_cache_held
}

_deb_packages_update_deinstalled () {
  if ( [[ ${+_deb_packages_cache_deinstalled} -eq 0 ]] ||
      _cache_invalid DEBS_deinstalled ) && ! _retrieve_cache DEBS_deinstalled;
  then
    _deb_packages_cache_deinstalled=()
    dpkg --get-selections | while read package state ; do
        [[ $state = deinstall ]] && _deb_packages_cache_deinstalled+=$package
    done
    _store_cache DEBS_deinstalled _deb_packages_cache_deinstalled
  fi
  cachevar=_deb_packages_cache_deinstalled
}

_deb_packages_update_xinstalled () {
  if ( [[ ${+_deb_packages_cache_xinstalled} -eq 0 ]] ||
      _cache_invalid DEBS_xinstalled ) && ! _retrieve_cache DEBS_xinstalled;
  then
    _deb_packages_cache_xinstalled=()
    dpkg --get-selections | while read package state ; do
        _deb_packages_cache_xinstalled+=$package
    done
    _store_cache DEBS_xinstalled _deb_packages_cache_xinstalled
  fi
  cachevar=_deb_packages_cache_xinstalled
}

_deb_packages_update_uninstalled () {
  _deb_packages_update_avail
  _deb_packages_update_installed
  if (( ! $+_deb_packages_cache_uninstalled )); then
    # Package lists too large to efficiently diff with zsh expansion
    _deb_packages_cache_uninstalled=(
      $( print -l $_deb_packages_cache_avail |
         fgrep -xvf =(print -l $_deb_packages_cache_installed) )
    )
  fi
  cachevar=_deb_packages_cache_uninstalled
}

_deb_packages () {
  local command="$argv[$#]" expl cachevar pkgset update_policy

  zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
  if [[ -z "$update_policy" ]]; then
    zstyle ":completion:*:*:$service:*" cache-policy _debs_caching_policy
  fi

  [[ "$command" = (installed|deinstalled|xinstalled|held|uninstalled|avail|available) ]] || {
    _message "unknown command: $command"
    return
  }

  zstyle -s ":completion:${curcontext}:" packageset pkgset

  [[ "$pkgset" = (installed|deinstalled|xinstalled|held|uninstalled|avail|available) ]] || {
    pkgset="$command"
  }

  [[ "$pkgset" = "available" ]] && pkgset="avail"

  expl=("${(@)argv[1,-2]}")

  _deb_packages_update_$pkgset

  typeset -gH $cachevar

  _tags packages && compadd "$expl[@]" - "${(@P)cachevar}"
}

_debs_caching_policy () {
  # rebuild if cache is more than a week old
  local -a oldp
  oldp=( "$1"(mw+1) )
  (( $#oldp )) && return 0

  [[ /var/cache/apt/pkgcache.bin -nt "$1" ||
     /var/lib/dpkg/available -nt "$1" ]]
}

_deb_packages "$@"
