# shellcheck disable=SC2329,SC2120

# x feishu claw ...

# x weixin claw ...

# x qywx claw ...

# x qywx claw --engine claude,codex,gemini,kimi --msg qywx

# x claw service start --name 0

# x claw connect --name 0 --im qywx
# x claw connect --name 0 --im weixin
# x claw connect --name 0 --im telegram

# x claw 是本体

# x claw service start --name 0
# x qywx bot connect

___x_cmd_claw_service(){
    [ $# -gt 0 ]    ||      set -- --help

    local op="$1";          shift
    case "$op" in
        -h|--help)          ___x_cmd help -m claw service "$@" ; return 0 ;;
        start|stop|restart|install|uninstall|status|log|run)
                            ___x_cmd_claw_service_"$op" "$@"      ;;
        *)                  N=claw M="Unknown op -> $op" log:ret:64   ;;
    esac
}

___x_cmd_claw_service_start(){
    while [ "$#" -gt 0 ]; do
        case "$1" in
            -h|--help)
                        shift; ___x_cmd help -m claw start "$@" ; return 0 ;;
            *)          claw:error "Unsupported option -> $1"
                        return 1
                        ;;
        esac
    done

    if ___x_cmd service isalive x-claw-service; then
        claw:info "claw service is already running. Use 'x claw log' to view logs"
        return 0
    else
        log:sub:init -i "x-claw-service" claw "Starting claw service"
        ___x_cmd service start      x-claw-service  --  x-cmd claw service run || return $?

        ___x_cmd sleep 2

        if ___x_cmd service isalive x-claw-service; then
            claw:info "claw service is running. Use 'x claw log' to view logs."
        else
            claw:error "Failed to start claw service — check logs for details"
            ___x_cmd service log tail x-claw-service
            return 1
        fi
    fi

    local active_list=""
    active_list="$( ___x_cmd_claw_run___list_activate )"

    if [ -n "$active_list" ]; then
        claw:info "Activated IM platforms found. Use 'x claw status' to view details."
    else
        local x_=""
        if ___x_cmd_claw_run___detect_im_; then
            claw:info "No active IM chat detected. Recommended: x claw connect $x_"
        else
            claw:warn "No active IM chat and no ready IM platform. Use 'x claw connect <weixin|telegram|qywx|feishu...>' when ready."
        fi
    fi
    log:sub:fini
}

___x_cmd_claw_service_stop(){
    while [ "$#" -gt 0 ]; do
        case "$1" in
            -h|--help)
                        shift; ___x_cmd help -m claw stop "$@" ; return 0 ;;
            *)          claw:error "Unsupported option -> $1"
                        return 1
                        ;;
        esac
    done

    if ___x_cmd service isalive x-claw-service; then
        log:sub:init -i "x-claw-service" claw "Stopping claw service"
        ___x_cmd service stop       x-claw-service
    else
        log:sub:init -i "x-claw-service" claw "claw service is not running"
    fi

    ___x_cmd_claw_run___teardown
    log:sub:fini
}

___x_cmd_claw_service_restart(){
    while [ "$#" -gt 0 ]; do
        case "$1" in
            -h|--help)
                        shift; ___x_cmd help -m claw restart "$@" ; return 0 ;;
            *)          claw:error "Unsupported option -> $1"
                        return 1
                        ;;
        esac
    done

    local active_list=""
    active_list="$( ___x_cmd_claw_run___list_activate )"

    ___x_cmd_claw_service_stop  || return $?
    ___x_cmd_claw_service_start || return $?

    if [ -z "$active_list" ]; then
        claw:debug "No active IM chat to restore after restart."
        return 0
    fi

    log:sub:init -i "connect" claw "Restoring active IM connections after restart"

    local im="" chatid=""
    while read -r im; do
        chatid=""
        case "$im" in
            *=*) chatid="${im#*=}"; im="${im%%=*}" ;;
        esac
        [ -n "$im"  ] || continue
        ___x_cmd_claw_connect "$im${chatid:+":$chatid"}" || return $?
    done <<A
$active_list
A

    log:sub:fini
}

___x_cmd_claw_service_status(){
    if ___x_cmd service isalive x-claw-service; then
        claw:info "x-claw-service is running"
    else
        claw:warn "x-claw-service is not running"
    fi
    ___x_cmd_claw_run_status
}

___x_cmd_claw_service_log(){
    # ___x_cmd service log tail     x-claw-service
    ___x_cmd_claw_log "$@"
}

___x_cmd_claw_service_run(){
    ___x_cmd log +claw

    local x_=""; ___x_cmd epoch get_
    ___X_CMD_CLAW_BOT_RECENT_STDOUT_LOG_DIR="$___X_CMD_CLAW_BOT_STDOUT_LOG_DIR/$x_"
    ___x_cmd_claw_runstart
}

___x_cmd_claw_service_install(){
    if ! ___x_cmd service isalive x-claw-service; then
        ___x_cmd claw service start || return $?
    fi

    claw:info "Installing claw service"
    ___x_cmd service install x-claw-service
}

___x_cmd_claw_service_uninstall(){
    claw:info "Uninstalling claw service"
    ___x_cmd service uninstall x-claw-service
}

# Fetch msg from the msgloop
# ___x_cmd_claw_weixin_run(){
#     :
# }

# ___x_cmd_claw_feishu_run(){
#     local chatid="$1"

#     # MSG_CHATID
#     # MSG_JSON
#     # MSG_BODY
#     # MSG_RECEIPTION
#     ___x_cmd_claw_feishu_run_handler(){
#         ___x_cmd agent session handle --name "$MSG_CHATID" --msg "$MSG_BODY"
#     }

#     ___x_cmd_feishu_loop "$chatid" -- ___x_cmd_claw_feishu_run_handler
# }


# # x agent chat init --name "$chatid" --msg "从现在开始，请以简易方式进行回复。"
# # x feishu react -- -- x agent chat handle --name "$chatid" --msg "$( jq .msg.body <"$MSG")"
# # x weixin react --chatid "$chatid" --engine claude --spirit x-claw
# # x weixin react --chatid "$chatid" -- x agent chat handle --name x-claw "weixin-$chatid" "$chatid" "$msg"


# # Do we have a default conductor

# # ___x_cmd_feishu_loop --appid "$id" --appsecret "$secret" --worker

# #

# ___x_cmd_claw_qywx_run(){
#     local chatid="$1"

#     while true; do
#         ___x_cmd qywx bot subscribe "$chatid" | {
#             ___x_cmd agent session init --name "$chatid" "<Very big prompt>"
#             while read -r json_msg; do
#                 ___x_cmd agent session handle --name "$chatid" --msg "$json_msg" \
#                     || case $? in  130)     return 130 ;; esac
#                 # read file from json_response
#                 ___x_cmd qwyx bot send "$chatid" "$json_response"
#             done
#         }

#         claw:info 'Restart in 5 seconds.'
#         ___x_cmd sleep 5s || return $?
#     done
# }



# ___x_cmd_claw_telegram_run(){
#     :
# }

