package com.mcjty.rftools.blocks.screens;

import com.mcjty.container.GenericGuiContainer;
import com.mcjty.gui.Window;
import com.mcjty.gui.events.ButtonEvent;
import com.mcjty.gui.layout.PositionalLayout;
import com.mcjty.gui.widgets.Button;
import com.mcjty.gui.widgets.Panel;
import com.mcjty.gui.widgets.Widget;
import com.mcjty.rftools.RFTools;
import com.mcjty.rftools.blocks.screens.modulesclient.ClientScreenModule;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;

import java.awt.*;
import java.util.Map;

public class GuiScreen  extends GenericGuiContainer<SimpleScreenTileEntity> {
    public static final int SCREEN_WIDTH = 256;
    public static final int SCREEN_HEIGHT = 224;

    private static final ResourceLocation iconLocation = new ResourceLocation(RFTools.MODID, "textures/gui/screen.png");

    private Panel toplevel;
    private Button buttons[] = new Button[7];
    private Panel modulePanels[] = new Panel[7];
    private ClientScreenModule[] clientScreenModules = new ClientScreenModule[7];

    private int selected = -1;

    public GuiScreen(SimpleScreenTileEntity screenTileEntity, ScreenContainer container) {
        super(screenTileEntity, container);

        xSize = SCREEN_WIDTH;
        ySize = SCREEN_HEIGHT;
    }

    @Override
    public void initGui() {
        super.initGui();

        toplevel = new Panel(mc, this).setBackground(iconLocation).setLayout(new PositionalLayout());

        for (int i = 0 ; i < 7 ; i++) {
            buttons[i] = new Button(mc, this).setLayoutHint(new PositionalLayout.PositionalHint(30, 7 + i*18 + 1, 55, 16)).setEnabled(false).setColor(0xff000000);
            final int finalI = i;
            buttons[i].addButtonEvent(new ButtonEvent() {
                @Override
                public void buttonClicked(Widget parent) {
                    selectPanel(finalI);
                }
            });
            toplevel.addChild(buttons[i]);
            modulePanels[i] = null;
            clientScreenModules[i] = null;
        }

        toplevel.setBounds(new Rectangle(guiLeft, guiTop, xSize, ySize));

        window = new Window(this, toplevel);

        selected = -1;
    }

    private void selectPanel(int i) {
        selected = i;
    }

    private void refreshButtons() {
        for (int i = 0 ; i < 7 ; i++) {
            final ItemStack slot = tileEntity.getStackInSlot(i);
            if (slot != null && slot.getItem() != null && slot.getItem() instanceof ModuleProvider) {
                buttons[i].setEnabled(true);
                ModuleProvider moduleProvider = (ModuleProvider) slot.getItem();
                Class<? extends ClientScreenModule> clientScreenModuleClass = moduleProvider.getClientScreenModule();
                if (!clientScreenModuleClass.isInstance(clientScreenModules[i])) {
                    toplevel.removeChild(modulePanels[i]);
                    try {
                        ClientScreenModule clientScreenModule = clientScreenModuleClass.newInstance();
                        clientScreenModules[i] = clientScreenModule;
                    } catch (InstantiationException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }

                    NBTTagCompound tagCompound = slot.getTagCompound();
                    if (tagCompound == null) {
                        tagCompound = new NBTTagCompound();
                    }

                    final NBTTagCompound finalTagCompound = tagCompound;
                    final int finalI = i;
                    modulePanels[i] = clientScreenModules[i].createGui(mc, this, tagCompound, new ModuleGuiChanged() {
                        @Override
                        public void updateData() {
                            slot.setTagCompound(finalTagCompound);
                            tileEntity.setInventorySlotContents(finalI, slot);
                        }
                    });
                    modulePanels[i].setLayoutHint(new PositionalLayout.PositionalHint(90, 7, 140, 130));
                    toplevel.addChild(modulePanels[i]);
                }

                buttons[i].setText(moduleProvider.getName());
            } else {
                buttons[i].setEnabled(false);
                buttons[i].setText("");
                clientScreenModules[i] = null;
                toplevel.removeChild(modulePanels[i]);
                modulePanels[i] = null;
                if (selected == i) {
                    selected = -1;
                }
            }
            if (modulePanels[i] != null) {
                modulePanels[i].setVisible(selected == i);
                buttons[i].setColor(selected == i ? 0xffffffff : 0xff000000);
            }
        }
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float v, int i, int i2) {
//        sendServerCommand(SimpleScreenTileEntity.CMD_SETTINGS,
//                new Argument("on", Integer.parseInt(onEnergy.getText())),
//                new Argument("off", Integer.parseInt(offEnergy.getText())));

        refreshButtons();
        window.draw();
    }
}

@@ -273,16 +273,59 @@
 .Panel;%0A
+import com.mcjty.gui.widgets.ToggleButton;%0A
 import c
@@ -353,16 +353,16 @@
 Widget;%0A
-
 import c
@@ -606,30 +606,8 @@
 t.*;
-%0Aimport java.util.Map;
 %0A%0Apu
@@ -942,16 +942,22 @@
 private 
+Toggle
 Button b
@@ -967,24 +967,30 @@
 ons%5B%5D = new 
+Toggle
 Button%5B7%5D;%0A 
@@ -1592,24 +1592,30 @@
 ns%5Bi%5D = new 
+Toggle
 Button(mc, t
@@ -1717,29 +1717,8 @@
 lse)
-.setColor(0xff000000)
 ;%0A  
@@ -2301,21 +2301,117 @@
     
-selected = i;
+if (buttons%5Bi%5D.isPressed()) %7B%0A            selected = i;%0A        %7D else %7B%0A            selected = -1;%0A        %7D
 %0A   
@@ -4936,13 +4936,15 @@
 .set
-Color
+Pressed
 (sel
@@ -4957,34 +4957,8 @@
 == i
- ? 0xffffffff : 0xff000000
 );%0A 

Use a togglebutton instead of a button for the gui of the screen system