package link.infra.simpleprocessors.gui;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import link.infra.simpleprocessors.SimpleProcessors;
import link.infra.simpleprocessors.blocks.programmer.ProgrammerContainer;
import link.infra.simpleprocessors.blocks.programmer.ProgrammerTileEntity;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;

public class BrowserTab extends ProgrammerTab {

	private static final ResourceLocation background = new ResourceLocation(SimpleProcessors.MODID, "textures/gui/browsertab.png");
	private HashMap<String, Integer> fileList;
	private BrowserScrollingList slotList;
	
	public BrowserTab() {
		super(background, "browser");
	}
	
	@Override
	public void initGui(ProgrammerGui programmerGui, ProgrammerTileEntity te, ProgrammerContainer container, FontRenderer fontRenderer) {
		//fileList = te.getFileList();
		fileList = new HashMap<String, Integer>();
		fileList.put("hello", 3);
		fileList.put("hi", 10);
		slotList = new BrowserScrollingList(programmerGui.mc, fileList, programmerGui.getGuiLeft() + 5, programmerGui.getGuiTop() + 33, programmerGui.width, programmerGui.height);
		SimpleProcessors.logger.info("Gui init");
		SimpleProcessors.logger.info(te.getFileList().size());
	}
	
	@Override
	public void drawTab(ProgrammerGui gui, FontRenderer fontRenderer, ProgrammerContainer container, float partialTicks, int mouseX, int mouseY) {
		slotList.drawScreen(mouseX, mouseY, partialTicks);
	}
	
	@Override
	public void actionPerformed(GuiButton button) {
		//slotList.actionPerformed(button);
		if (button.id == 1) {
			String name = "unnamed";
			int i = 1;
			while (fileList.containsKey(name)) {
				name = "unnamed_" + i;
				i++;
			}
			fileList.put(name, 0);
			slotList.refresh(fileList);
		}
	}
	
	@Override
	public void handleMouseInput(ProgrammerGui programmerGui, int x, int y) throws IOException {
		slotList.handleMouseInput(x, y);
	}
	
	@Override
	public void initButtons(List<GuiButton> buttonList, ProgrammerGui gui) {
		int guiLeft = gui.getGuiLeft();
		int guiTop = gui.getGuiTop();
		buttonList.add(new GuiButton(0, guiLeft + 115, guiTop + 33, 56, 20, I18n.format("button." + SimpleProcessors.MODID + ".programmer.tab.browser.open.name")));
		buttonList.add(new GuiButton(1, guiLeft + 115, guiTop + 55, 56, 20, I18n.format("button." + SimpleProcessors.MODID + ".programmer.tab.browser.new.name")));
	}
	
}

@@ -755,18 +755,49 @@
 otList;%0A
+%09private GuiButton openButton;%0A
 %09%0A
-
 %09public 
@@ -1070,24 +1070,26 @@
 teger%3E();%0A%09%09
+//
 fileList.put
@@ -1100,24 +1100,26 @@
 llo%22, 3);%0A%09%09
+//
 fileList.put
@@ -1401,24 +1401,92 @@
 ().size());%0A
+%09%09%0A%09%09if (fileList.size() == 0) %7B%0A%09%09%09openButton.enabled = false;%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09@Overr
@@ -1969,34 +1969,183 @@
 %0A%09%09%09
-slotList.refresh(fileList)
+refresh();%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void refresh() %7B%0A%09%09slotList.refresh(fileList);%0A%09%09if (fileList.size() == 0) %7B%0A%09%09%09openButton.enabled = false;%0A%09%09%7D else %7B%0A%09%09%09openButton.enabled = true
 ;%0A%09%09
@@ -2445,39 +2445,37 @@
 GuiTop();%0A%09%09
-buttonList.add(
+openButton = 
 new GuiButto
@@ -2597,24 +2597,53 @@
 open.name%22))
+;%0A%09%09buttonList.add(openButton
 );%0A%09%09buttonL

Disable open button when there are no files