package openmods.gui.component;

import java.util.List;
import java.util.Map;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import openmods.gui.misc.BoxRenderer;
import openmods.gui.misc.ISlotBackgroundRenderer;

import org.lwjgl.opengl.GL11;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;

public class GuiComponentPanel extends GuiComponentResizableComposite {

	private static final BoxRenderer BOX_RENDERER = new BoxRenderer(0, 5);

	public static final ISlotBackgroundRenderer normalSlot = new ISlotBackgroundRenderer() {
		@Override
		public void render(Gui gui, Slot slot) {
			gui.drawTexturedModalRect(slot.xDisplayPosition - 1, slot.yDisplayPosition - 1, 0, 20, 18, 18);
		}
	};

	public static final ISlotBackgroundRenderer bigSlot = new ISlotBackgroundRenderer() {
		@Override
		public void render(Gui gui, Slot slot) {
			gui.drawTexturedModalRect(slot.xDisplayPosition - 5, slot.yDisplayPosition - 5, 29, 20, 26, 26);
		}
	};

	public static final ISlotBackgroundRenderer noRenderSlot = new ISlotBackgroundRenderer() {
		@Override
		public void render(Gui gui, Slot slot) {}
	};

	private final Map<Integer, ISlotBackgroundRenderer> slotRenderers = Maps.newHashMap();

	private final Container container;

	public GuiComponentPanel(int x, int y, int width, int height, Container container) {
		super(x, y, width, height);
		this.container = container;
	}

	public void setSlotRenderer(int slotId, ISlotBackgroundRenderer renderer) {
		slotRenderers.put(slotId, renderer);
	}

	@Override
	protected void renderComponentBackground(Minecraft minecraft, int x, int y, int mouseX, int mouseY) {
		GL11.glColor3f(1, 1, 1);
		bindComponentsSheet();
		BOX_RENDERER.render(this, this.x + x, this.y + y, width, height, 0xFFFFFFFF);
	}

	@Override
	@SuppressWarnings("unchecked")
	protected void renderComponentForeground(Minecraft minecraft, int x, int y, int mouseX, int mouseY) {
		GL11.glColor3f(1, 1, 1);
		bindComponentsSheet();

		if (container != null) {
			for (Slot slot : (List<Slot>)container.inventorySlots) {
				Objects.firstNonNull(slotRenderers.get(slot.slotNumber), normalSlot).render(this, slot);
			}
		}
	}

}

@@ -1229,16 +1229,409 @@
 %7B%7D%0A%09%7D;%0A%0A
+%09public static ISlotBackgroundRenderer coloredSlot(final int color) %7B%0A%09%09return new ISlotBackgroundRenderer() %7B%0A%09%09%09@Override%0A%09%09%09public void render(Gui gui, Slot slot) %7B%0A%09%09%09%09GL11.glColor3ub((byte)(color %3E%3E 16), (byte)(color %3E%3E 8), (byte)(color %3E%3E 0));%0A%09%09%09%09gui.drawTexturedModalRect(slot.xDisplayPosition - 1, slot.yDisplayPosition - 1, 0, 20, 18, 18);%0A%09%09%09%09GL11.glColor3f(1, 1, 1);%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A%0A
 %09private

Add colored slots to GUI panel