# shellcheck shell=sh disable=SC3043,SC2120

___x_cmd_ui_region(){
    local subcmd="$1";  [ "$#" -gt 0 ] && shift
    case "$subcmd" in
        autorefresh)                         ___x_cmd_ui_region_autorefresh "$@"                    ;;
        keyboard)                            ___x_cmd_ui_region_update_with_keyboard "$@"           ;;
        autorefresh_with_keyboard)           ___x_cmd_ui_region_autorefresh_with_keyboard "$@"      ;;
        autorefresh_with_keyboard_vc)        ___x_cmd_ui_region_autorefresh_with_keyboard_vc "$@"   ;;
        send_update)
            ;;
        send_env)
            ;;
    esac
}

# Section: region update
___x_cmd_ui_region_update(){
    local AWK_IS_INTERACTIVE=1
    # UI_CURSOR_SAVE = "\0337"
    # UI_CURSOR_RESTORE = "\0338"

    # restore cursor then clear lines ...
    trap '
    printf "\033[34h\033[?25h" >&2;
    return 130
    ' INT
    LC_ALL="$___X_CMD_AWK_LANGUAGE" \
    ___x_cmd_awk___inner \
        -f "$___X_CMD_ROOT_MOD/awk/lib/core.awk" \
        -f "$___X_CMD_ROOT_MOD/awk/lib/ui/ui.awk" \
        -f "$___X_CMD_ROOT_MOD/ui/lib/awk/region.awk"
}

# EndSection

# Section: send
___x_cmd_ui_region_send(){
    ___x_cmd_ui_region_send_update "$@"
}

___X_CMD_UI_REGION_END_LINE="${___X_CMD_UNSEENCHAR_003}${___X_CMD_UNSEENCHAR_002}${___X_CMD_UNSEENCHAR_005}"

___x_cmd_ui_region_send_env(){
    while [ $# -gt 0 ]; do
        ___x_cmd_ui_region_send_env_kv "$1" "$(eval printf \"\$"$1"\")"
        shift
    done
}

___x_cmd_ui_region_send_env_kv(){
    printf "ENV %s\n" "$1"
    printf "%s\n%s\n" "$2" "$___X_CMD_UI_REGION_END_LINE"
    shift
}

___x_cmd_ui_region_send_update(){
    printf "UPDATE %s %s\n" "${COLUMNS}" "${LINES}"
    if [ "$#" -eq 0 ]; then
        ___x_cmd_cmds_cat
    else
        printf "%s" "$*"
    fi
    printf "\n%s\n" "$___X_CMD_UI_REGION_END_LINE"
}

___x_cmd_ui_region_send_screeninfo(){
    printf "SIZE %s %s\n" "${COLUMNS}" "${LINES}"
}
# TODO: sendenv

# EndSection

# Section: run
___x_cmd_ui_region___run_clear(){
    # This is useless
    [ -z "$START" ] && return

    if [ "${___X_CMD_UI_CLEAR_ONEXIT:-yes}" = yes ]; then
        {
            printf "\0338\033[J\033[2K"
            printf "\033[34h\033[?25h\033[?7h"
        } >&2
    fi
    ___x_cmd_cmds stty echo 2>/dev/null
    START=
}

___x_cmd_ui_region___run_inner(){
    ___x_cmd_cmds stty -echo 2>/dev/null
    trap ___x_cmd_ui_region___run_clear INT

    local START=1
    "$@" | ___x_cmd_ui_region_update

    ___x_cmd_ui_region___run_clear
}

# ___x_cmd_ui_region_run(){
#     eval "$(___x_cmd_ui_region___run_inner "$@")"
# }

# ___x_cmd_ui_region_run_(){
#     if [ -z "$___X_CMD_ANTI_ZSH" ] && [ "$___X_CMD_SHELL" = zsh ]; then
#         ___X_CMD_ROOT_CODE="$___X_CMD_ROOT_CODE" \
#         ___X_CMD_ANTI_ZSH=1 \
#         /bin/zsh --no-rcs "$___X_CMD_ROOT_MOD/ui/lib/region.sh" "$@"
#     #     /bin/sh "$___X_CMD_ROOT_MOD/pick/lib/main.sh" "$@"
#     else
#         # ___x_cmd_ui_region___run_inner "$@"
#         :
#     fi
# }

___x_cmd_ui_region_run_(){
    ___x_cmd antizshbin ___x_cmd_ui_region___run_inner "$@"
}

___x_cmd_ui_region_run(){
    ___x_cmd_compat_syncfs "$___X_CMD_DEVTTY_FP"
    local x_=; ___x_cmd storerat_ ___x_cmd_ui_region_run_ "$@" || return $?
    eval "$x_"
}

# ___x_cmd_ui___datastore_(){
#     [ "$___X_CMD_SHELL" = zsh ] || {
#         x_="$("$@")"
#         return
#     }

#     local c=0
#     local fp="$___X_CMD_ROOT_TMP/datastore/ui/$$"
#     x ensurefp "$fp"
#     "$@" >"$fp"
#     c="$?"
#     read -r x_ <"$fp"
#     ___x_cmd_cmds rm -f "$fp"
#     return "$c"
# }


# EndSection

# Section: autorefresh_with_keyboard
___x_cmd_ui_region___autorefresh_with_keyboard_mainloop(){
    local fun="${1:?Provide handler}"; shift
    local on=1
    {
        trap '___x_cmd_ui_region___run_clear; exit 0' USR1
        ___x_cmd pidofsubshell
        (
            trap 'on=' USR1
            ___x_cmd pidofsubshell
            local ticks=1
            while [ -n "$on" ]; do
                ticks="$((ticks+1))"
                ___x_cmd tty update
                printf "R:%s:%s:%s\n" "$ticks" "$COLUMNS" "$LINES"
                sleep "${___X_CMD_UI_REFRESH_INTERVAL:-0.1}"
            done
        ) &

        if [ -n "$ZSH_VERSION" ]; then
            trap '___x_cmd_ui_region___run_clear; exit 130' INT  # FOR zsh trap
        else
            trap ':; exit 130' INT   # FOR ___x_cmd_ui_getchar. Resolve bug in specific bash version. Refer issue:#I4Z4WH for more details
        fi
        while ___x_cmd_ui_getchar && [ -n "$on" ]; do
            printf "C:%s:%s\n" "${___X_CMD_UI_GETCHAR_TYPE}" "${___X_CMD_UI_GETCHAR_CHAR}" || break
        done

        # Trap dd command in function ___x_cmd_ui_getchar for ash/dash. Issue:#I4Z4X1
        kill -s INT "$PPID" 2>/dev/null
        ___x_cmd_ui_region___run_clear
        printf "C:ascii-letter-lowercase:q\n" # for exit PID_OF_UPSUBSHELL_INNER
    } | {
        local PID_OF_UPSUBSHELL; local PID_OF_UPSUBSHELL_INNER
        read -r PID_OF_UPSUBSHELL && read -r PID_OF_UPSUBSHELL_INNER

        "$fun" "$@"

        kill -s USR1 "$PID_OF_UPSUBSHELL_INNER"
        kill -s USR1 "$PID_OF_UPSUBSHELL"
    }
}

___x_cmd_ui_region_autorefresh_with_keyboard(){
    < "$___X_CMD_DEVTTY_FP" ___x_cmd_ui_region_run ___x_cmd_ui_region___autorefresh_with_keyboard_mainloop "$@"
    if [ -z "$BASH_VERSION" ] && [ -z "$ZSH_VERSION" ]; then stty -raw; fi       # For kill ___x_cmd_ui_getchar in alpine
}
# EndSection

