display_line()
{
  local width=26 label_with_dots formatted_label

  # On calcule combien de points ajouter (Largeur - longueur du label)
  # On utilise une astuce simple : on ajoute 27 points au label et on coupe à 27.
  label_with_dots="${1}............................"
  formatted_label="${label_with_dots:0:$width}"

  # 4. Affichage
  printf "${COLOR_YELLOW}%s: ${COLOR_WHITE}%s${NONE}\n" "$formatted_label" "$2"
}

display_conditional_list()
{
    clear

    # On boucle sur chaque section du JSON
    echo "$1" | jq -c '.[]' | while read -r section; do

        local label cmd bin title items has_printed_title=false

        title=$(echo "$section" | jq -r '.section')
        items=$(echo "$section" | jq -c '.items[]')

        # On boucle sur les items de la section
        while read -r item; do

            label=$(echo "$item" | jq -r '.label')
            cmd=$(echo "$item" | jq -r '.cmd')
            bin=$(echo "$item" | jq -r '.bin // empty')

            # Vérification de la condition (binaire)
            if [[ -n "$bin" ]] && ! command -v "$bin" &> /dev/null; then
                continue
            fi

            # Affichage du titre une seule fois si au moins un item est valide
            if [ "$has_printed_title" = false ]; then
                echo -e "${COLOR_CYAN}[ $title ]${NONE}"
                has_printed_title=true
            fi

            display_line "$label" "$cmd"

        done <<< "$items"
    done
}

draw_bar() {
    local pct=$1
    # Nettoyage du point si présent (arrondi simple)
    [[ "$pct" == *"."* ]] && pct=${pct%.*}

    # Sécurité pour ne pas dépasser 100 ou descendre sous 0
    ((pct > 100)) && pct=100
    ((pct < 0)) && pct=0

    # Sélection automatique de la couleur
    local color="${COLOR_LIGHT_BLUE}"
    if (( pct >= 90 )); then color="${COLOR_LIGHT_RED}"
    elif (( pct >= 70 )); then color="${COLOR_ORANGE}"
    elif (( pct >= 50 )); then color="${COLOR_YELLOW}"
    elif (( pct >= 30 )); then color="${COLOR_LIGHT_GREEN}"
    elif (( pct >= 15 )); then color="${COLOR_GREEN}"
    fi

    local size=20
    local filled=$((pct * size / 100))
    local empty=$((size - filled))

    # Affichage de la barre
    echo -ne "${NONE}["
    echo -ne "${color}"
    for ((i=0; i<filled; i++)); do echo -ne "■"; done
    echo -ne "${COLOR_GRAY}"
    for ((i=0; i<empty; i++)); do echo -ne "·"; done
    echo -ne "${NONE}] ${color}${pct}%${NONE}"
}
draw_ascii_art() {

  local L_IPS L_UPT L_HRD L_CPU_BAR L_MEM_BAR L_HDD_BAR L_USR L_DAT
    # Pré-formatage des étiquettes pour l'alignement
    L_IPS="${COLOR_YELLOW}IP Addresses.: ${F_IPS}"
    L_UPT="${COLOR_YELLOW}Uptime.......: ${F_UPT}"
    L_HRD="${COLOR_YELLOW}Hardware.....: ${F_HARD}"
    L_CPU="${COLOR_YELLOW}CPU..........: ${F_CPU}"
    L_CPU_BAR="${COLOR_YELLOW}CPU Usage....: ${F_BAR_CPU}"
    L_MEM_BAR="${COLOR_YELLOW}RAM Usage....: ${F_BAR_MEM}"
    L_HDD_BAR="${COLOR_YELLOW}Disk Usage...: ${F_BAR_DSK}"
    L_USR="${COLOR_YELLOW}Nb acc user..: ${F_USR}"
    L_DAT="${COLOR_WHITE}$(LC_TIME=fr_FR.UTF-8 date +"%A, %e %B %Y, %r")"

    clear
    echo -e "${COLOR_CYAN}        ==[ You are connected to ${COLOR_WHITE}$(hostname -f)${COLOR_CYAN} ]=="
    echo -e "${COLOR_GREEN}          _ __ _"
    echo -e "${COLOR_GREEN}      ((-)).--.((-))        ${L_DAT}"
    echo -e "${COLOR_GREEN}      /     ''     \\       ${F_OS}"
    echo -e "${COLOR_GREEN}     (   \______/   )"
    echo -e "${COLOR_GREEN}      \    (  )    /        ${L_IPS}"
    echo -e "${COLOR_GREEN}      / /~~~~~~~~\ \        ${L_CPU}"
    echo -e "${COLOR_GREEN} /~~\/ /          \ \/~~\   ${L_HRD}"
    echo -e "${COLOR_GREEN}(   ( (            ) )   )  ${L_UPT}"
    echo -e "${COLOR_GREEN} \ \ \ \          / / / /   ${L_USR}"
    echo -e "${COLOR_GREEN} _\ \/  \.______./  \/ /_   ${L_CPU_BAR}"
    echo -e "${COLOR_GREEN} ___/ /\__________/\ \___   ${L_MEM_BAR}"
    echo -e "${COLOR_GREEN}=========================== ${L_HDD_BAR}${NONE}"
}

display_dashboard() {
    # Déclaration de toutes les variables partagées en local
    local F_IPS F_UPT F_BAR_CPU F_BAR_DSK F_USR F_HARD F_OS

    # Exécution des sondes
    get_os_info
    get_hardware_info
    get_network_info
    get_uptime_info
    get_memory_info
    get_load_info
    get_disk_info
    get_disk_info
    get_user_info
    get_disk_info

    # Affichage
    draw_ascii_art
}
