package net.cmr.jurassicrevived.screen.custom;

import com.mojang.blaze3d.systems.RenderSystem;
import net.cmr.jurassicrevived.Constants;
import net.cmr.jurassicrevived.screen.renderer.EnergyDisplayTooltipAreaDouble;
import net.cmr.jurassicrevived.util.MouseUtil;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

import java.util.Optional;

public class PowerCellScreen extends AbstractContainerScreen<PowerCellMenu> {
    private static final ResourceLocation GUI_TEXTURE =
            Constants.rl("textures/gui/tank/tank_gui.png");
    private EnergyDisplayTooltipAreaDouble energyInfoArea;

    public PowerCellScreen(PowerCellMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
        super(pMenu, pPlayerInventory, pTitle);
    }

    @Override
    protected void init() {
        super.init();
        // Gets rid of title and inventory title
        this.inventoryLabelY = 10000;
        this.titleLabelY = 10000;

        assignEnergyInfoArea();
    }

    private void renderEnergyAreaTooltip(GuiGraphics guiGraphics, int pMouseX, int pMouseY, int x, int y) {
        if(isMouseAboveArea(pMouseX, pMouseY, x, y, 80, 8, 16, 64)) {
            guiGraphics.renderTooltip(this.font, energyInfoArea.getTooltips(),
                    Optional.empty(), pMouseX - x, pMouseY - y);
        }
    }

    private void assignEnergyInfoArea() {
        energyInfoArea = new EnergyDisplayTooltipAreaDouble(((width - imageWidth) / 2) + 80,
                ((height - imageHeight) / 2) + 8, menu.blockEntity.getEnergyStorage(null));
    }

    @Override
    protected void renderLabels(GuiGraphics guiGraphics, int pMouseX, int pMouseY) {
        int x = (width - imageWidth) / 2;
        int y = (height - imageHeight) / 2;

        renderEnergyAreaTooltip(guiGraphics, pMouseX, pMouseY, x, y);
    }

    @Override
    protected void renderBg(GuiGraphics guiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, GUI_TEXTURE);
        int x = (width - imageWidth) / 2;
        int y = (height - imageHeight) / 2;

        guiGraphics.blit(GUI_TEXTURE, x, y, 0, 0, imageWidth, imageHeight);

        energyInfoArea.render(guiGraphics);
    }

    @Override
    public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
		//? if >1.20.1 {
		/*this.renderBackground(guiGraphics, mouseX, mouseY, delta);
		 *///?} else {
		renderBackground(guiGraphics);
		//?}
        super.render(guiGraphics, mouseX, mouseY, delta);
        int x = (this.width - this.imageWidth) / 2;
        int y = (this.height - this.imageHeight) /2;
        renderTooltip(guiGraphics, mouseX, mouseY);

        // Draw hover highlight ABOVE background + fluid, but BELOW tooltips
        if (MouseUtil.isMouseOver(mouseX, mouseY, x + 80, y + 8, 16, 64)) {
            renderHoverHighlight(guiGraphics, x + 80, y + 8, 16, 64);
        }
    }

    private static void renderHoverHighlight(GuiGraphics g, int x, int y, int w, int h) {
        // Push to a higher Z so it renders above the tank contents
        var pose = g.pose();
        pose.pushPose();
        pose.translate(0.0F, 0.0F, 200.0F); // large enough to be on top of background/fluid
        // Same color at top and bottom to avoid gradient banding; alpha ~0.5
        g.fillGradient(x, y, x + w, y + h, 0x80FFFFFF, 0x80FFFFFF);
        pose.popPose();
    }

    private boolean isMouseAboveArea(int pMouseX, int pMouseY, int x, int y, int offsetX, int offsetY, int width, int height) {
        return MouseUtil.isMouseOver(pMouseX, pMouseY, x + offsetX, y + offsetY, width, height);
    }
}