package info.u_team.u_team_core.gui.elements;

import java.util.*;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

import info.u_team.u_team_core.UCoreMain;
import info.u_team.u_team_core.util.GuiUtil;
import net.minecraft.client.*;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.*;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.client.config.GuiUtils;

@OnlyIn(Dist.CLIENT)
public class EnergyStorageWidget extends Widget {
	
	public static final ResourceLocation ENERGY_TEXTURE = new ResourceLocation(UCoreMain.MODID, "textures/gui/energy.png");
	
	private final LongSupplier capacity;
	private final LongSupplier storage;
	
	public EnergyStorageWidget(int x, int y, int height, Supplier<IEnergyStorage> energyStorage) {
		this(x, y, height, () -> energyStorage.get().getMaxEnergyStored(), () -> energyStorage.get().getEnergyStored());
	}
	
	public EnergyStorageWidget(int x, int y, int height, LongSupplier capacity, LongSupplier storage) {
		super(x, y, 14, height < 3 ? 3 : height, "");
		this.capacity = capacity;
		this.storage = storage;
	}
	
	@Override
	public void renderButton(int mouseX, int mouseY, float partialTicks) {
		final Minecraft minecraft = Minecraft.getInstance();
		minecraft.getTextureManager().bindTexture(ENERGY_TEXTURE);
		
		double ratio = (double) storage.getAsLong() / capacity.getAsLong();
		if (ratio > 1) {
			ratio = 1;
		}
		
		final int storageOffset = (int) ((1 - ratio) * (height - 2));
		
		for (int yComponent = 1; yComponent < height - 1; yComponent += 2) {
			blit(x + 1, y + yComponent, 0, 0, 12, 2, 16, 16); // Background with side border
		}
		
		for (int yComponent = 1 + storageOffset; yComponent < height - 1; yComponent++) {
			if (yComponent % 2 == 0) {
				blit(x + 1, y + yComponent, 0, 3, 12, 1, 16, 16); // Fuel
			} else {
				blit(x + 1, y + yComponent, 0, 2, 12, 1, 16, 16); // Fuel
			}
		}
		
		GuiUtil.drawContainerBorder(x, y, width, height);
	}
	
	@Override
	public void renderToolTip(int mouseX, int mouseY) {
		if (isHovered) {
			final Minecraft minecraft = Minecraft.getInstance();
			final MainWindow mainWindow = minecraft.mainWindow;
			final List<String> list = new ArrayList<>();
			list.add(storage.getAsLong() + " / " + capacity.getAsLong() + " FE");
			GuiUtils.drawHoveringText(list, mouseX, mouseY, mainWindow.getWidth(), mainWindow.getHeight(), 300, minecraft.fontRenderer);
		}
	}
	
	@Override
	public void playDownSound(SoundHandler handler) {
		// Don't play click sound
	}
}

@@ -90,56 +90,9 @@
 ion.
-LongSupplier;%0Aimport java.util.function.Supplier
+*
 ;%0A%0Ai
@@ -387,24 +387,76 @@
 stmarker.*;%0A
+import net.minecraftforge.common.util.LazyOptional;%0A
 import net.m
@@ -781,16 +781,16 @@
 pacity;%0A
-
 %09private
@@ -820,16 +820,282 @@
 rage;%0A%09%0A
+%09public EnergyStorageWidget(int x, int y, int height, LazyOptional%3CIEnergyStorage%3E energyStorage) %7B%0A%09%09this(x, y, height, () -%3E energyStorage.map(IEnergyStorage::getMaxEnergyStored).orElse(0), () -%3E energyStorage.map(IEnergyStorage::getEnergyStored).orElse(0));%0A%09%7D%0A%09%0A
 %09public 

Add LazyOptional constructor to energy storage widget