package client.gui.ingame;

import client.gui.Gui;
import client.gui.element.Label;
import client.gui.element.NavButton;
import client.gui.element.PressType;
import client.gui.element.FieldAction;
import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.Field;
import client.gui.element.FieldCallback;
import client.network.ClientPlayer;
import common.item.ItemStack;
import common.packet.CPacketMessage;
import common.util.Color;

public class GuiRename extends Gui implements FieldCallback, ButtonCallback {
	private final String itemName;
	private final String origName;
	private final int slot;
	private final Gui previous;
	
	private Field lines;
	private ActButton button;
	
	public void init(int width, int height) {
		this.add(new Label(0, -140, 300, 0, "Benenne " + this.itemName + " um"));
		this.lines = this.add(new Field(0, 0, 300, 0, 50, this, this.origName));
		this.add(new NavButton(0, 40, 149, 0, this.previous, "Abbrechen"));
		this.button = this.add(new ActButton(151, 40, 149, 0, this, "Umbenennen"));
		this.shift();
	}
	
	public String getTitle() {
		return "Gegenstand umbenennen";
	}
    
    public GuiRename(int slot, ItemStack stack, Gui prev) {
    	this.itemName = stack.getItem().getDisplay();
    	this.origName = stack.hasDisplayName() ? Color.stripCodes(stack.getDisplayName()) : "";
    	this.slot = slot;
    	this.previous = prev;
    }
    
	public void use(Field elem, FieldAction value) {
		if(value == FieldAction.SEND)
			this.use(this.button, PressType.PRIMARY);
	}

	public void use(ActButton elem, PressType action) {
		ClientPlayer nethandler = this.gm.getNetHandler();
        if(nethandler != null)
            nethandler.addToSendQueue(new CPacketMessage(CPacketMessage.Type.ITEM, this.lines.getText(), this.slot));
        this.gm.show(this.previous);
	}
}
