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.client.state.ClientLaborState;
import com.tiedup.remake.core.TiedUpMod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
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 labor task progress bar.
 * Displayed in the top-right corner when a labor task is active.
 */
@OnlyIn(Dist.CLIENT)
@Mod.EventBusSubscriber(modid = TiedUpMod.MOD_ID, value = Dist.CLIENT)
public class LaborProgressOverlay {

    // Bar dimensions
    private static final int BAR_WIDTH = 150;
    private static final int BAR_HEIGHT = 10;
    private static final int PADDING = 6;
    private static final int MARGIN = 10;

    // Animation
    private static float smoothProgress = 0.0f;
    private static final float SMOOTH_SPEED = 0.1f;

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

        // Check if we have an active task
        if (!ClientLaborState.hasActiveTask()) {
            // Fade out smoothly
            smoothProgress = Math.max(0, smoothProgress - SMOOTH_SPEED);
            if (smoothProgress <= 0.01f) {
                return;
            }
        } else {
            // Smooth interpolation towards target
            float target = ClientLaborState.getProgressFraction();
            smoothProgress += (target - smoothProgress) * SMOOTH_SPEED;
        }

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

        GuiGraphics graphics = event.getGuiGraphics();
        int screenWidth = mc.getWindow().getGuiScaledWidth();

        // Position in top-right corner
        int x = screenWidth - BAR_WIDTH - PADDING * 2 - MARGIN;
        int y = MARGIN;

        // Calculate panel dimensions
        int panelWidth = BAR_WIDTH + PADDING * 2;
        int panelHeight = BAR_HEIGHT + PADDING * 2 + mc.font.lineHeight * 2 + 6;

        // Background panel
        graphics.fill(
            x,
            y,
            x + panelWidth,
            y + panelHeight,
            GuiColors.withAlpha(GuiColors.BG_DARK, 220)
        );

        // Border
        GuiRenderUtil.drawBorder(
            graphics,
            x,
            y,
            panelWidth,
            panelHeight,
            GuiColors.ACCENT_TAN
        );

        // Task description
        String description = ClientLaborState.getTaskDescription();
        graphics.drawString(
            mc.font,
            description,
            x + PADDING,
            y + PADDING,
            GuiColors.TEXT_WHITE,
            false
        );

        // Progress bar position
        int barX = x + PADDING;
        int barY = y + PADDING + mc.font.lineHeight + 4;

        // Progress bar background
        graphics.fill(
            barX,
            barY,
            barX + BAR_WIDTH,
            barY + BAR_HEIGHT,
            GuiColors.BG_LIGHT
        );

        // Progress bar fill
        int fillWidth = (int) (BAR_WIDTH * smoothProgress);
        int fillColor =
            smoothProgress >= 1.0f ? GuiColors.SUCCESS : GuiColors.ACCENT_TAN;
        graphics.fill(
            barX,
            barY,
            barX + fillWidth,
            barY + BAR_HEIGHT,
            fillColor
        );

        // Progress bar border
        GuiRenderUtil.drawBorder(
            graphics,
            barX,
            barY,
            BAR_WIDTH,
            BAR_HEIGHT,
            GuiColors.BORDER_LIGHT
        );

        // Progress text inside bar
        String progressStr = ClientLaborState.getProgressString();
        int textWidth = mc.font.width(progressStr);
        graphics.drawString(
            mc.font,
            progressStr,
            barX + (BAR_WIDTH - textWidth) / 2,
            barY + (BAR_HEIGHT - mc.font.lineHeight) / 2 + 1,
            GuiColors.TEXT_WHITE,
            false
        );

        // Value text below bar
        String valueStr = ClientLaborState.getValueEmeralds() + " emeralds";
        graphics.drawString(
            mc.font,
            valueStr,
            x + PADDING,
            barY + BAR_HEIGHT + 2,
            GuiColors.TEXT_GRAY,
            false
        );
    }
}
