# shellcheck shell=dash

# If the data already exists, then stop
___x_cmd_hn___story_data(){
    [ "$#" -gt 0 ] || N=hn M="Please privode store item id" log:ret:64
    ___x_cmd_hn___story_datainbulk "$@" || return $?
    ___x_cmd_hn___story_data_output "$@"
}

___x_cmd_hn___story_data_output(){
    local cur_datadir=""
    ___x_cmd_hn_cur    cur_datadir:=cur_datadir  2>/dev/null
    cur_datadir="${cur_datadir:-"$___X_CMD_HN_CACHE_STORE"}"
    local datafile=""; local x_
    local id; for id in "$@"; do
        datafile="$cur_datadir/${id}.json"; x_=""
        [ ! -f "$datafile" ] || ___x_cmd rat_ "$datafile"
        [ -n "$x_" ] || printf "%s\n" "{}"
        printf "%s\n" "$x_"
    done
}

___x_cmd_hn___story_needupdate(){
    local datafile="$1"
    [ -f "$datafile" ] || return 0

    local cur_story_timeout=""
    ___x_cmd_hn_cur    cur_story_timeout:=story_timeout  2>/dev/null

    if ___x_cmd fileage --in "${cur_story_timeout:-"$___X_CMD_HN_DEFAULT_STORY_TIMEOUT"}" "$datafile"; then
        return 1 # In ten minutes, skip the update
    fi

    # if not sure, then return 0
    local file_time;    file_time="$(___x_cmd fileage --stat "$datafile")"
    local news_time;    news_time="$(___x_cmd jo .time <"$datafile")"

    # if the data download time - date created time is less than 3 hours, then return 0
    # 3 * 60 * 60 = 3 hours
    [ $((file_time - news_time)) -ge 10800 ] || return 0
    return 1
}

___x_cmd_hn___story_datainbulk(){
    local cur_datadir=""
    ___x_cmd_hn_cur    cur_datadir:=cur_datadir  2>/dev/null
    cur_datadir="${cur_datadir:-"$___X_CMD_HN_CACHE_STORE"}"

    local arg=""; local datafile=""
    local id; for id in "$@"; do
        datafile="$cur_datadir/${id}.json"
        # [ ! -f "$datafile" ] || ! ___x_cmd fileage --in "$___X_CMD_HN_CACHE___TIMELIMIT" "$datafile" || continue
        ___x_cmd_hn___story_needupdate "$datafile" || continue
        arg="$arg $id"
    done
    [ -n "$arg" ] || return 0
    eval ___x_cmd_hn___story_json_with_pbar "$arg"
}

___x_cmd_hn___story_json_with_pbar(){
    [ "$#" -gt 0 ] || N=hn M="Please privode store item id" log:ret:64
    ___x_cmd_hn___story_json "$@" | ___x_cmd ourl pbar --size 4 --total "$#"
}
___x_cmd_hn___story_json(){
    [ "$#" -gt 0 ] || N=hn M="Please privode store item id" log:ret:64
    local arg=
    while [ "$#" -gt 0 ]; do
        if [ "$#" -le 500 ]; then
            ___x_cmd_hn___story_json_inner "$@"
            return
        else
            arg="$*"
            shift 500
            arg="${arg%"$*"}"
            eval ___x_cmd_hn___story_json_inner "$arg" || return $?
        fi
    done
}
___x_cmd_hn___story_json_inner(){
    ___x_cmd_hn___story_raw "$@" | ___x_cmd_hn___story_parse_resp2jsonfp "$#"
}

___x_cmd_hn___story_raw(){
    [ "$#" -gt 0 ] || N=hn M="Please privode store item id" log:ret:64
    local OLDIFS="$IFS"
    local IFS=","
    local idlist="{$*}"
    IFS="$OLDIFS"

    local cur_datadir=""
    ___x_cmd_hn_cur    cur_datadir:=cur_datadir  2>/dev/null
    cur_datadir="${cur_datadir:-"$___X_CMD_HN_CACHE_STORE"}"
    ___x_cmd mkdirp "$cur_datadir"

    set -- "https://hacker-news.firebaseio.com/v0/item/${idlist}.json?print=pretty" --fail-early --fail-with-body
    set -- "$@" -m 5 -L -sS -w "$___X_CMD_OURL_WRITEOUT" --output "${cur_datadir}/#1.tmp"
    hn:debug "x curl $* 2>&1"

    local proxy=""
    ___x_cmd_hn_cur    proxy:=     2>/dev/null
    ___x_cmd proxy runifset "$proxy"    ___x_cmd curl "$@" 2>&1
}

___x_cmd_hn___story_parse_resp2jsonfp(){
    local n=$1
    ourl:writeout:init
    while ourl:writeout:read; do
        printf "%s\n" "[http_code=${XOW_code}]" "[size=${XOW_size} B]" "[speed=${XOW_speed} B/s]" "[exitcode=${XOW_exitcode}]"
        n="$((n-1))"
        case "$XOW_code" in
            20*)    ___x_cmd mv -f "${XOW_fp}" "${XOW_fp%".tmp"}.json" ;;
            *)
                    hn:error "[Http_code=$XOW_code] Get data failed"
                    ___x_cmd_ourl_json2errorlog
                    ___x_cmd rmrf "$XOW_fp"
                    return "$XOW_exitcode"
        esac
    done
    [ "${n}" -le 0 ]
}

