# shellcheck shell=sh disable=SC2154,SC2120,SC3043,SC2034

# Reference:      https://core.telegram.org/bots/api#sendmessage
___x_cmd_telegram_bot(){
    param:subcmd ___x_cmd_telegram_bot    \
        send   "send msg"
    param:subcmd:try
    param:run

    if [ "$#" -eq 0 ]; then
        ___x_cmd_telegram bot --help
    else
        ___x_cmd_telegram bot send "$@"
    fi
    return
}

___x_cmd_telegram_bot_send() {
    param:subcmd ___x_cmd_telegram_bot_send    \
        --text            "send text"            \
        --file            "send file"            \
        --poll            "send Poll"            \
        --contact         "send Contact"         \
        --dice            "send Dice"            \
        --venue           "send Venue"           \
        --location        "send Location"        \
        --raw             "send raw json"        \
        --stop_poll       "send stopPoll"        \
        --image           "send image"           \
        --video           "send Video"           \
        --video_note      "send VideoNote"       \
        --audio           "send Audio"           \
        --voice           "send Voice"           \
        --animation       "send Animation"       \

    param:subcmd:try
    param:run
        # --chat_action     "send ChatAction"

    if [ "$#" -eq 0 ]; then
        ___x_cmd_telegram bot send --help
    else
        ___x_cmd_telegram_bot_send_text "$@"
    fi
    return
}

___x_cmd_telegram_bot_send_text() {
    param:scope     telegram
    param:dsl <<A
option:
    #1          "Provide text"          <>=""
    --chat      "Provide chat id"       <>=""
    --token     "token"                 <>=""
    --pipe|-p   "pipe"
A
    param:run

    local text
    local msg_type="sendMessage"

    if [ -n "$pipe" ]; then
        text="$(___x_cmd_cmds_cat)"
    else
        text="$1"
    fi

    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "text": "$text"
}
A
}

___x_cmd_telegram_bot_send_raw() {
    param:scope             telegram
    param:dsl <<A
option:
    #1                  "Provide raw json"              <raw>:json
    --msg_type          "Provide message type"          <msg_type>
    --token             "token"                         <>=""
A
    param:run

    local json="${1}"; [ "$json" != "-" ] || json="$(___x_cmd_cmds_cat)"
    ___x_cmd_telegram_bot___send_json <<A
$json
A
}

___x_cmd_telegram_bot_send_image() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendPhoto"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "photo" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_file() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendDocument"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "document" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_video() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendVideo"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "video" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_video_note() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendVideoNote"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "video_note" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_audio() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendAudio"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "audio" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_voice() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendVoice"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "voice" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_animation() {
    param:scope     telegram
    param:dsl <<A
option:
    --file_path     "file path"         <file_path>:file_path
    --chat          "Provide chat id"   <>=""
    --token         "token"             <>=""
A
    param:run

    local msg_type="sendAnimation"
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_file "animation" "$msg_type" "$chat" "$file_path"
}

___x_cmd_telegram_bot_send_location() {
    param:scope     telegram
    param:dsl <<A
option:
    --latitude      "Latitude of the location"         <latitude>:number
    --longitude     "longitude of the location"        <longitude>:number
    --chat          "Provide chat id"                  <>=""
    --token         "token"                            <>=""
A
    param:run
    local msg_type=sendLocation
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "latitude" : "$latitude",
    "longitude" : "$longitude"
}
A
}

___x_cmd_telegram_bot_send_venue() {
    param:scope     telegram
    param:dsl <<A
option:
    --latitude      "Latitude of the location"         <latitude>:number
    --longitude     "longitude of the location"        <longitude>:number
    --title         "provide title"                    <title>:text
    --address       "provide address"                  <address>:text
    --chat          "Provide chat id"                  <>=""
    --token         "token"                            <>=""
A
    param:run
    local msg_type=sendVenue
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "title": "$title",
    "address": "$address",
    "latitude" : "$latitude",
    "longitude" : "$longitude"
}
A
}

___x_cmd_telegram_bot_send_contact() {
    param:scope     telegram
    param:dsl <<A
option:
    --phone_number   "Provide phone_number"        <phone_number>:phone_number
    --first_name     "Provide first_name"          <first_name>:text
    --chat           "Provide chat id"             <>=""
    --token          "token"                       <>=""
A
    param:run
    local msg_type=sendContact
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "phone_number": "$phone_number",
    "first_name" : "$first_name"
}
A
}

___x_cmd_telegram_bot_send_poll() {
    param:scope     telegram
    param:dsl <<A
option:
    --question       "Poll question, 1-300 characters"                   <question>:text
    --options        "A JSON-serialized list of answer options"          <options>:text
    --chat           "Provide chat id"                                   <>=""
    --token          "token"                                             <>=""
A
    param:run
    local msg_type=sendPoll
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    options=$(___x_cmd_telegram_bot___get_mobiles "${options}")
    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "question" : "$question",
    "options" : [ $options ]
}
A
}

___x_cmd_telegram_bot_send_stop_poll() {
    param:scope     telegram
    param:dsl <<A
option:
    --message_id     "Poll message_id"          <message_id>:message_id
    --chat           "Provide chat id"          <>=""
    --token          "token"                    <>=""

A
    param:run
    local msg_type=stopPoll
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat",
    "message_id" : "$message_id"
}
A
}

___x_cmd_telegram_bot_send_dice() {
    param:scope     telegram
    param:dsl <<A
option:
    --chat           "Provide chat id"          <>=""
    --token          "token"                    <>=""
A
    param:run
    local msg_type=sendDice
    local x_; ___x_cmd_telegram_bot___get_chat_ || return 1
    local chat="$x_"

    ___x_cmd_telegram_bot___send_json <<A
{
    "chat_id": "$chat"
}
A
}

# ___x_cmd_telegram_bot_send_chat_action() {
#     param:scope     telegram
#     param:dsl <<A
# option:
#     --chat_id        "Provide chat_id"                                   <>:
#     --action         "Type of action to broadcast,:[typing, upload_photo, upload_video, upload_voice, upload_document, choose_sticker, find_location,upload_video_note]"
#                                                                          <action>:text
#     --token       "token"           <>=""
# A
#     param:run
#     local msg_type=sendChatAction
#     ___x_cmd_telegram_bot___send_json <<A
# {
#     "chat_id": "$chat_id",
#     "action":"$action"
# }
# A
# }
