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

# Section(inner): endpoint, send_json

# Reference:      https://api.slack.com/reference/block-kit/blocks

___x_cmd_slack_bot___send_json() {
    local req_json ; req_json="$(___x_cmd_cmds_cat)"
    slack:debug "$(printf "%s" "$req_json" | x jo fmt)"
    local webhook ; webhook="$(___x_cmd_slack_webhook)" || return 1

    local res
    if ! res="$(___x_cmd_http post json "$webhook?wait=true" "$req_json")" ; then
        slack:error "Failed to send msg, res: $res $(___x_cmd_http resp body)"
    fi
    printf "%s" "$res"
}

___x_cmd_slack_bot(){
    param:subcmd ___x_cmd_slack_bot    \
        send   "send msg"              

    param:subcmd:try
    param:run
    slack:error "Subcommand required"
    ___x_cmd_slack help
}

___x_cmd_slack_bot_send() {
    param:subcmd ___x_cmd_slack_bot_send    \
        text             "send text"        \
        image            "send image"       \
        video            "send video"       \
        action           "send action"      \
        raw              "send raw json"    \
        context          "send context"     \
        section          "send section"     \
        imagetext        "send imagetext"      

    param:subcmd:try
    param:run

    ___x_cmd_slack_bot_send_text "$@"
}

___x_cmd_slack_bot_send_text() {
    param:scope            slack
    param:dsl <<A
option:
    #1      "Provide text"      <content>:text
A
    param:run
    local text="$1"
    ___x_cmd_slack_bot___send_json <<A
{
    "text": "$text"
}
A
}

___x_cmd_slack_bot_send_raw() {
    param:scope             slack
    param:dsl <<A
option:
    #1      "Provide raw json(If #1 is '-', the json is read from stdin)"      <raw>:json
A
    param:run

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


___x_cmd_slack_bot_send_section() {
    param:scope            slack
    param:dsl <<A
option:
    --section|m          "Provide multiple action section in following format [ <type> <text>]"                  
        <type>:text
        <text>:text
A
    param:run
    local IFS="
"
     local all; local type ; local text ;
    for i in $(param_marg section); do
        type=$(param_marg_get section "$i" 1)
        text=$(param_marg_get section "$i" 2)
        all="$all$(x jo dict type=section text="$( x jo dict type text)")"
    done
    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [
        $(printf "%s" "$all" | x jo)
	]
}
A
}

___x_cmd_slack_bot_send_action() {
    param:scope            slack
    param:dsl <<A
option:
    --section|m            "Provide multiple action section in following format [ <type> <text> ]"                  
        <type>:text
        <text>:text
    --accessory|m          "Provide multiple action type in following format [ <btn_text> <url> ]"                  
        <btn_text>:text
        <url>:url
A
    param:run
    local IFS="
"
    local all; local type ; local text ; local btn_text ; local url
    for i in $(param_marg section); do
        for j in $(param_marg accessory); do
            type=$(param_marg_get section "$i" 1)
            text=$(param_marg_get section "$i" 2)
            btn_text=$(param_marg_get accessory "$j" 1)
            url=$(param_marg_get accessory "$j" 2)       
            all="$all$(x jo dict type=section text="$(x jo dict type text)" accessory="$(x jo dict type=button text="$(x jo dict type=plain_text text="$btn_text")" url)")" 
        done
    done

    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [
		${all#,}
    ]
}
A
}


___x_cmd_slack_bot_send_imagetext() {
    param:scope            slack
    param:dsl <<A
option:
    --text              "Provide text"                                   <content>:text
    --type              "Block msg type [ <mrkdwn> <plain_text> ]"       <type>:text
    --image_url         "The URL of the image to be displayed"           <image_url>:url
    --alt_text          "A plain-text summary of the image"              <alt_text>:text
A
    param:run

    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [
		{
			"type": "section",
			"text": {
				"type": "$type",
				"text": "$text"
			},
			"accessory": {
				"type": "image",
				"image_url": "$image_url",
				"alt_text": "$alt_text"
			}
		}
	]
}
A
}

___x_cmd_slack_bot_send_image() {
    param:scope            slack
    param:dsl <<A
option:
    --text              "Provide text"                                   <text>:text
    --image_url         "The URL of the image to be displayed"           <image_url>:url
    --alt_text          "A plain-text summary of the image"              <alt_text>:text
A
    param:run

    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [
		{
			"type": "image",
			"title": {
				"type": "plain_text",
				"text": "$text"
			},
			"image_url": "$image_url",
			"alt_text": "$alt_text"
		}
	]
}
A
}

___x_cmd_slack_bot_send_context() {
    param:scope            slack
    param:dsl <<A
option:
    --text|m           "Provide multiple action section in following format [ <type> <text> ]"
        <type>:text
        <text>:text
    --image|m          "Provide multiple action card in following format [ <image_url> <alt_text> ]"
        <image_url>:url
        <alt_text>:text

A
    param:run

    local IFS="
"
    local all; local type ; local text
    for i in $(param_marg text); do
        type=$(param_marg_get text "$i" 1)
        text=$(param_marg_get text "$i" 2)
        all="$all$(x jo dict "type:$type" "text:$text" )"
    done

    local image_url; local alt_text
    for i in $(param_marg image); do
        image_url=$(param_marg_get image "$i" 1)
        alt_text=$(param_marg_get image "$i" 2)
        all="$all$(x jo dict "type:image" "image_url:$image_url" "alt_text:$alt_text")"
    done
    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [
		{
			"type": "context",
			"elements": [
				  $(printf "%s" "$all" | x jo)
			]
		}
	]
}
A
}

___x_cmd_slack_bot_send_video() {
    param:scope            slack
    param:dsl <<A
option:
    --tit_text              "Video title in plain text format"                                                           <tit_text>:text
    --alt_text              "A tooltip for the video. Required for accessibility"                                        <alt_text>:text
    --desc_text             "Description for video in plain text format."                                                <content>:text
    --title_url             "Hyperlink for the title text. Must correspond to the non-embeddable URL for the video."     <title_url>:url
    --video_url             "The URL to be embedded"                                                                     <video_url>:url
    --author_name           "Author name to be displayed. Must be less than 50 characters"                               <author_name>:author_name=""
    --thumbnail_url         "The thumbnail image URL"                                                                    <thumbnail_url>:url
    --provider_name         "The originating application or domain of the video ex."                                     <provider_name>:text=""
    --provider_icon_url     "Icon for the video provider ex. "                                                           <provider_icon_url>:url=""
A
    param:run

    ___x_cmd_slack_bot___send_json <<A
{
	"blocks": [      
		{
			"type": "video",
			"title": {
				"type": "plain_text",
				"text": "$tit_text"
			},
			"title_url": "$title_url",
			"description": {
				"type": "plain_text",
				"text": "$desc_text"
			},
			"video_url": "$video_url",
			"alt_text": "$alt_text",
			"thumbnail_url": "$thumbnail_url",
			"author_name": "$author_name",
			"provider_name": "$provider_name",
			"provider_icon_url": "$provider_icon_url"
		}
	]
}
A
}
