package client.gui.ingame;

import client.gui.Gui;
import client.gui.element.Label;
import client.gui.element.NavButton;
import client.gui.element.FieldAction;
import client.gui.element.Field;
import client.gui.element.FieldCallback;
import client.network.ClientPlayer;
import common.packet.CPacketSign;
import common.util.BlockPos;

public class GuiSign extends Gui implements FieldCallback {
	private final BlockPos position;
	private final Field[] lines;
	private final String[] tempLines;
	
	public void init(int width, int height) {
		this.add(new Label(0, -140, 300, 0, "Bearbeite Schild"));
		this.add(new Label(0, -80, 300, 0, String.format("%d, %d, %d", this.position.getX(), this.position.getY(), this.position.getZ())));
		this.add(new Label(0, -50, 300, 0, this.gm.world == null ? "<?>" : this.gm.world.dimension.getDisplay()));
		for(int z = 0; z < this.lines.length; z++) {
			this.lines[z] = this.add(new Field(0, 40 * z, 300, 0, 50, this, this.tempLines[z] == null ? "" : this.tempLines[z]));
		}
		this.add(new NavButton(0, 40 * (this.lines.length + 1), 300, 0, null, "Fertig"));
		this.shift();
	}
	
	public String getTitle() {
		return "Schild bearbeiten";
	}
    
    public void onGuiClosed() {
        ClientPlayer nethandler = this.gm.getNetHandler();
        if(nethandler != null) {
    		for(int z = 0; z < this.lines.length; z++) {
    			this.tempLines[z] = this.lines[z].getText();
    		}
            nethandler.addToSendQueue(new CPacketSign(this.position, this.tempLines));
        }
    }
    
    public GuiSign(BlockPos sign, String[] lines) {
    	this.position = sign;
    	this.lines = new Field[lines.length];
    	this.tempLines = new String[lines.length];
		System.arraycopy(lines, 0, this.tempLines, 0, lines.length);
    }
    
	public void use(Field elem, FieldAction value) {
		if(value == FieldAction.SEND)
			this.gm.show(null);
	}
}
