package noppes.npcs.client.gui.script;

import java.util.ArrayList;
import java.util.List;

import noppes.npcs.shared.client.gui.components.GuiBasic;
import noppes.npcs.shared.client.gui.components.GuiCustomScroll;
import noppes.npcs.shared.client.gui.components.GuiButtonNop;
import noppes.npcs.shared.client.gui.components.GuiLabel;
import noppes.npcs.controllers.ScriptContainer;

public class GuiScriptList extends GuiBasic {
	private GuiCustomScroll scroll1;
	private GuiCustomScroll scroll2;
	private ScriptContainer container;
	private List<String> scripts;
	
	public GuiScriptList(List<String> scripts, ScriptContainer container){
		this.container = container;
		setBackground("menubg.png");
		imageWidth = 346;
		imageHeight = 216;
		if(scripts == null)
			scripts = new ArrayList<String>();
		this.scripts = scripts;
	}

	@Override
	public void init(){
		super.init();
        if(scroll1 == null){
        	scroll1 = new GuiCustomScroll(this,0);
        	scroll1.setSize(140, 180);
        }
        scroll1.guiLeft = guiLeft + 4;
        scroll1.guiTop = guiTop + 14;
        this.addScroll(scroll1);
        addLabel(new GuiLabel(1, "script.availableScripts", guiLeft + 4, guiTop + 4));
        
        if(scroll2 == null){
        	scroll2 = new GuiCustomScroll(this,1);
        	scroll2.setSize(140, 180);
        }
        scroll2.guiLeft = guiLeft + 200;
        scroll2.guiTop = guiTop + 14;
        this.addScroll(scroll2);
        addLabel(new GuiLabel(2, "script.loadedScripts", guiLeft + 200, guiTop + 4));
        List<String> temp = new ArrayList<String>(scripts);
        temp.removeAll(container.scripts);
        scroll1.setList(temp);
        scroll2.setList(container.scripts);

    	addButton(new GuiButtonNop(this, 1, guiLeft + 145, guiTop + 40, 55, 20, ">"));
    	addButton(new GuiButtonNop(this, 2, guiLeft + 145, guiTop + 62, 55, 20, "<"));

    	addButton(new GuiButtonNop(this, 3, guiLeft + 145, guiTop + 90, 55, 20, ">>"));
    	addButton(new GuiButtonNop(this, 4, guiLeft + 145, guiTop + 112, 55, 20, "<<"));
        
    	addButton(new GuiButtonNop(this, 66, guiLeft + 260, guiTop + 194, 60, 20, "gui.done"));
	}

	@Override
	public void buttonEvent(GuiButtonNop guibutton){
    	GuiButtonNop button = (GuiButtonNop) guibutton;
		if(button.id == 1){
			if(scroll1.hasSelected()){
				container.scripts.add(scroll1.getSelected());
				scroll1.clearSelection();
				scroll2.clearSelection();
				init();
			}				
		}
		if(button.id == 2){
			if(scroll2.hasSelected()){
				container.scripts.remove(scroll2.getSelected());
				scroll2.clearSelection();
				init();
			}				
		}
		if(button.id == 3){
			container.scripts.clear();
			for(String script : scripts){
				container.scripts.add(script);
			}
			scroll1.clearSelection();
			scroll2.clearSelection();
			init();
		}
		if(button.id == 4){
			container.scripts.clear();
			scroll1.clearSelection();
			scroll2.clearSelection();
			init();
		}
		if(button.id == 66){
        	close();
        }
    }

	@Override
	public void save() {
		
	}

}
