#!/bin/bash
# world_overview_generator.sh - Erzeugt die Welten-Übersichtsseite

generate_worlds_overview() {
    local overview_file="${WEB_ROOT_PATH}/worlds.html"
    log_message "Erzeuge Weltenübersicht: ${overview_file}..."
    {
        generate_html_header "Weltenübersicht" "." "worlds"
        echo "<h2>Unsere Welten</h2>"
    } > "$overview_file"
    
    local discovered_worlds_count=0
    shopt -s nullglob
    local world_key_dirs=("${MINETESTMAPPER_WORLD_DATA_BASE_PATH}"/*/)
    shopt -u nullglob

    if [ ${#world_key_dirs[@]} -eq 0 ]; then
        log_message "WARNUNG: Keine Welt-Verzeichnisse in ${MINETESTMAPPER_WORLD_DATA_BASE_PATH}."
        echo "<p>Keine Welten verfügbar.</p>" >> "$overview_file"
    else
        local overview_entry_template_path="${TEMPLATE_DIR_PATH}/worlds_overview_entry.template"
        for world_data_dir_loop_overview in "${world_key_dirs[@]}"; do
            local current_world_key
            current_world_key=$(basename "$world_data_dir_loop_overview")
            local world_mt_file="${world_data_dir_loop_overview}world.mt"
            local web_conf_file="${world_data_dir_loop_overview}web.conf"
            if [ ! -f "$world_mt_file" ] || [ ! -f "$web_conf_file" ]; then continue; fi
            
            local world_display_name_ov
            world_display_name_ov=$(get_config_value_from_file "$web_conf_file" "WORLD_DISPLAY_NAME")
            local world_short_desc_ov
            world_short_desc_ov=$(get_config_value_from_file "$web_conf_file" "WORLD_SHORT_DESCRIPTION" "$DEFAULT_WORLD_SHORT_DESCRIPTION")
            
            unset ADMIN_NAME
            source "$web_conf_file" &>/dev/null
            local admin_name_ov
            if [ ${#ADMIN_NAME[@]} -gt 0 ]; then admin_name_ov=$(IFS=', '; echo "${ADMIN_NAME[*]}"); else admin_name_ov="$SITE_OWNER_NAME"; fi
            
            local current_map_png_filename_for_preview_ov
            current_map_png_filename_for_preview_ov=$(get_config_value_from_file "$web_conf_file" "WEB_MAP_PNG_FILENAME" "$DEFAULT_WEB_MAP_PNG_FILENAME")

            if [ -z "$world_display_name_ov" ]; then 
                local mt_world_name_from_mt_ov
                mt_world_name_from_mt_ov=$(grep -E "^\s*world_name\s*=" "$world_mt_file" | tail -n 1 | cut -d'=' -f2 | xargs)
                if [ -n "$mt_world_name_from_mt_ov" ]; then world_display_name_ov="$mt_world_name_from_mt_ov"; 
                else world_display_name_ov="Welt ${current_world_key}"; fi
            fi
            
            local detail_page_filename="world_${current_world_key}.html"
            local preview_img_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/${current_map_png_filename_for_preview_ov}"
            local preview_img_abs_path="${WEB_ROOT_PATH}/${preview_img_rel_path}"

            # KORREKTUR: Logik zur Status-Prüfung entfernt. Stattdessen wird nur der Pfad zur Status-Datei erstellt.
            local status_url_path="/${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/online_status.txt"
            
            local preview_img_html
            if [ -f "$preview_img_abs_path" ]; then
                preview_img_html="<img src='${preview_img_rel_path}?v=${CACHE_BUSTER}' alt='Vorschau ${world_display_name_ov}'>"
            else
                preview_img_html="<img src='images/placeholder_map.png?v=${CACHE_BUSTER}' alt='Keine Vorschau für ${world_display_name_ov}'>"
            fi
            
            local temp_overview_entry_file
            temp_overview_entry_file=$(mktemp)
            render_template "$overview_entry_template_path" "$temp_overview_entry_file" \
                "detail_page_filename" "$detail_page_filename" \
                "preview_img_html" "$preview_img_html" \
                "world_display_name_ov" "$world_display_name_ov" \
                "status_url" "$status_url_path" \
                "admin_name_ov" "$admin_name_ov" \
                "world_short_desc_ov" "$world_short_desc_ov"
            cat "$temp_overview_entry_file" >> "$overview_file"
            rm "$temp_overview_entry_file"
            
            discovered_worlds_count=$((discovered_worlds_count + 1))
        done
        if [ "$discovered_worlds_count" -eq 0 ]; then echo "<p>Keine Welten mit gültiger Konfiguration gefunden.</p>" >> "$overview_file"; fi
    fi
    generate_html_footer >> "$overview_file"
}
