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

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

/**
 * Utility class for rendering vanilla-style GUI elements.
 * Provides methods to draw backgrounds using Minecraft's textures.
 */
@OnlyIn(Dist.CLIENT)
public final class GuiTextureHelper {

    private GuiTextureHelper() {}

    // === Vanilla Textures ===
    public static final ResourceLocation CHEST_BACKGROUND =
        ResourceLocation.fromNamespaceAndPath(
            "minecraft",
            "textures/gui/container/generic_54.png"
        );

    public static final ResourceLocation INVENTORY_BACKGROUND =
        ResourceLocation.fromNamespaceAndPath(
            "minecraft",
            "textures/gui/container/inventory.png"
        );

    // === Chest Texture Dimensions ===
    public static final int CHEST_WIDTH = 176;
    public static final int CHEST_ROW_HEIGHT = 18;
    public static final int CHEST_HEADER_HEIGHT = 17;
    public static final int CHEST_PLAYER_INV_HEIGHT = 96;
    public static final int SLOT_SIZE = 18;

    /**
     * Render a chest-style background with variable number of rows.
     * Uses the vanilla generic_54.png texture (large chest).
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Top position
     * @param rows Number of inventory rows (1-6)
     */
    public static void renderChestBackground(
        GuiGraphics graphics,
        int x,
        int y,
        int rows
    ) {
        rows = Math.min(6, Math.max(1, rows));

        // Header section (title area) - top 17 pixels
        graphics.blit(
            CHEST_BACKGROUND,
            x,
            y,
            0,
            0,
            CHEST_WIDTH,
            CHEST_HEADER_HEIGHT
        );

        // Inventory rows - 18 pixels each
        for (int i = 0; i < rows; i++) {
            graphics.blit(
                CHEST_BACKGROUND,
                x,
                y + CHEST_HEADER_HEIGHT + i * CHEST_ROW_HEIGHT,
                0,
                CHEST_HEADER_HEIGHT,
                CHEST_WIDTH,
                CHEST_ROW_HEIGHT
            );
        }

        // Player inventory section (bottom part with separator + 3 rows + hotbar)
        // In generic_54.png, player section starts at y=125 and is 96 pixels tall
        graphics.blit(
            CHEST_BACKGROUND,
            x,
            y + CHEST_HEADER_HEIGHT + rows * CHEST_ROW_HEIGHT,
            0,
            125,
            CHEST_WIDTH,
            CHEST_PLAYER_INV_HEIGHT
        );
    }

    /**
     * Calculate total height for a chest-style container with N rows.
     *
     * @param rows Number of inventory rows
     * @return Total height in pixels
     */
    public static int getChestHeight(int rows) {
        return (
            CHEST_HEADER_HEIGHT +
            rows * CHEST_ROW_HEIGHT +
            CHEST_PLAYER_INV_HEIGHT
        );
    }

    /**
     * Render a vanilla-style 3D beveled panel.
     * Creates a dark panel with highlight on top/left and shadow on bottom/right.
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Top position
     * @param width Panel width
     * @param height Panel height
     */
    public static void renderBeveledPanel(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        // Main background (dark gray)
        graphics.fill(x, y, x + width, y + height, 0xC0101010);

        // Top highlight (light gray)
        graphics.fill(x, y, x + width, y + 2, 0xFF373737);
        // Left highlight
        graphics.fill(x, y, x + 2, y + height, 0xFF373737);

        // Bottom shadow (black)
        graphics.fill(x, y + height - 2, x + width, y + height, 0xFF000000);
        // Right shadow
        graphics.fill(x + width - 2, y, x + width, y + height, 0xFF000000);

        // Inner fill (slightly lighter)
        graphics.fill(x + 2, y + 2, x + width - 2, y + height - 2, 0xFF2D2D2D);
    }

    /**
     * Render a small equipment panel (for armor/weapon slots).
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Top position
     * @param slots Number of slots vertically
     */
    public static void renderEquipmentPanel(
        GuiGraphics graphics,
        int x,
        int y,
        int slots
    ) {
        int width = 26;
        int height = 8 + slots * SLOT_SIZE;

        // Background
        graphics.fill(x, y, x + width, y + height, 0xC0101010);

        // Border
        graphics.fill(x, y, x + width, y + 1, 0xFF373737);
        graphics.fill(x, y, x + 1, y + height, 0xFF373737);
        graphics.fill(x, y + height - 1, x + width, y + height, 0xFF000000);
        graphics.fill(x + width - 1, y, x + width, y + height, 0xFF000000);

        // Inner
        graphics.fill(x + 1, y + 1, x + width - 1, y + height - 1, 0xFF2D2D2D);

        // Slot backgrounds
        for (int i = 0; i < slots; i++) {
            int slotX = x + 4;
            int slotY = y + 4 + i * SLOT_SIZE;
            renderSlotBackground(graphics, slotX, slotY);
        }
    }

    /**
     * Render a single slot background (18x18 with inner shadow).
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Top position
     */
    public static void renderSlotBackground(
        GuiGraphics graphics,
        int x,
        int y
    ) {
        // Outer dark border
        graphics.fill(x, y, x + SLOT_SIZE, y + SLOT_SIZE, 0xFF373737);
        // Inner lighter area
        graphics.fill(
            x + 1,
            y + 1,
            x + SLOT_SIZE - 1,
            y + SLOT_SIZE - 1,
            0xFF8B8B8B
        );
        // Slot center (dark)
        graphics.fill(
            x + 1,
            y + 1,
            x + SLOT_SIZE - 1,
            y + SLOT_SIZE - 1,
            0xFF373737
        );
    }

    /**
     * Render a vanilla-style XP progress bar.
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Top position
     * @param width Bar width
     * @param progress Progress value (0.0 to 1.0)
     * @param color Fill color (ARGB)
     */
    public static void renderProgressBar(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        float progress,
        int color
    ) {
        // Background (black with inner dark gray)
        graphics.fill(x, y, x + width, y + 5, 0xFF000000);
        graphics.fill(x + 1, y + 1, x + width - 1, y + 4, 0xFF2E2E2E);

        // Fill
        int fillWidth = (int) ((width - 2) *
            Math.min(1.0f, Math.max(0.0f, progress)));
        if (fillWidth > 0) {
            graphics.fill(x + 1, y + 1, x + 1 + fillWidth, y + 4, color);
        }
    }

    /**
     * Render a vanilla-style XP bar with green color.
     */
    public static void renderXpBar(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        float progress
    ) {
        renderProgressBar(graphics, x, y, width, progress, 0xFF80FF20);
    }

    /**
     * Render a horizontal separator line.
     *
     * @param graphics GuiGraphics context
     * @param x Left position
     * @param y Y position
     * @param width Line width
     */
    public static void renderSeparator(
        GuiGraphics graphics,
        int x,
        int y,
        int width
    ) {
        graphics.fill(x, y, x + width, y + 1, 0xFF373737);
        graphics.fill(x, y + 1, x + width, y + 2, 0xFF000000);
    }
}
