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

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

/**
 * Utility methods for GUI rendering.
 */
@OnlyIn(Dist.CLIENT)
public final class GuiRenderUtil {

    // Vanilla MC 3D border colors
    public static final int MC_PANEL_BG = 0xFFC6C6C6;
    public static final int MC_HIGHLIGHT_OUTER = 0xFFFFFFFF;
    public static final int MC_HIGHLIGHT_INNER = 0xFFDBDBDB;
    public static final int MC_SHADOW_INNER = 0xFF555555;
    public static final int MC_SHADOW_OUTER = 0xFF373737;
    public static final int MC_SLOT_BG = 0xFF8B8B8B;
    public static final int MC_TEXT_DARK = 0xFF404040;
    public static final int MC_TEXT_GRAY = 0xFF555555;

    private GuiRenderUtil() {}

    /**
     * Draw a vanilla MC-style raised panel (light gray with 3D beveled borders).
     */
    public static void drawMCPanel(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        // Fill
        graphics.fill(x, y, x + width, y + height, MC_PANEL_BG);
        // Top highlight (outer white, inner light)
        graphics.fill(x, y, x + width, y + 1, MC_HIGHLIGHT_OUTER);
        graphics.fill(x + 1, y + 1, x + width - 1, y + 2, MC_HIGHLIGHT_INNER);
        // Left highlight
        graphics.fill(x, y, x + 1, y + height, MC_HIGHLIGHT_OUTER);
        graphics.fill(x + 1, y + 1, x + 2, y + height - 1, MC_HIGHLIGHT_INNER);
        // Bottom shadow
        graphics.fill(
            x,
            y + height - 1,
            x + width,
            y + height,
            MC_SHADOW_OUTER
        );
        graphics.fill(
            x + 1,
            y + height - 2,
            x + width - 1,
            y + height - 1,
            MC_SHADOW_INNER
        );
        // Right shadow
        graphics.fill(x + width - 1, y, x + width, y + height, MC_SHADOW_OUTER);
        graphics.fill(
            x + width - 2,
            y + 1,
            x + width - 1,
            y + height - 1,
            MC_SHADOW_INNER
        );
    }

    /**
     * Draw a vanilla MC-style sunken panel (inverted 3D borders — dark outside, light inside).
     */
    public static void drawMCSunkenPanel(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        graphics.fill(x, y, x + width, y + height, MC_SLOT_BG);
        // Top shadow (dark)
        graphics.fill(x, y, x + width, y + 1, MC_SHADOW_OUTER);
        graphics.fill(x + 1, y + 1, x + width - 1, y + 2, MC_SHADOW_INNER);
        // Left shadow (dark)
        graphics.fill(x, y, x + 1, y + height, MC_SHADOW_OUTER);
        graphics.fill(x + 1, y + 1, x + 2, y + height - 1, MC_SHADOW_INNER);
        // Bottom highlight (light)
        graphics.fill(
            x,
            y + height - 1,
            x + width,
            y + height,
            MC_HIGHLIGHT_OUTER
        );
        graphics.fill(
            x + 1,
            y + height - 2,
            x + width - 1,
            y + height - 1,
            MC_HIGHLIGHT_INNER
        );
        // Right highlight (light)
        graphics.fill(
            x + width - 1,
            y,
            x + width,
            y + height,
            MC_HIGHLIGHT_OUTER
        );
        graphics.fill(
            x + width - 2,
            y + 1,
            x + width - 1,
            y + height - 1,
            MC_HIGHLIGHT_INNER
        );
    }

    /**
     * Draw a vanilla MC-style sunken slot.
     */
    public static void drawMCSlot(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        graphics.fill(x, y, x + width, y + height, MC_SLOT_BG);
        // Top shadow
        graphics.fill(x, y, x + width, y + 1, MC_SHADOW_OUTER);
        // Left shadow
        graphics.fill(x, y, x + 1, y + height, MC_SHADOW_OUTER);
        // Bottom highlight
        graphics.fill(
            x,
            y + height - 1,
            x + width,
            y + height,
            MC_HIGHLIGHT_OUTER
        );
        // Right highlight
        graphics.fill(
            x + width - 1,
            y,
            x + width,
            y + height,
            MC_HIGHLIGHT_OUTER
        );
    }

    /**
     * Draw vanilla-style slot hover overlay (white semi-transparent).
     */
    public static void drawSlotHover(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        graphics.fill(x + 1, y + 1, x + width - 1, y + height - 1, 0x80FFFFFF);
    }

    /**
     * Draw a vanilla MC-style button (raised 3D appearance).
     */
    public static void drawMCButton(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height,
        boolean hovered,
        boolean enabled
    ) {
        int bg = enabled ? (hovered ? 0xFFA0A0A0 : MC_SLOT_BG) : 0xFF606060;
        graphics.fill(x, y, x + width, y + height, bg);
        if (enabled) {
            // Top highlight
            graphics.fill(x, y, x + width, y + 1, MC_HIGHLIGHT_OUTER);
            // Left highlight
            graphics.fill(x, y, x + 1, y + height, MC_HIGHLIGHT_OUTER);
            // Bottom shadow
            graphics.fill(
                x,
                y + height - 1,
                x + width,
                y + height,
                MC_SHADOW_OUTER
            );
            // Right shadow
            graphics.fill(
                x + width - 1,
                y,
                x + width,
                y + height,
                MC_SHADOW_OUTER
            );
        } else {
            // Flat dark border for disabled
            graphics.fill(x, y, x + width, y + 1, 0xFF505050);
            graphics.fill(x, y + height - 1, x + width, y + height, 0xFF505050);
            graphics.fill(x, y, x + 1, y + height, 0xFF505050);
            graphics.fill(x + width - 1, y, x + width, y + height, 0xFF505050);
        }
    }

    /**
     * Draw a selected slot highlight border (gold/yellow).
     */
    public static void drawSelectedBorder(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height
    ) {
        int gold = 0xFFFFD700;
        // Top
        graphics.fill(x, y, x + width, y + 1, gold);
        graphics.fill(x, y + 1, x + width, y + 2, gold);
        // Bottom
        graphics.fill(x, y + height - 2, x + width, y + height - 1, gold);
        graphics.fill(x, y + height - 1, x + width, y + height, gold);
        // Left
        graphics.fill(x, y, x + 1, y + height, gold);
        graphics.fill(x + 1, y, x + 2, y + height, gold);
        // Right
        graphics.fill(x + width - 2, y, x + width - 1, y + height, gold);
        graphics.fill(x + width - 1, y, x + width, y + height, gold);
    }

    /**
     * Draw centered text WITHOUT shadow (vanilla drawCenteredString always adds shadow).
     * Use this for dark text on light MC panels.
     */
    public static void drawCenteredStringNoShadow(
        GuiGraphics graphics,
        net.minecraft.client.gui.Font font,
        String text,
        int centerX,
        int y,
        int color
    ) {
        int textWidth = font.width(text);
        graphics.drawString(
            font,
            text,
            centerX - textWidth / 2,
            y,
            color,
            false
        );
    }

    /**
     * Draw a 1-pixel border around a rectangle.
     *
     * @param graphics The graphics context
     * @param x Left position
     * @param y Top position
     * @param width Rectangle width
     * @param height Rectangle height
     * @param color Border color (ARGB)
     */
    public static void drawBorder(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height,
        int color
    ) {
        // Top
        graphics.fill(x, y, x + width, y + 1, color);
        // Bottom
        graphics.fill(x, y + height - 1, x + width, y + height, color);
        // Left
        graphics.fill(x, y, x + 1, y + height, color);
        // Right
        graphics.fill(x + width - 1, y, x + width, y + height, color);
    }

    /**
     * Draw a border with custom thickness around a rectangle.
     *
     * @param graphics The graphics context
     * @param x Left position
     * @param y Top position
     * @param width Rectangle width
     * @param height Rectangle height
     * @param thickness Border thickness in pixels
     * @param color Border color (ARGB)
     */
    public static void drawBorder(
        GuiGraphics graphics,
        int x,
        int y,
        int width,
        int height,
        int thickness,
        int color
    ) {
        // Top
        graphics.fill(x, y, x + width, y + thickness, color);
        // Bottom
        graphics.fill(x, y + height - thickness, x + width, y + height, color);
        // Left
        graphics.fill(x, y, x + thickness, y + height, color);
        // Right
        graphics.fill(x + width - thickness, y, x + width, y + height, color);
    }
}
