package net.cmr.jurassicrevived.screen.custom;

import com.mojang.blaze3d.systems.RenderSystem;
import net.cmr.jurassicrevived.Constants;
import net.cmr.jurassicrevived.config.JRConfigManager;
import net.cmr.jurassicrevived.screen.renderer.EnergyDisplayTooltipArea;
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.util.Mth;
import net.minecraft.world.entity.player.Inventory;

import java.util.Optional;

public class DNAAnalyzerScreen extends AbstractContainerScreen<DNAAnalyzerMenu> {
	private static final ResourceLocation GUI_TEXTURE =
		Constants.rl("textures/gui/dna_analyzer/dna_analyzer_gui.png");
	private static final ResourceLocation DNA_TEXTURE =
		Constants.rl("textures/gui/generic/syringe_dna.png");
	private static final ResourceLocation POWER_BAR_TEXTURE =
		Constants.rl("textures/gui/generic/power_bar.png");
	private static final ResourceLocation TEST_TUBE_TEXTURE =
		Constants.rl("textures/gui/generic/test_tube.png");
	private static final ResourceLocation AMBER_TEXTURE =
		Constants.rl("textures/gui/generic/syringe.png");
	private EnergyDisplayTooltipArea energyInfoArea;

	public DNAAnalyzerScreen(DNAAnalyzerMenu menu, Inventory playerInventory, Component title) {
		super(menu, playerInventory, title);
	}

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

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

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

		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(TEST_TUBE_TEXTURE, x + 57, y + 35, 0, 0, 16, 16, 16, 16);
		guiGraphics.blit(AMBER_TEXTURE,  x + 80, y + 7, 0, 0, 16, 16, 16, 16);


		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()) {
			int fullW = 6, fullH = 16;
			int visible = Mth.clamp(menu.getScaledArrowProgress(), 0, fullH);

			// Top-down drain: draw the remaining part starting below the drained portion
			int remaining = fullH - visible;
			int srcU = 0;
			int srcV = visible;

			int drawX = x + 85;
			int drawY = y + 38 + visible;

			if (remaining > 0) {
				guiGraphics.blit(DNA_TEXTURE, drawX, drawY, srcU, srcV, fullW, remaining, fullW, fullH);
			}
		}
	}

	@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);
		renderTooltip(guiGraphics, mouseX, mouseY);
		int x = (this.width - this.imageWidth) / 2;
		int y = (this.height - this.imageHeight) /2;

		RenderProgressArrow(guiGraphics, x, y);

		if (JRConfigManager.get().requirePower) {
			energyInfoArea.render(guiGraphics);
		}
	}

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