import { world, HudElement, HudVisibility } from "@minecraft/server";
import { CONFIG } from "./dss_hud_config.js";

// Mapping of configuration keys to corresponding HUD elements
const HUD_MAPPING = {
    PAPERDOLL: HudElement.PaperDoll,
    ARMOR: HudElement.Armor,
    TOOLTIPS: HudElement.ToolTips,
    TOUCHCONTROLS: HudElement.TouchControls,
    CROSSHAIR: HudElement.Crosshair,
    HOTBAR: HudElement.Hotbar,
    HEALTH: HudElement.Health,
    PROGRESSBAR: HudElement.ProgressBar,
    HUNGER: HudElement.Hunger,
    AIRBUBBLES: HudElement.AirBubbles,
    HORSEHEALTH: HudElement.HorseHealth,
    STATUSEFFECTS: HudElement.StatusEffects,
    ITEMTEXT: HudElement.ItemText
};

// Function to update HUD visibility based on configuration
function updateHUDVisibility(player) {
    try {
        const hudConfig = CONFIG.HUD || {};
        const elementsToShow = [];
        const elementsToHide = [];

        // Iterate through the HUD configuration settings
        for (const [key, isVisible] of Object.entries(hudConfig)) {
            const hudElement = HUD_MAPPING[key];
            if (hudElement !== undefined) {
                if (isVisible) {
                    elementsToShow.push(hudElement);
                } else {
                    elementsToHide.push(hudElement);
                }
            } else {
                logMessage("warn", `Unknown HUD key: "${key}" in configuration.`);
            }
        }

        // Batch update HUD elements to show
        if (elementsToShow.length > 0) {
            player.onScreenDisplay.setHudVisibility(HudVisibility.Reset, elementsToShow);
        }

        // Batch update HUD elements to hide
        if (elementsToHide.length > 0) {
            player.onScreenDisplay.setHudVisibility(HudVisibility.Hide, elementsToHide);
        }
    } catch (error) {
        logMessage("error", `Error updating HUD visibility: ${error.message}`);
    }
}

// Enhanced initialization for HUD settings
function initializeHUDSettings(player) {
    try {
        if (!player || !player.onScreenDisplay) {
            throw new Error("Player or onScreenDisplay not available.");
        }
        updateHUDVisibility(player);
    } catch (error) {
        logMessage("error", `Error during HUD initialization: ${error.message}`);
    }
}

// Subscribe to relevant events for consistent HUD updates
world.afterEvents.playerSpawn.subscribe((event) => {
    const player = event.player;
    initializeHUDSettings(player);
});

world.afterEvents.playerDimensionChange.subscribe((event) => {
    const player = event.player;
    initializeHUDSettings(player);
});

// Error and warning logging utility
function logMessage(type, message) {
    switch (type) {
        case "info":
            console.info(`[INFO]: ${message}`);
            break;
        case "warn":
            console.warn(`[WARNING]: ${message}`);
            break;
        case "error":
            console.error(`[ERROR]: ${message}`);
            break;
        default:
            console.log(`[LOG]: ${message}`);
    }
}
