package net.halbear.aotg.custom.GuiScreens;

import com.mojang.blaze3d.platform.InputConstants;
import net.halbear.aotg.custom.blocks.Forge.ForgeItemRecipe;
import net.halbear.aotg.custom.blocks.Forge.ForgeItemsScrollList;
import net.halbear.aotg.custom.blocks.Forge.ForgeWorkbenchGUI;
import net.halbear.aotg.registries.ModBlocks;
import net.halbear.aotg.registries.ModItems;
import net.halbear.aotg.registries.ModScreens;
import net.halbear.aotg.utility.Networking.AlloyFurnaceGuiButtonHandler;
import net.halbear.aotg.utility.Networking.ForgeWorkbenchGuiButtonHandler;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;

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


public class ForgeWorkbenchGUIScreen extends AbstractContainerScreen<ForgeWorkbenchGUI> implements ModScreens.ScreenAccessor {
    private final Level world;
    private final int x, y, z;
    private final Player entity;
    private boolean menuStateUpdateActive = false;
    private ImageButton Blower;
    private static final Identifier BACKGROUND = Identifier.parse("ageofthegods:textures/screens/forge_gui.png");
    private static final Identifier BELLOW_SPRITE_SHEET = Identifier.parse("ageofthegods:textures/screens/bellow-sheet.png");
    private static final Identifier MOLTEN_FILL_SPRITE = Identifier.parse("ageofthegods:textures/screens/alloy_furnace_molten_element_gui.png");
    private static final Identifier FLAME_SPRITE = Identifier.parse("ageofthegods:textures/screens/alloy_furnace_flame_gui.png");
    public ForgeItemsScrollList scrollList;
    private ItemStack hoveredStack = ItemStack.EMPTY;
    private ItemStack selectedStack = ItemStack.EMPTY;

    public void setHoveredStack(ItemStack stack) {
        this.hoveredStack = stack;
    }
    public void setSelectedStack(ItemStack stack) {
        this.selectedStack = stack;
    }

    public void onItemClicked(ItemStack clickedStack) {
        if (this.minecraft.player != null) {
            this.minecraft.player.sendOverlayMessage(
                    Component.literal("Selected: ").append(clickedStack.getHoverName())
            );
        }
    }

    public ForgeWorkbenchGUIScreen(ForgeWorkbenchGUI container, Inventory inventory, Component text) {
        super(container, inventory, text, 176, 166);
        this.world = container.world;
        this.x = container.x;
        this.y = container.y;
        this.z = container.z;
        this.entity = container.entity;
    }

    @Override
    public void updateMenuState(int elementType, String name, Object elementState) {
        menuStateUpdateActive = false;
    }

    @Override
    public void extractRenderState(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, float partialTicks) {
        super.extractRenderState(guiGraphics, mouseX, mouseY, partialTicks);
        if(selectedStack != ItemStack.EMPTY){
            guiGraphics.fakeItem(selectedStack,this.leftPos + 115, this.topPos + 13);
        }
        ForgeItemRecipe.RecipeSet set = menu.GetRecipes();
        List<ItemStack> previewList = new ArrayList<>();
        set.AlloyRecipes().forEach(forgedAlloyRecipe -> {
            previewList.add(new ItemStack(forgedAlloyRecipe.OutcomeItem()));
        });
        set.ItemRecipes().forEach(forgedAlloyRecipe -> {
            previewList.add(new ItemStack(forgedAlloyRecipe.primaryOutcomeItem()));
        });
        this.scrollList.refreshItems(previewList);
    }

    @Override
    public void extractBackground(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, float partialTicks) {
        super.extractBackground(guiGraphics, mouseX, mouseY, partialTicks);
        guiGraphics.blit(RenderPipelines.GUI_TEXTURED, BACKGROUND, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight, this.imageWidth, this.imageHeight);

    }

    @Override
    public boolean keyPressed(KeyEvent event) {
        int key = InputConstants.getKey(event).getValue();
        if (key == 256) {
            this.minecraft.player.closeContainer();
            return true;
        }
        return super.keyPressed(event);
    }

    public void SetScrollListItems(List<ItemStack>itemStacks){
        List<ItemStack> previewList = new ArrayList<>();
        previewList.addAll(itemStacks);
        this.scrollList.refreshItems(previewList);
    }

    @Override
    protected void extractLabels(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY) {
        guiGraphics.text(this.font, Component.translatable("gui.ageofthegods.forge_workbench.title"), 114, 61, -12829636, false);
    }

    @Override
    public void init() {
        super.init();
        int listWidth = 66;
        int listHeight = 74;
        int topOffset = this.topPos;
        this.scrollList = new ForgeItemsScrollList(this, this.minecraft, listWidth, listHeight, topOffset, 18);

        this.scrollList.setPosition(        this.leftPos + 29, topOffset + 6);

        List<ItemStack> previewList = new ArrayList<>();
        previewList.add(new ItemStack(ModItems.XIPHOS_SWORD.asItem()));
        previewList.add(new ItemStack(ModItems.BRONZE_INGOT.asItem()));
        previewList.add(new ItemStack(ModItems.ORICHALCUM_INGOT.asItem()));
        previewList.add(new ItemStack(ModItems.ELECTRUM_INGOT.asItem()));
        previewList.add(new ItemStack(ModItems.HEPATIZON_INGOT.asItem()));

        this.scrollList.refreshItems(previewList);
        this.addRenderableWidget(this.scrollList);
        this.addWidget(this.scrollList);

        Blower = new ImageButton( this.leftPos + 100, this.topPos + 56, 47, 22,
                new WidgetSprites(Identifier.parse("ageofthegods:textures/screens/forge_button_clear.png"), Identifier.parse("ageofthegods:textures/screens/forge_button_hover.png")), e -> {
            int x = ForgeWorkbenchGUIScreen.this.x;
            int y = ForgeWorkbenchGUIScreen.this.y;
            int z = ForgeWorkbenchGUIScreen.this.z;
            ClientPacketDistributor.sendToServer(new ForgeWorkbenchGuiButtonHandler(0, x, y, z));
            ForgeWorkbenchGuiButtonHandler.handleButtonAction(entity, 0, x, y, z);
        }) {
            @Override
            public void extractContents(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, float partialTicks) {
                guiGraphics.blit(RenderPipelines.GUI_TEXTURED, sprites.get(isActive(), isHoveredOrFocused()), getX(), getY(), 0, 0, width, height, width, height);
            }
        };
        this.addRenderableWidget(Blower);
    }
}
