# shellcheck shell=dash

___x_cmd log init hotkey
xrc:mod:lib     hotkey   boot fzf co/_index

___x_cmd_hotkey___main(){
    [ "$#" -gt 0 ] ||   set -- --help

    local op="$1";      shift
    case "$op" in
        try|use|unuse|boot|co)
                        ___x_cmd_hotkey_"$op" "$@" ;;
        -h|--help)      ___x_cmd help -m hotkey ;    return ;;
        *)              ___x_cmd help -m hotkey >&2; return 64 ;;
    esac
}

___x_cmd_hotkey_try(){
    local X_help_cmd; X_help_cmd='___x_cmd help -m hotkey try' help:arg:parse
    ___x_cmd_hotkey_exec "$@"
}

___x_cmd_hotkey_use(){
    local X_help_cmd; X_help_cmd='___x_cmd help -m hotkey use' help:arg:parse
    ___x_cmd_hotkey_exec --use "$@"
}

___x_cmd_hotkey_unuse(){
    local X_help_cmd; X_help_cmd='___x_cmd help -m hotkey unuse' help:arg:parse
    while [ "$#" -gt 0 ]; do
        case "$1" in
            co|fzf-history|fzf-file|fzf-cd)
                hotkey:info "Unusing hotkey: $1"
                ___x_cmd_hotkey_boot unsetfile "$1"
                shift ;;
            *)  break ;;
        esac
    done
}

___x_cmd_hotkey_exec(){
    local loglevel="info"; local is_use=""
    local load_co_winget=""; local load_fzf_history=""; local load_fzf_file=""; local load_fzf_cd=""
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --loglevel)     loglevel="$2"; shift 2 ;;
            --use)          is_use=1 ;          shift ;;
            co)             load_co_winget=1;   shift ;;
            fzf-history)    load_fzf_history=1; shift ;;
            fzf-file)       load_fzf_file=1;    shift ;;
            fzf-cd)         load_fzf_cd=1;      shift ;;
            *)  break ;;
        esac
    done
    if [ "$load_co_winget" = 1 ]; then
        local k="Ctrl+X"
        case "$BASH_VERSION" in
            3.*) k="Ctrl+X Ctrl+X" ;;
        esac
        ___x_cmd log :hotkey "$loglevel" "Binding hotkey co widget -> $k"
        ___x_cmd_hotkey_co --bind || return $?
        [ -z "$is_use" ] || ___x_cmd_hotkey_boot setfile co
    fi

    if [ "$load_fzf_history" = 1 ]; then
        ___x_cmd log :hotkey "$loglevel" "Binding hotkey fzf-history widget -> Ctrl+R"
        ___x_cmd_hotkey_fzf --bind --ctrl-r  || return $?
        [ -z "$is_use" ] || ___x_cmd_hotkey_boot setfile fzf-history
    fi
    if [ "$load_fzf_file" = 1 ]; then
        ___x_cmd log :hotkey "$loglevel" "Binding hotkey fzf-file widget -> Ctrl+T"
        ___x_cmd_hotkey_fzf --bind --ctrl-t  || return $?
        [ -z "$is_use" ] || ___x_cmd_hotkey_boot setfile fzf-file
    fi
    if [ "$load_fzf_cd" = 1 ]; then
        ___x_cmd log :hotkey "$loglevel" "Binding hotkey fzf-cd widget -> Alt+C"
        ___x_cmd_hotkey_fzf --bind --alt-c   || return $?
        [ -z "$is_use" ] || ___x_cmd_hotkey_boot setfile fzf-cd
    fi
}

___x_cmd_hotkey___widget_supported(){
    local loglevel="error"
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --loglevel)     loglevel="$2"; shift 2 ;;
            *)              break ;;
        esac
    done

    local widget="$1"
    case "$widget" in
        co|fzf-history|fzf-file|fzf-cd)
            [ -n "${BASH_VERSION}${ZSH_VERSION}" ] && return 0
            ___x_cmd log :hotkey "$loglevel" "Widget '$widget' requires bash or zsh"
            return 1
            ;;
        *)
            ___x_cmd log :hotkey "$loglevel" "Unknown widget -> $widget"
            return 1
            ;;
    esac
}
