#! /bin/sh
# shellcheck shell=dash

# Author: Li Junhao (l@x-cmd.com)

# shellcheck disable=2034

___x_cmd log init neo

xrc:mod:lib     neo     fetch/render    \
                fetch/info  fetch/info_gpu


___X_CMD_NEO_FETCH_CACHE="$___X_CMD_ROOT_CACHE/neo/fetch"

___x_cmd_neo_fetch(){
    local mode=mini
    local logo=1
    local logo_name=""
    local color=1
    local info=""
    local indent=0

    local mode=auto

    ___x_cmd mkdirp "$___X_CMD_NEO_FETCH_CACHE"

    local widgetlist=""

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)                  ___x_cmd help -m neo fetch  ; return 0 ;;
            --mini|--std|--all)         ___x_cmd_neo_fetch___handle_widgetlist "${1#--}" ;;

            --no)                       return 0 ;;
            --fastfetch)                ___x_cmd fastfetch ; return 0 ;;

            --llm)                      mode=llm ;;
            --ui)                       mode=ui ;;

            --logo)                     logo="$2";  arg:2:shift; continue ;;
            --no-logo)                  logo=0                      ;;

            --color)                    color=1                     ;;
            --no-color)                 color=0                     ;;

            --title|\
            --os|--host|--kernel|\
            --uptime|--pkgs|--shell|\
            --resolution|--de|--wm|--wmtheme|--theme|--icons|\
            --terminal|--terminalfont|\
            --cpu|--gpu|--memory)       ___x_cmd_neo_fetch___handle_widgetlsit_add "${1#--}" ;;

            --info)                     info="$2"; arg:2:shift; continue ;;
            --indent)                   indent="$2"; arg:2:shift; continue ;;
            *)                          N=neo M="Unknown options -> $1" log:ret:64 ;;
        esac
        shift
    done

    [ -n "$widgetlist" ] ||     ___x_cmd_neo_fetch___handle_widgetlist std

    info="${info:-${___X_CMD_NEO_INFO:-}}"

    ___x_cmd_neo_fetch___render_"$mode" "$indent"
}


___x_cmd_neo_fetch___handle_widgetlsit_add(){
    while [ $# -gt 0 ]; do
        widgetlist="${widgetlist}${1}${___X_CMD_UNSEENCHAR_NEWLINE}"
        shift
    done
}

___x_cmd_neo_fetch___handle_widgetlist(){
    case "$1" in
        mini)
            widgetlist="$widgetlist
title
os
host
kernel
shell
cpu
gpu
"
            ;;
        std)
            widgetlist="$widgetlist
title
os
host
kernel
uptime
pkgs
shell
resolution
de
wm
terminal
cpu
gpu
memory
"
            ;;
        all)
            widgetlist="$widgetlist
title
os
host
kernel
uptime
pkgs
shell
resolution
de
wm
wmtheme
theme
icons
terminal
terminalfont
cpu
gpu
memory
"
            ;;
    esac
}
