package net.cmr.jurassicrevived.screen.custom;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.architectury.fluid.FluidStack;
import net.cmr.jurassicrevived.Constants;
import net.cmr.jurassicrevived.screen.renderer.FluidTankRenderer;
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 net.minecraft.world.item.TooltipFlag;

import java.util.Optional;

public class TankScreen extends AbstractContainerScreen<TankMenu> {
	private static final ResourceLocation GUI_TEXTURE =
		Constants.rl("textures/gui/tank/tank_gui.png");
	private FluidTankRenderer fluidRenderer;

	public TankScreen(TankMenu 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;

		assignFluidRenderer();
	}

	private void assignFluidRenderer() {
		fluidRenderer = new FluidTankRenderer(64000, true, 16, 64);
	}

	private void renderFluidTooltipArea(GuiGraphics guiGraphics, int pMouseX, int pMouseY, int x, int y,
		FluidStack stack, int offsetX, int offsetY, FluidTankRenderer renderer) {
		if(isMouseAboveArea(pMouseX, pMouseY, x, y, offsetX, offsetY, renderer)) {
			guiGraphics.renderTooltip(this.font, renderer.getTooltip(stack, TooltipFlag.Default.NORMAL),
				Optional.empty(), pMouseX - x, pMouseY - y);
		}
	}

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

		renderFluidTooltipArea(guiGraphics, pMouseX, pMouseY, x, y, menu.getFluid(), 80, 8, fluidRenderer);
	}

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

		fluidRenderer.render(guiGraphics, x + 80, y + 8, menu.getFluid());
	}

	@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 = (width - imageWidth) / 2;
		int y = (height - imageHeight) / 2;
		if (MouseUtil.isMouseOver(mouseX, mouseY, x + 80, y + 8, fluidRenderer.getWidth(), fluidRenderer.getHeight())) {
			renderHoverHighlight(guiGraphics, x + 80, y + 8, fluidRenderer.getWidth(), fluidRenderer.getHeight());
		}

		renderTooltip(guiGraphics, mouseX, mouseY);
	}

	private static void renderHoverHighlight(GuiGraphics guiGraphics, int x, int y, int width, int height) {
		guiGraphics.pose().pushPose();
		guiGraphics.pose().translate(0, 0, 200);
		guiGraphics.fillGradient(x, y, x + width, y + height, 0x80FFFFFF, 0x80FFFFFF);
		guiGraphics.pose().popPose();
	}

	public static boolean isMouseAboveArea(int pMouseX, int pMouseY, int x, int y, int offsetX, int offsetY, FluidTankRenderer renderer) {
		return MouseUtil.isMouseOver(pMouseX, pMouseY, x + offsetX, y + offsetY, renderer.getWidth(), renderer.getHeight());
	}
}