# shellcheck shell=dash

___x_cmd_webtop_container_restart___all(){
    {
        local id
        local name
        while read -r id && read -r name; do
            webtop:info "Removing container [name=$name] [id=$id]"
            ___x_cmd docker -- restart "$id"
        done
    } <<A
$(___x_cmd_webtop_container_foreach running)
A

    [ -n "$id" ] || webtop:info "No contaniner to restart."
    ! ___x_cmd_runmode_allow_chatty || ___x_cmd webtop ps
}


___x_cmd_webtop_container_restart(){
    local x_=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -a|--all)   ___x_cmd_webtop_container_restart___all; return ;;
            --id)       x_="$2";    shift 2 ;;
            *)          break ;;
        esac
    done

    [ -n "$x_" ] || ___x_cmd_webtop_container_id_ "$@" || N=webtop M="Not found any container to restart" log:ret:1

    ___x_cmd docker -- restart "$x_"
    ! ___x_cmd_runmode_allow_chatty || ___x_cmd_webtop_container_start___ui "$x_"
}



___x_cmd_webtop_container_start___ui(){
    local id="$1"
    local name="$2"
    local port="$3"

    [ -n "$name" ] || {
        name="$(___x_cmd_webtop container name "$id")"
    }

    [ -n "$port" ] || {
        port="${name##*-}"
    }

    local choice
    ___x_cmd ui select choice "Next for container [name=$name] [id=$id]" \
        "x webtop open $port                        # Open the vnc in browser"  \
        "x open https://x-cmd.com/mod/webtop        # For more information"     \
        "return 0"

    case "$choice" in
        1)      ___x_cmd open "http://localhost:${port#*:}"     ;;
        2)      ___x_cmd open "https://x-cmd.com/mod/webtop"    ;;
        *)      return 0
    esac
}
