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

import com.google.common.collect.ImmutableList;
import com.tiedup.remake.bounty.Bounty;
import com.tiedup.remake.client.gui.util.GuiColors;
import com.tiedup.remake.client.gui.util.GuiLayoutConstants;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * List Entry for BountyListScreen.
 * Displays bounty details (target, reward, time).
 * Refactored to be a ContainerObjectSelectionList.Entry.
 */
@OnlyIn(Dist.CLIENT)
public class BountyEntryWidget
    extends ContainerObjectSelectionList.Entry<BountyEntryWidget>
{

    private final Bounty bounty;
    private final Consumer<BountyEntryWidget> onSelect;
    private boolean selected = false;

    public BountyEntryWidget(
        Bounty bounty,
        Consumer<BountyEntryWidget> onSelect
    ) {
        this.bounty = bounty;
        this.onSelect = onSelect;
    }

    public Bounty getBounty() {
        return bounty;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    @Override
    public void render(
        GuiGraphics graphics,
        int index,
        int top,
        int left,
        int width,
        int height,
        int mouseX,
        int mouseY,
        boolean hovering,
        float partialTick
    ) {
        Minecraft mc = Minecraft.getInstance();
        Font font = mc.font;

        // Background
        int bgColor = selected
            ? GuiColors.ACCENT_BROWN
            : (hovering ? GuiColors.BG_LIGHT : GuiColors.BG_DARK);
        graphics.fill(left, top, left + width, top + height, bgColor);

        // Border
        int borderColor = selected
            ? GuiColors.ACCENT_TAN
            : GuiColors.BORDER_LIGHT;
        graphics.fill(
            left,
            top + height - 1,
            left + width,
            top + height,
            borderColor
        ); // Bottom separator

        // === LEFT: Reward item icon ===
        int iconX = left + GuiLayoutConstants.MARGIN_S;
        int iconY = top + (height - 16) / 2;
        ItemStack reward = bounty.getReward();
        if (!reward.isEmpty()) {
            graphics.renderItem(reward, iconX, iconY);
            graphics.renderItemDecorations(font, reward, iconX, iconY);
        }

        // === CENTER: Bounty info ===
        int textX =
            left +
            GuiLayoutConstants.MARGIN_S +
            20 +
            GuiLayoutConstants.MARGIN_M;
        int textY = top + GuiLayoutConstants.MARGIN_S;

        // Line 1: Client
        String clientLabel = Component.translatable(
            "gui.tiedup.bounties.client",
            bounty.getClientName()
        ).getString();
        graphics.drawString(
            font,
            clientLabel,
            textX,
            textY,
            GuiColors.TEXT_GRAY
        );

        // Line 2: Target (highlighted)
        textY += GuiLayoutConstants.LINE_HEIGHT + 2;
        String targetLabel = Component.translatable(
            "gui.tiedup.bounties.target",
            bounty.getTargetName()
        ).getString();
        graphics.drawString(
            font,
            targetLabel,
            textX,
            textY,
            GuiColors.TEXT_WHITE
        );

        // Line 3: Reward description
        textY += GuiLayoutConstants.LINE_HEIGHT + 2;
        String rewardLabel = Component.translatable(
            "gui.tiedup.bounties.reward",
            bounty.getRewardDescription()
        ).getString();
        graphics.drawString(
            font,
            rewardLabel,
            textX,
            textY,
            GuiColors.ACCENT_TAN
        );

        // Line 4: Time remaining
        textY += GuiLayoutConstants.LINE_HEIGHT + 2;
        int[] time = bounty.getRemainingTime();
        String timeLabel = Component.translatable(
            "gui.tiedup.bounties.time",
            String.valueOf(time[0]),
            String.valueOf(time[1])
        ).getString();
        int timeColor = bounty.isExpired()
            ? GuiColors.ERROR
            : (time[0] == 0 && time[1] < 30
                  ? GuiColors.WARNING
                  : GuiColors.TEXT_GRAY);
        graphics.drawString(font, timeLabel, textX, textY, timeColor);
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        if (button == 0) {
            if (onSelect != null) {
                onSelect.accept(this);
            }
            return true;
        }
        return false;
    }

    @Override
    public List<? extends GuiEventListener> children() {
        return ImmutableList.of();
    }

    @Override
    public List<? extends NarratableEntry> narratables() {
        return ImmutableList.of();
    }
}
