# shellcheck shell=bash disable=SC2154,SC2034

# x shodan notifier create -p jira --server <server> --username <username> --token <token> --project <project> --issue_type <type>
# x shodan notifier create phone <phone-number>
# x shodan notifier create slack ...

___x_cmd_shodan_notifier_create(){
    local X_help_cmd='x help -m shodan notifier create';   help:arg-null:parse

    local provider="$1";  shift
    case "$provider" in
        jira|phone|pagerduty|slack|telegram|webhook|gitter)
            ___x_cmd_shodan_notifier_create_"$provider"           "$@" ;;
        *)
            ___x_cmd_shodan_notifier_create_generic "$provider"   "$@" ;;
    esac
}

___x_cmd_shodan_notifier_create_generic(){
    local param=""
    while [ "$#" -gt 0 ]; do
        case "$1" in
            -p|--provider)      param="$param -d 'provider=$2'";           shift 2 ;;
            -d|--description)   param="$param -d 'description=$2'";        shift 2 ;;
            --*)                param="$param -d 'to=${2}'";               shift 2 ;;
            *)                  break ;;
        esac
    done

    if ! ___x_cmd_shodan_is_interactive_env; then
        ___x_cmd_shodan_curl post "/notifier" "$param"
        return
    fi

    ___x_cmd_shodan_curl post "/notifier" "$param" | x j2y
}

___x_cmd_shodan_notifier_create_curl(){
    local param="--data-urlencode 'provider=$provider'"
    [ -z "$description" ] || param="--data-urlencode 'description=$description'"
    while [ $# -gt 0 ]; do
        param="$param --data-urlencode '$1'"
        shift
    done
    shodan:info param="$param"

    if ! ___x_cmd_shodan_is_interactive_env; then
        ___x_cmd_shodan_curl post "/notifier" "$param"
        return
    fi

    ___x_cmd_shodan_curl post "/notifier" "$param" | x j2y
}

# shellcheck disable=SC2142,SC2154
alias @___x_cmd_shodan_notifier_create__alias='
    local description
    case "$1" in
        -d|--description)   description="$2";   shift 2 ;;
        -h|--help)          x help -m shodan notifier "$provider" ;;
    esac
'

___x_cmd_shodan_notifier_create_jira(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl server="$1" username="$2" token="$3" project="$4" issue_type="$5"
}

___x_cmd_shodan_notifier_create_phone(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl to="$1"
}


___x_cmd_shodan_notifier_create_pagerduty(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "routing_key=$1"
}

___x_cmd_shodan_notifier_create_slack(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "webhook_url=$1"
}

___x_cmd_shodan_notifier_create_telegram(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "chat_id=$1" "token=$2"
}

___x_cmd_shodan_notifier_create_webhook(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "url=$1"
}

___x_cmd_shodan_notifier_create_email(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "to=$1"
}

___x_cmd_shodan_notifier_create_gitter(){
    @___x_cmd_shodan_notifier_create__alias
    ___x_cmd_shodan_notifier_create_curl "room_id=$1" "token=$2"
}
