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.config.JRConfigManager;
import net.cmr.jurassicrevived.screen.renderer.EnergyDisplayTooltipArea;
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 FossilCleanerScreen extends AbstractContainerScreen<FossilCleanerMenu> {
	private static final ResourceLocation GUI_TEXTURE =
		Constants.rl("textures/gui/fossil_cleaner/fossil_cleaner_gui.png");
	private static final ResourceLocation BUBBLES_TEXTURE =
		Constants.rl("textures/gui/generic/bubbles.png");
	private static final ResourceLocation WHITE_BUBBLES_TEXTURE =
		Constants.rl("textures/gui/generic/white_bubbles.png");
	private static final ResourceLocation POWER_BAR_TEXTURE =
		Constants.rl("textures/gui/generic/power_bar.png");
	private static final ResourceLocation SKULL_TEXTURE =
		Constants.rl("textures/gui/generic/skull.png");
	private FluidTankRenderer fluidRenderer;
	private EnergyDisplayTooltipArea energyInfoArea;

	public FossilCleanerScreen(FossilCleanerMenu menu, Inventory playerInventory, Component title) {
		super(menu, playerInventory, title);
	}

	@Override
	protected void init() {
		super.init();

		this.inventoryLabelY = 10000;
		this.titleLabelY = 10000;

		assignFluidRenderer();
		assignEnergyInfoArea();
	}

	private void renderEnergyAreaTooltip(GuiGraphics guiGraphics, int pMouseX, int pMouseY, int x, int y) {
		if(isMouseAboveArea(pMouseX, pMouseY, x, y, 160, 11, 8, 64) && JRConfigManager.get().requirePower) {
			guiGraphics.renderTooltip(this.font, energyInfoArea.getTooltips(),
				Optional.empty(), pMouseX - x, pMouseY - y);
		}
	}

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

	private void assignFluidRenderer() {
		fluidRenderer = new FluidTankRenderer(16000, true, 16, 50);
	}

	private void renderFluidTooltipArea(GuiGraphics guiGraphics, int MouseX, int MouseY, int x, int y,
		FluidStack stack, int offsetX, int offsetY, FluidTankRenderer renderer) {
		if(isMouseAboveArea(MouseX, MouseY, x, y, offsetX, offsetY, renderer)) {
			guiGraphics.renderTooltip(this.font, renderer.getTooltip(stack, TooltipFlag.Default.NORMAL),
				Optional.empty(), MouseX - x, MouseY - 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.blockEntity.getFluid(), 7, 8, fluidRenderer);
		if (JRConfigManager.get().requirePower) {
			renderEnergyAreaTooltip(guiGraphics, pMouseX, pMouseY, x, y);
		}
	}

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

		guiGraphics.blit(GUI_TEXTURE, x, y, 0, 0, this.imageWidth, this.imageHeight, 176, 166);
		guiGraphics.blit(BUBBLES_TEXTURE,  x + 73, y + 37, 0, 0, 29, 12, 29, 12);
		guiGraphics.blit(SKULL_TEXTURE,  x + 57, y + 35, 0, 0, 16, 16, 16, 16);
		RenderProgressArrow(guiGraphics, x, y);

		if (JRConfigManager.get().requirePower) {
			guiGraphics.blit(POWER_BAR_TEXTURE, x+159, y+10, 0, 0, 10, 66, 10, 66);
		}
	}

	private void RenderProgressArrow(GuiGraphics guiGraphics, int x, int y) {
		if(menu.isCrafting()) {
			guiGraphics.blit(WHITE_BUBBLES_TEXTURE, x+73, y + 37, 0, 0, menu.getScaledArrowProgress(), 12, 29, 12);
		}
	}

	@Override
	public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
		//? if >1.20.1 {
		/*this.renderBackground(guiGraphics, mouseX, mouseY, partialTick);
		 *///?} else {
		renderBackground(guiGraphics);
		//?}
		super.render(guiGraphics, mouseX, mouseY, partialTick);
		int x = (this.width - this.imageWidth) / 2;
		int y = (this.height - this.imageHeight) /2;
		if (MouseUtil.isMouseOver(mouseX, mouseY, x + 7, y + 8, 16, 50)) {
			renderHoverHighlight(guiGraphics, x + 7, y + 8, 16, 50);
		}
		renderTooltip(guiGraphics, mouseX, mouseY);
		fluidRenderer.render(guiGraphics, x + 7, y + 8, menu.blockEntity.getFluid());
		if (JRConfigManager.get().requirePower) {
			energyInfoArea.render(guiGraphics);
		}
	}

	private static void renderHoverHighlight(GuiGraphics g, int x, int y, int w, int h) {
		g.pose().pushPose();
		g.pose().translate(0, 0, 200);
		g.fillGradient(x, y, x + w, y + h, 0x80FFFFFF, 0x80FFFFFF);
		g.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());
	}

	public static 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);
	}
}