package client.gui.character;

import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.GuiList;
import client.gui.element.ListEntry;
import client.gui.element.NavButton;
import client.gui.element.PressType;
import client.renderer.Drawing;
import common.entity.npc.SpeciesInfo;
import common.init.EntityRegistry;
import common.init.SpeciesRegistry;
import common.packet.CPacketAction;

public class GuiSpecies extends GuiList<GuiSpecies.SpeciesEntry> implements ButtonCallback
{
	protected class SpeciesEntry implements ListEntry
	{
		private final SpeciesInfo species;
		
	    protected SpeciesEntry(SpeciesInfo species)
	    {
	    	this.species = species;
	    }

	    public void draw(int x, int y, int width, int height, int mouseX, int mouseY, boolean hovered)
	    {
	    	if(GuiSpecies.this.gm.player != null && this.species == GuiSpecies.this.gm.player.getSpecies())
	    		Drawing.drawRect(x, y, 1, 44, 0xffaf0000);
            Drawing.drawText(this.species.name, x + 3, y, 0xff000000 | this.species.color1 | this.species.color2);
            if(this.species.classEnum != null)
                Drawing.drawText(this.species.classEnum.getEnumConstants().length + " Klassen", x + 3, y + 18, 0xffc0c0c0);
	    }
	    
	    public void select(boolean dclick, int mx, int my)
	    {
	    	if((GuiSpecies.this.selectButton.enabled = GuiSpecies.this.gm.player == null || this.species != GuiSpecies.this.gm.player.getSpecies()) && dclick)
	    		GuiSpecies.this.use(GuiSpecies.this.selectButton, PressType.PRIMARY);
	    }
	}

	public static final GuiSpecies INSTANCE = new GuiSpecies();
	
	private ActButton selectButton;
	
    private GuiSpecies() {
    }
    
    public void init(int width, int height)
    {
    	super.init(width, height);
        this.setDimensions(width, height, 32, height - 32);
        this.elements.clear();
        for(SpeciesInfo species : SpeciesRegistry.SPECIMEN) {
        	this.elements.add(new SpeciesEntry(species));
        }
        this.add(new NavButton(width / 2 - 195, height - 30, 194, 0, GuiChar.INSTANCE, "Zurück"));
        this.selectButton = this.add(new ActButton(width / 2 + 1, height - 30, 194, 0, this, "Spezies ändern"));
    }
        
	public String getTitle() {
		return "Spezies wählen";
	}
    
    public int getSlotHeight()
    {
        return 44 + 4;
    }

	public void use(ActButton elem, PressType action) {
		SpeciesEntry entry = this.getSelected();
		if(entry != null && GuiSpecies.this.gm.player != null)
			GuiSpecies.this.gm.player.client.addToSendQueue(new CPacketAction(CPacketAction.Action.SET_SPECIES, EntityRegistry.getEntityID(entry.species.clazz)));
	}
}
