# shellcheck shell=dash disable=SC2154,SC2034
___x_cmd_shodan_alert_ls(){
    local X_help_cmd='x help -m shodan alert ls';   help:arg:parse
    shodan:arg:format:all

    "___x_cmd_shodan_alert_ls___${format}"
}

___x_cmd_shodan_alert_ls___app(){
    shodan:info "Obtain a listing of all the network alerts that are currently active on the account"

    if  ! ___x_cmd_shodan_is_interactive_env; then
        ___x_cmd_shodan_alert_ls___curl
        return
    fi

    local data=; local alert_id=
    data="$(___x_cmd_shodan_alert_ls___csv | x csv app --width 30%,30%,30% --return print)"
    alert_id="${data%%,*}"

    local id=""; local alert_ip=
    x ui select id "What to do NEXT"     \
    "x shodan alert info $alert_id"      \
    "x shodan alert rm $alert_id"        \
    "x shodan alert edit $alert_id"      \
    "EXIT"     || return

    case "$id" in
        1)      x shodan alert info  "$alert_id" ;;
        2)      x shodan alert rm    "$alert_id" ;;
        3)
                alert_ip="$(x ted)"
                x shodan alert edit "$alert_id" "$alert_ip" ;;
        4)
            shodan:info "EXIT [code=0]"
            return 0 ;;
    esac
}

___x_cmd_shodan_alert_ls___csv(){
    local data
    data="$(___x_cmd_shodan_alert_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data ___x_cmd_shodan_alert_ls___parse_json2csv
}

___x_cmd_shodan_alert_ls___tsv(){
    local data
    data="$(___x_cmd_shodan_alert_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data ___x_cmd_shodan_alert_ls___parse_json2csv | x csv 2tsv
}

___x_cmd_shodan_alert_ls___json(){
    local data
    data="$(___x_cmd_shodan_alert_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data
}


___x_cmd_shodan_alert_ls___raw(){
    ___x_cmd_shodan_curl get "/shodan/alert/info"
}

___x_cmd_shodan_alert_ls___parse_json2csv(){
    x ja 2c .id .name .filters.ip |\
        x csv awk '{ ip_col = juq(cval(3)); gsub("\",\"", ",", ip_col); print cval(1)","cval(2)","ip_col }' |\
        x csv header --add ID Name  IP/Network
}


___x_cmd_shodan_alert_ll(){
    local X_help_cmd='x help -m shodan alert ll';   help:arg:parse
    local data
    data="$(___x_cmd_shodan_alert_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data ___x_cmd_shodan_alert_ls___parse_json2csv | x csv static_tab
}

