package net.halbear.aotg.custom.GuiScreens;

import com.mojang.blaze3d.platform.InputConstants;
import net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceBlockEntity;
import net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceGUI;
import net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceMultiBlock;
import net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceUpdateHandler;
import net.halbear.aotg.registries.ModBlocks;
import net.halbear.aotg.registries.ModScreens;
import net.halbear.aotg.utility.Networking.AlloyFurnaceGuiButtonHandler;
import net.minecraft.client.Minecraft;
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.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;

import static net.halbear.aotg.custom.blocks.AlloyFurnace.AlloyFurnaceBlockEntity.MAX_BELLOW;

public class AlloyFurnaceGUIScreen extends AbstractContainerScreen<AlloyFurnaceGUI> 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/alloy_furnace_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 AlloyFurnaceGUIScreen(AlloyFurnaceGUI 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);
    }

    @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);
        int bellowPush = this.menu.getSyncedVariable(3);
        int blowerSprite = (int)Math.clamp(3 * (float)bellowPush/(float)MAX_BELLOW,0,2);
        guiGraphics.blit(RenderPipelines.GUI_TEXTURED, BELLOW_SPRITE_SHEET, this.leftPos + 9, this.topPos + 58, 0, 20 * blowerSprite, 35, 20, 35, 60);
        int CookProgress = GetMoltenProgress(world,x,y,z,19,0,0,100f);
        if(CookProgress > 0) guiGraphics.blit(RenderPipelines.GUI_TEXTURED, MOLTEN_FILL_SPRITE, this.leftPos + 100, this.topPos + 36 + (19 - CookProgress), 0, 0, 17, CookProgress, 17, 19);
        int FuelProgress = GetMoltenProgress(world,x,y,z,21,0,1,this.menu.getSyncedVariable(4));
        if(FuelProgress > 0) guiGraphics.blit(RenderPipelines.GUI_TEXTURED, FLAME_SPRITE, this.leftPos + 47, this.topPos + 54 + (21 - FuelProgress), 0, 21 - FuelProgress, 14, FuelProgress, 14, 21);
    }

    private int GetMoltenProgress(Level world, int x, int y, int z, int FullHeight, int MinimumHeight, int ElementVariable, float MaxValue){
        BlockState state = world.getBlockState(new BlockPos(x,y,z));
        BlockPos position = new BlockPos(x,y,z);
        if(state.getBlock().getName().getString().equals(ModBlocks.ALLOY_FURNACE.get().getName().getString())) {
//            if(!state.getValue(AlloyFurnaceMultiBlock.PARENT_BLOCK)){
//                int ModelPart = state.getValue(AlloyFurnaceMultiBlock.LEVEL);
//                position = AlloyFurnaceUpdateHandler.GetParentPosFromModelPart(world,ModelPart,state,position);
//                if(position == null) return MinimumHeight;
//            }
            int cookProgress = this.menu.getSyncedVariable(ElementVariable);
            float Multipler = Math.min((float)cookProgress/MaxValue,1.0f);
            int TargetHeight = (int)Math.ceil((float)FullHeight*Multipler);
            return Math.clamp(TargetHeight,MinimumHeight,FullHeight);
        }
        return MinimumHeight;
    }

    @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);
    }

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

    @Override
    public void init() {
        super.init();
        Blower = new ImageButton( this.leftPos + 9, this.topPos + 58, 35, 20,
                new WidgetSprites(Identifier.parse("ageofthegods:textures/screens/bellow_button.png"), Identifier.parse("ageofthegods:textures/screens/bellow_button.png")), e -> {
            int x = AlloyFurnaceGUIScreen.this.x;
            int y = AlloyFurnaceGUIScreen.this.y;
            int z = AlloyFurnaceGUIScreen.this.z;
            ClientPacketDistributor.sendToServer(new AlloyFurnaceGuiButtonHandler(0, x, y, z));
            AlloyFurnaceGuiButtonHandler.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);

    }
}
