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

import com.tiedup.remake.client.gui.util.GuiColors;
import com.tiedup.remake.client.gui.util.GuiRenderUtil;
import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.state.PlayerBindState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * Overlay that shows status icons when player is restrained.
 * Icons appear in top-left corner showing current bondage state.
 *
 */
@OnlyIn(Dist.CLIENT)
@Mod.EventBusSubscriber(modid = TiedUpMod.MOD_ID, value = Dist.CLIENT)
public class StatusOverlay {

    // Icon size and spacing
    private static final int ICON_SIZE = 16;
    private static final int PADDING = 2;
    private static final int MARGIN = 5;

    // Visibility toggle
    private static boolean visible = true;

    // Icon colors (use centralized GuiColors)
    private static final int COLOR_BOUND = GuiColors.TYPE_BIND;
    private static final int COLOR_GAGGED = GuiColors.TYPE_GAG;
    private static final int COLOR_BLIND = GuiColors.TYPE_BLINDFOLD;
    private static final int COLOR_DEAF = GuiColors.TYPE_EARPLUGS;
    private static final int COLOR_COLLAR = GuiColors.TYPE_COLLAR;
    private static final int COLOR_MITTENS = GuiColors.TYPE_MITTENS;

    /**
     * Toggle overlay visibility.
     */
    public static void toggleVisibility() {
        visible = !visible;
    }

    /**
     * Set overlay visibility.
     */
    public static void setVisible(boolean vis) {
        visible = vis;
    }

    /**
     * Check if overlay is visible.
     */
    public static boolean isVisible() {
        return visible;
    }

    @SubscribeEvent
    public static void onRenderOverlay(RenderGuiOverlayEvent.Post event) {
        // Only render after hotbar
        if (event.getOverlay() != VanillaGuiOverlay.HOTBAR.type()) {
            return;
        }

        if (!visible) {
            return;
        }

        Minecraft mc = Minecraft.getInstance();
        Player player = mc.player;
        if (player == null) {
            return;
        }

        PlayerBindState state = PlayerBindState.getInstance(player);
        if (state == null) {
            return;
        }

        // Don't show if not restrained at all
        if (
            !state.isTiedUp() &&
            !state.isGagged() &&
            !state.isBlindfolded() &&
            !state.hasEarplugs() &&
            !state.hasCollar() &&
            !state.hasMittens()
        ) {
            return;
        }

        GuiGraphics graphics = event.getGuiGraphics();

        // Position: top-left corner
        int x = MARGIN;
        int y = MARGIN;

        // Background panel
        int iconCount = countActiveIcons(state);
        if (iconCount > 0) {
            int cols = Math.min(iconCount, 3);
            int rows = (iconCount + 2) / 3;
            int panelWidth = cols * (ICON_SIZE + PADDING) + PADDING;
            int panelHeight = rows * (ICON_SIZE + PADDING) + PADDING;

            // Semi-transparent background
            graphics.fill(
                x - PADDING,
                y - PADDING,
                x + panelWidth,
                y + panelHeight,
                GuiColors.withAlpha(GuiColors.BG_DARK, 180)
            );
        }

        // Render icons in grid (max 3 per row)
        int iconIndex = 0;

        if (state.isTiedUp()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_BOUND, "B");
        }
        if (state.isGagged()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_GAGGED, "G");
        }
        if (state.isBlindfolded()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_BLIND, "X");
        }
        if (state.hasEarplugs()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_DEAF, "D");
        }
        if (state.hasCollar()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_COLLAR, "C");
        }
        if (state.hasMittens()) {
            renderIcon(graphics, x, y, iconIndex++, COLOR_MITTENS, "M");
        }
    }

    /**
     * Count active status icons.
     */
    private static int countActiveIcons(PlayerBindState state) {
        int count = 0;
        if (state.isTiedUp()) count++;
        if (state.isGagged()) count++;
        if (state.isBlindfolded()) count++;
        if (state.hasEarplugs()) count++;
        if (state.hasCollar()) count++;
        if (state.hasMittens()) count++;
        return count;
    }

    /**
     * Render a single status icon.
     * Uses colored squares with letters as placeholders until proper textures are made.
     */
    private static void renderIcon(
        GuiGraphics graphics,
        int baseX,
        int baseY,
        int index,
        int color,
        String letter
    ) {
        int col = index % 3;
        int row = index / 3;

        int x = baseX + col * (ICON_SIZE + PADDING);
        int y = baseY + row * (ICON_SIZE + PADDING);

        // Icon background
        graphics.fill(x, y, x + ICON_SIZE, y + ICON_SIZE, color);

        // Border
        GuiRenderUtil.drawBorder(
            graphics,
            x,
            y,
            ICON_SIZE,
            ICON_SIZE,
            GuiColors.darken(color, 0.3f)
        );

        // Letter (centered)
        Minecraft mc = Minecraft.getInstance();
        int textWidth = mc.font.width(letter);
        graphics.drawString(
            mc.font,
            letter,
            x + (ICON_SIZE - textWidth) / 2,
            y + (ICON_SIZE - mc.font.lineHeight) / 2 + 1,
            GuiColors.TEXT_WHITE,
            false
        );
    }
}
