package com.tiedup.remake.client.gui.util;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Utility class for rendering stat bars and value-colored elements in GUIs.
 * Centralizes stat bar rendering logic to reduce code duplication.
 */
@OnlyIn(Dist.CLIENT)
public final class GuiStatsRenderer {

    private GuiStatsRenderer() {}

    // ARGB colors for stat values (green -> orange -> red)
    private static final int COLOR_GREEN = 0xFF4CAF50;
    private static final int COLOR_ORANGE = 0xFFFF9800;
    private static final int COLOR_RED = 0xFFF44336;

    /**
     * Render a stat bar with label and value-based coloring.
     *
     * @param graphics Graphics context
     * @param font Font renderer
     * @param x Left position
     * @param y Top position
     * @param label Stat label (e.g., "Hunger", "Willpower")
     * @param value Stat value (0-100)
     * @param totalWidth Total width including label and bar
     */
    public static void renderStatBar(
        GuiGraphics graphics,
        Font font,
        int x,
        int y,
        String label,
        float value,
        int totalWidth
    ) {
        // Draw label
        int labelWidth = font.width(label) + 2;
        graphics.drawString(font, label, x, y, 0xFFAAAAAA, false);

        // Calculate bar dimensions
        int barX = x + labelWidth;
        int barW = totalWidth - labelWidth;
        int barH = 6;

        // Background (black)
        graphics.fill(barX, y + 1, barX + barW, y + 1 + barH, 0xFF000000);

        // Fill based on value (0-100)
        int fillWidth = (int) (barW * (value / 100f));
        int fillColor = getValueColorArgb(value);
        if (fillWidth > 0) {
            graphics.fill(
                barX + 1,
                y + 2,
                barX + 1 + Math.min(fillWidth, barW - 2),
                y + barH,
                fillColor
            );
        }
    }

    /**
     * Get RGB color for a stat value (green -> orange -> red).
     * Used for text rendering.
     *
     * @param value Stat value (0-100)
     * @return RGB color value (0xRRGGBB)
     */
    public static int getValueColor(float value) {
        if (value >= 70) return 0x4CAF50; // Green
        if (value >= 40) return 0xFF9800; // Orange
        return 0xF44336; // Red
    }

    /**
     * Get ARGB color for a stat value (green -> orange -> red).
     * Used for fill operations.
     *
     * @param value Stat value (0-100)
     * @return ARGB color value (0xAARRGGBB)
     */
    public static int getValueColorArgb(float value) {
        if (value >= 70) return COLOR_GREEN;
        if (value >= 40) return COLOR_ORANGE;
        return COLOR_RED;
    }
}
