package net.geforcemods.securitycraft.screen.components;

import java.util.function.Consumer;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager.DestFactor;
import com.mojang.blaze3d.platform.GlStateManager.SourceFactor;
import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class TogglePictureButton extends ClickButton{

	private Block blockToRender;
	private Item itemToRender;
	private ResourceLocation textureLocation;
	private int[] u;
	private int[] v;
	private int currentIndex = 0;
	private final int toggleCount;

	public TogglePictureButton(int id, int xPos, int yPos, int width, int height, ItemStack stackToRender, int toggleCount) {
		this(id, xPos, yPos, width, height, stackToRender, toggleCount, null);
	}

	public TogglePictureButton(int id, int xPos, int yPos, int width, int height, ItemStack itemToRender, int toggleCount, Consumer<ClickButton> onClick) {
		super(id, xPos, yPos, width, height, "", onClick);

		this.toggleCount = toggleCount;

		if(!itemToRender.isEmpty() && itemToRender.getItem() instanceof BlockItem)
			blockToRender = Block.getBlockFromItem(itemToRender.getItem());
		else
			this.itemToRender = itemToRender.getItem();
	}

	public TogglePictureButton(int id, int xPos, int yPos, int width, int height, ResourceLocation texture, int[] textureX, int[] textureY, int toggleCount, Consumer<ClickButton> onClick)
	{
		super(id, xPos, yPos, width, height, "", onClick);

		if(textureX.length != toggleCount || textureY.length != toggleCount)
			throw new RuntimeException("TogglePictureButton was setup incorrectly. Array lengths must match toggleCount!");

		textureLocation = texture;
		u = textureX;
		v = textureY;
		this.toggleCount = toggleCount;
	}

	/**
	 * Draws this button to the screen.
	 */
	@Override
	public void render(MatrixStack matrix, int mouseX, int mouseY, float partialTicks)
	{
		if (visible)
		{
			Minecraft mc = Minecraft.getInstance();
			FontRenderer font = mc.fontRenderer;
			ItemRenderer itemRenderer = mc.getItemRenderer();
			int hoverState = !active ? 0 : !isHovered ? 1 : 2;

			mc.getTextureManager().bindTexture(WIDGETS_LOCATION);
			RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
			isHovered = mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height;
			RenderSystem.enableBlend();
			RenderSystem.blendFuncSeparate(770, 771, 1, 0);
			RenderSystem.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA);
			blit(matrix, x, y, 0, 46 + hoverState * 20, width / 2, height);
			blit(matrix, x + width / 2, y, 200 - width / 2, 46 + hoverState * 20, width / 2, height);

			if(blockToRender != null){
				RenderSystem.enableRescaleNormal(); //(this.width / 2) - 8
				itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(blockToRender), x + 2, y + 3);
				itemRenderer.renderItemOverlayIntoGUI(font, new ItemStack(blockToRender), x + 2, y + 3, "");
			}else if(itemToRender != null){
				RenderSystem.enableRescaleNormal();
				itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(itemToRender), x + 2, y + 2);
				itemRenderer.renderItemOverlayIntoGUI(font, new ItemStack(itemToRender), x + 2, y + 2, "");
				RenderSystem.disableLighting();
			}
			else if(textureLocation != null)
			{
				RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
				mc.getTextureManager().bindTexture(textureLocation);
				blit(matrix, x + 2, y + 2, u[currentIndex], v[currentIndex], 16, 16);
			}

			int color = 0xe0e0e0;

			if (!active)
				color = 0xa0a0a0;
			else if (isHovered)
				color = 0xffffa0;

			drawCenteredString(matrix, font, getMessage(), x + width / 2, y + (height - 8) / 2, color);
		}
	}

	@Override
	public void onClick(double mouseX, double mouseY)
	{
		setCurrentIndex(currentIndex + 1);
		super.onClick(mouseX, mouseY);
	}

	public int getCurrentIndex()
	{
		return currentIndex;
	}

	public void setCurrentIndex(int newIndex)
	{
		currentIndex = newIndex % toggleCount;
	}

	public Item getItemStack() {
		return (blockToRender != null ? blockToRender.asItem() : itemToRender);
	}

}

@@ -483,45 +483,8 @@
 er;%0A
-import net.minecraft.item.BlockItem;%0A
 impo
@@ -511,16 +511,16 @@
 m.Item;%0A
+
 import n
@@ -970,652 +970,8 @@
 t;%0A%0A
-%09public TogglePictureButton(int id, int xPos, int yPos, int width, int height, ItemStack stackToRender, int toggleCount) %7B%0A%09%09this(id, xPos, yPos, width, height, stackToRender, toggleCount, null);%0A%09%7D%0A%0A%09public TogglePictureButton(int id, int xPos, int yPos, int width, int height, ItemStack itemToRender, int toggleCount, Consumer%3CClickButton%3E onClick) %7B%0A%09%09super(id, xPos, yPos, width, height, %22%22, onClick);%0A%0A%09%09this.toggleCount = toggleCount;%0A%0A%09%09if(!itemToRender.isEmpty() && itemToRender.getItem() instanceof BlockItem)%0A%09%09%09blockToRender = Block.getBlockFromItem(itemToRender.getItem());%0A%09%09else%0A%09%09%09this.itemToRender = itemToRender.getItem();%0A%09%7D%0A%0A
 %09pub
@@ -1279,16 +1279,16 @@
 eCount)%0A
-
 %09%09%09throw
@@ -1337,16 +1337,17 @@
  was set
+ 
 up incor

remove unnecessary constructors