package net.halbear.aotg.custom.blocks.Forge;

import net.halbear.aotg.custom.GuiScreens.ForgeWorkbenchGUIScreen;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jspecify.annotations.NonNull;

import java.util.ArrayList;
import java.util.List;

public class ForgeItemsScrollList extends ObjectSelectionList<ForgeItemsScrollList.ItemEntry> {
    private final ForgeWorkbenchGUIScreen parent;
    public ForgeItemsScrollList(ForgeWorkbenchGUIScreen parent, Minecraft minecraft, int width, int height, int y, int itemHeight) {
        super(minecraft, width, height, y, itemHeight);
        this.parent = parent;
    }

    public void refreshItems(List<ItemStack> items) {
        this.clearEntries();
        for (ItemStack stack : items) {
            this.addEntry(new ItemEntry(this.parent, stack));
        }
    }

    @Override
    public int getRowWidth() {
        return 68 - this.scrollbarWidth();
    }
    @Override
    public void setSelected(ForgeItemsScrollList.ItemEntry entry) {
        super.setSelected(entry);

        if (entry != null) {
            this.parent.onItemClicked(entry.getItemStack());
            parent.setSelectedStack(entry.getItemStack());
        }
    }
    @Override
    public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) {
        if (parent.scrollList != null && parent.scrollList.mouseScrolled(mouseX, mouseY, scrollX, scrollY)) {
            return true;
        }
        return super.mouseScrolled(mouseX, mouseY, scrollX, scrollY);
    }
    @Override
    protected int scrollBarX() {
        return this.getX() + this.getRowWidth();
    }

    public static class ItemEntry extends ObjectSelectionList.Entry<ItemEntry> implements GuiEventListener {
        private final ForgeWorkbenchGUIScreen parent;
        private final ItemStack itemStack;

        public ItemEntry(ForgeWorkbenchGUIScreen parent, ItemStack stack) {
            this.parent = parent;
            this.itemStack = stack;
        }

        public ItemStack getItemStack(){return itemStack;}

        @Override
        public Component getNarration() {
            return this.itemStack.getDisplayName();
        }

        @Override
        public void extractContent(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean isHovered, float partialTick) {
            int left = this.getX();
            int top = this.getY();
            int rowWidth = this.getWidth();
            int rowHeight = this.getHeight();

            if (isHovered) {
                graphics.fill(left, top, left + rowWidth, top + rowHeight, 0x22FFFFFF);
            }
            List<ItemStack> recipeItems = new ArrayList<>();
            ForgedAlloyRecipe alloy_recipe = ForgedAlloyRecipe.RecipeMap.get(this.itemStack.getItem());
            ForgeItemRecipe item_recipe = ForgeItemRecipe.FORGE_ITEM_RECIPE_MAP.get(this.itemStack.getItem());
            if(alloy_recipe != null){
                recipeItems.add(new ItemStack(alloy_recipe.RequiredMetal1(), alloy_recipe.AlloyRatio() < 1 ? 1 : (int)alloy_recipe.AlloyRatio()));
                recipeItems.add(new ItemStack(alloy_recipe.RequiredMetal2(), alloy_recipe.AlloyRatio() < 1 ? 1/(int)alloy_recipe.AlloyRatio() : 1));
            }
            if(item_recipe != null){
                for(int i = 0; i < item_recipe.RequiredItems().length; i++){
                    recipeItems.add(new ItemStack(item_recipe.RequiredItems()[i], item_recipe.RequiredItemCounts()[i]));
                }
            }
            graphics.fakeItem(this.itemStack, left + 4, top);
            graphics.pose().pushMatrix();
            graphics.pose().translate(left + 28, top + 2);
            graphics.pose().scale(0.75f, 0.75f);
            for(int i = 0; i < recipeItems.size(); i++){
                graphics.fakeItem(recipeItems.get(i),  (i * 20),1);
                graphics.text(parent.getFont(), "" + recipeItems.get(i).getCount(), (i * 20) + 17 - parent.getFont().width("" + recipeItems.get(i).getCount()), 9, -1, true);
            }
            graphics.pose().popMatrix();

            if (isHovered && mouseX >= left && mouseX <= left + rowWidth && mouseY >= top && mouseY <= top + rowHeight) {
                this.parent.setHoveredStack(this.itemStack);
            }
        }

    }
}
