# Copyright (C) 2011-2020 Aratelia Limited - Juan A. Rubio and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#
# Programmable bash_completion file for 'tizonia'.
#

_tizonia()
{
    local cur prev opts diropts # fileopts keywords
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    global="--help --buffer-seconds --cast --daemon --recurse --shuffle --version --proxy-server --proxy-user --proxy-password"
    omx="--comp-list --roles-of-comp --comps-of-role"
    server="--server --port --station-name --station-genre --no-icy-metadata --bitrate-modes --sampling-rates"
    client="--station-id"
    spotify="--spotify-user --spotify-password --spotify-owner --spotify-recover-lost-token --spotify-allow-explicit-tracks --spotify-preferred-bitrate --spotify-tracks --spotify-artist --spotify-album --spotify-playlist --spotify-track-id --spotify-artist-id --spotify-album-id --spotify-playlist-id --spotify-related-artists --spotify-featured-playlist --spotify-new-releases --spotify-recommendations-by-track-id --spotify-recommendations-by-artist-id --spotify-recommendations-by-track --spotify-recommendations-by-artist --spotify-recommendations-by-genre --spotify-user-liked-tracks --spotify-user-recent-tracks --spotify-user-top-tracks --spotify-user-top-artists --spotify-user-playlist"
    gmusic="--gmusic-user --gmusic-password --gmusic-device-id --gmusic-album --gmusic-artist --gmusic-library --gmusic-playlist --gmusic-podcast --gmusic-station --gmusic-tracks --gmusic-unlimited-station --gmusic-unlimited-album --gmusic-unlimited-artist --gmusic-unlimited-tracks --gmusic-unlimited-playlist --gmusic-unlimited-genre --gmusic-unlimited-activity --gmusic-unlimited-feeling-lucky-station --gmusic-unlimited-promoted-tracks"
    scloud="--soundcloud-oauth-token --soundcloud-user-stream --soundcloud-user-likes --soundcloud-user-playlist --soundcloud-creator --soundcloud-tracks --soundcloud-playlists --soundcloud-genres --soundcloud-tags"
    tunein="--tunein-search --tunein-type --tunein-local --tunein-music --tunein-talk --tunein-sports --tunein-location --tunein-podcasts --tunein-trending --tunein-keywords"
    youtube="--youtube-api-key --youtube-audio-stream --youtube-audio-playlist --youtube-audio-mix --youtube-audio-search --youtube-audio-mix-search --youtube-audio-channel-playlist --youtube-audio-channel-uploads"
    plex="--plex-server-base-url --plex-auth-token --plex-music-section --plex-audio-tracks --plex-audio-artist --plex-audio-album --plex-audio-playlist"
    iheart="--iheart-search --iheart-keywords"

    diropts="--recurse"
    opts="${global} ${omx} ${server} ${client} ${spotify} ${gmusic} ${scloud} ${tunein} ${youtube} ${plex} ${iheart}"

    helpkeyword="help"
    helpopts="global openmax server client spotify googlemusic soundcloud tunein youtube plex iheart keyboard config examples"

    roleskeyword="roles-of-comp"


    if [[ ${prev} =~ ${helpkeyword} ]]; then
        COMPREPLY=( $(compgen -W "${helpopts}" -- ${cur}) )
        return 0
    elif [[ ${prev} =~ ${diropts} ]]; then
        COMPREPLY=( $(compgen -A directory -- ${cur}) )
        return 0
    elif [[ ${prev} =~ ${roleskeyword} && ${cur} =~ OMX* ]]; then
        COMPREPLY=( $(tizonia -L 2>1 | grep -Po "${cur}.*(?=])") )
        return 0
    elif [[ ${prev} =~ ${roleskeyword} ]]; then
        COMPREPLY=( $(tizonia -L 2>1 | grep -Po 'OMX.*(?=])') )
        return 0
    elif [[ ${cur} == * ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}

complete -F _tizonia tizonia
