
# render --mini
# render --std
# render --all
# render --llm  -> All of the information


___x_cmd_neo_fetch___render_auto(){
    # If the mode is AI. Then using the llm mode.
    ___x_cmd_neo_fetch___render_ui "$1"

}


___x_cmd_neo_fetch___render_llm(){
    local x_
    ___x_cmd_neo_fetch___render_llm_ || return $?
    printf "%s\n" "$x_"
}

___x_cmd_neo_fetch___render_llm_(){
    local ret_
    ret_=""
    while read -r widget; do
        case "$widget" in
            "")     continue    ;;

            title)
                ___x_cmd_neo_fetch___user_
                [ -z "$x_" ] || ret_="${ret_}user : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ___x_cmd_neo_fetch___host_
                [ -z "$x_" ] || ret_="${ret_}host : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            os)
                ___x_cmd_neo_fetch___os_
                [ -z "$x_" ] || ret_="${ret_}os : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            host)
                ___x_cmd_neo_fetch___host_
                [ -z "$x_" ] || ret_="${ret_}host : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            kernel)
                ___x_cmd_neo_fetch___kernel_
                [ -z "$x_" ] || ret_="${ret_}kernel : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            uptime)
                ___x_cmd_neo_fetch___uptime_
                [ -z "$x_" ] || ret_="${ret_}uptime : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            pkgs)
                ___x_cmd_neo_fetch___pkgs_
                [ -z "$x_" ] || ret_="${ret_}pkgs : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            shell)
                ___x_cmd_neo_fetch___shell_
                [ -z "$x_" ] || ret_="${ret_}shell : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            resolution)
                ___x_cmd_neo_fetch___resolution_
                [ -z "$x_" ] || ret_="${ret_}resolution : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            de)
                ___x_cmd_neo_fetch___de_
                [ -z "$x_" ] || ret_="${ret_}de : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            wm)
                ___x_cmd_neo_fetch___wm_
                [ -z "$x_" ] || ret_="${ret_}wm : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            wmtheme)
                ___x_cmd_neo_fetch___wm_theme_
                [ -z "$x_" ] || ret_="${ret_}wmtheme : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            theme)
                ___x_cmd_neo_fetch___theme_
                [ -z "$x_" ] || ret_="${ret_}theme : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            icons)
                ___x_cmd_neo_fetch___icons_
                [ -z "$x_" ] || ret_="${ret_}icons : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            terminal)
                ___x_cmd_neo_fetch___terminal_
                [ -z "$x_" ] || ret_="${ret_}terminal : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            terminalfont)
                ___x_cmd_neo_fetch___terminal_font_
                [ -z "$x_" ] || ret_="${ret_}terminalfont : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            cpu)
                ___x_cmd_neo_fetch___cpu_
                [ -z "$x_" ] || ret_="${ret_}cpu : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            gpu)
                ___x_cmd_neo_fetch___gpu_
                [ -z "$x_" ] || ret_="${ret_}gpu : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;

            memory)
                ___x_cmd_neo_fetch___memory_
                [ -z "$x_" ] || ret_="${ret_}memory : ${x_}${___X_CMD_UNSEENCHAR_NEWLINE}"
                ;;
        esac
    done <<A
$widgetlist
A
    x_="$ret_"
}


# UI render: logo on left, info on right
# Reference: pfetch's dynamic printing method

___x_cmd_neo_fetch___render_ui(){
    local indent="${1:-0}"
    local logowidth=0; local logoheight=0; local infono=0
    local logoline; local widget; local label; local val; local line

    # Get logo and calculate dimensions
    ___x_cmd ascii logo get_ "$logo_name"
    [ -n "$x_" ] || x_="$___X_CMD_NEO_LOGO_DEFAULT"


    # Build indent spaces
    local indent_spaces=""
    while [ "${#indent_spaces}" -lt "$indent" ]; do
        indent_spaces="${indent_spaces} "
    done

    # Calculate logo width and height
    # Print logo with indentation - add indent to each line except the last (reset color line)
    local logo_output=""
    local is_first=1
    while [ -n "$x_" ]; do
        logoline="${x_%%"${___X_CMD_UNSEENCHAR_NEWLINE}"**}"
        x_="${x_#"${logoline}"}"
        x_="${x_#"${___X_CMD_UNSEENCHAR_NEWLINE}"}"

        logoheight=$((logoheight + 1))
        [ "${#logoline}" -le "$logowidth" ] || logowidth="${#logoline}"

        if [ "$is_first" -eq 1 ]; then
            is_first=0
            logo_output="${indent_spaces}${logoline}"
        else
            logo_output="${logo_output}${___X_CMD_UNSEENCHAR_NEWLINE}${indent_spaces}${logoline}"
        fi
    done

    printf "%s" "$logo_output"

    # Add padding between logo and info
    logowidth=$((logowidth + indent))

    # Move cursor up to align with logo top
    [ "$logoheight" -le 1 ] || printf '\033[%dA' "$((logoheight - 1))"

    # Render info lines next to logo
    printf '\n'

    while read -r widget; do
        [ "$infono" -ge "$logoheight" ] && break
        [ -n "$widget" ] || continue

        # Move cursor right to info position
        printf '\033[%dC' "$logowidth"

        # Get widget label and value
        label="$widget"
        ___x_cmd_neo_fetch___widget_ "$widget"
        val="$x_"
        [ -n "$val" ] || { printf '\n'; infono=$((infono + 1)); continue; }

        # Print formatted: label in color, value in default
        printf '\033[1;3%dm%-10s\033[0m %s\n' "${___X_CMD_NEO_FETCH_COLOR:-7}" "$label" "$val"
        infono=$((infono + 1))
    done <<A
$widgetlist
A

    # Move cursor to bottom if info is shorter than logo
    [ "$infono" -ge "$logoheight" ] || printf '\033[%dB' "$((logoheight - infono))"
}

___x_cmd_neo_fetch___widget_(){
    case "$1" in
        title)
            ___x_cmd_neo_fetch___user_
            [ -z "$x_" ] || { x_="$x_"; return 0; }
            ;;
        os)          ___x_cmd_neo_fetch___os_          ;;
        host)        ___x_cmd_neo_fetch___host_        ;;
        kernel)      ___x_cmd_neo_fetch___kernel_      ;;
        uptime)      ___x_cmd_neo_fetch___uptime_      ;;
        pkgs)        ___x_cmd_neo_fetch___pkgs_        ;;
        shell)       ___x_cmd_neo_fetch___shell_       ;;
        resolution)  ___x_cmd_neo_fetch___resolution_  ;;
        de)          ___x_cmd_neo_fetch___de_          ;;
        wm)          ___x_cmd_neo_fetch___wm_          ;;
        wmtheme)     ___x_cmd_neo_fetch___wm_theme_    ;;
        theme)       ___x_cmd_neo_fetch___theme_       ;;
        icons)       ___x_cmd_neo_fetch___icons_       ;;
        terminal)    ___x_cmd_neo_fetch___terminal_    ;;
        terminalfont) ___x_cmd_neo_fetch___terminal_font_ ;;
        cpu)         ___x_cmd_neo_fetch___cpu_         ;;
        gpu)         ___x_cmd_neo_fetch___gpu_         ;;
        memory)      ___x_cmd_neo_fetch___memory_      ;;
    esac
}
