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.packet.CPacketAction;

public class GuiClass extends GuiList<GuiClass.ClassEntry> implements ButtonCallback
{
	protected class ClassEntry implements ListEntry
	{
		private final Enum clazz;
		
	    protected ClassEntry(Enum clazz)
	    {
	    	this.clazz = clazz;
	    }

	    public void draw(int x, int y, int width, int height, int mouseX, int mouseY, boolean hovered)
	    {
	    	if(GuiClass.this.gm.player != null && this.clazz == GuiClass.this.gm.player.getNpcClass())
	    		Drawing.drawRect(x, y, 1, 44, 0xffaf0000);
            Drawing.drawText(this.clazz.toString().isEmpty() ? "<Keine>" : this.clazz.toString(), x + 3, y, 0xffffffff);
	    }
	    
	    public void select(boolean dclick, int mx, int my)
	    {
	    	if((GuiClass.this.selectButton.enabled = GuiClass.this.gm.player == null || this.clazz != GuiClass.this.gm.player.getNpcClass()) && dclick)
	    		GuiClass.this.use(GuiClass.this.selectButton, PressType.PRIMARY);
	    }
	}

	public static final GuiClass INSTANCE = new GuiClass();
	
	private ActButton selectButton;
	
    private GuiClass() {
    }
    
    public void init(int width, int height)
    {
    	super.init(width, height);
        this.setDimensions(width, height, 32, height - 32);
        this.elements.clear();
        if(this.gm.player != null && this.gm.player.getSpecies().classEnum != null)
	        for(Enum clazz : this.gm.player.getSpecies().classEnum.getEnumConstants()) {
	        	this.elements.add(new ClassEntry(clazz));
	        }
        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, "Klasse ändern"));
    }
        
	public String getTitle() {
		return "Klasse wählen";
	}
    
    public int getSlotHeight()
    {
        return 44 + 4;
    }

	public void use(ActButton elem, PressType action) {
		ClassEntry entry = this.getSelected();
		if(entry != null && GuiClass.this.gm.player != null) {
			GuiClass.this.gm.player.client.addToSendQueue(new CPacketAction(CPacketAction.Action.SET_CLASS, entry.clazz.ordinal()));
			this.gm.show(GuiChar.INSTANCE);
		}
	}
}
