package net.minecraft.client.gui;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.resources.I18n;
import net.minecraft.world.storage.ISaveFormat;
import net.minecraft.world.storage.WorldInfo;
import org.lwjgl.input.Keyboard;

@SideOnly(Side.CLIENT)
public class GuiRenameWorld extends GuiScreen
{
	private GuiScreen field_146585_a;
	private GuiTextField field_146583_f;
	private final String field_146584_g;
	private static final String __OBFID = "CL_00000709";

	public GuiRenameWorld(GuiScreen par1GuiScreen, String par2Str)
	{
		this.field_146585_a = par1GuiScreen;
		this.field_146584_g = par2Str;
	}

	public void updateScreen()
	{
		this.field_146583_f.updateCursorCounter();
	}

	public void initGui()
	{
		Keyboard.enableRepeatEvents(true);
		this.buttonList.clear();
		this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, I18n.format("selectWorld.renameButton", new Object[0])));
		this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, I18n.format("gui.cancel", new Object[0])));
		ISaveFormat isaveformat = this.mc.getSaveLoader();
		WorldInfo worldinfo = isaveformat.getWorldInfo(this.field_146584_g);
		String s = worldinfo.getWorldName();
		this.field_146583_f = new GuiTextField(this.fontRendererObj, this.width / 2 - 100, 60, 200, 20);
		this.field_146583_f.setFocused(true);
		this.field_146583_f.setText(s);
	}

	public void onGuiClosed()
	{
		Keyboard.enableRepeatEvents(false);
	}

	protected void actionPerformed(GuiButton p_146284_1_)
	{
		if (p_146284_1_.enabled)
		{
			if (p_146284_1_.id == 1)
			{
				this.mc.displayGuiScreen(this.field_146585_a);
			}
			else if (p_146284_1_.id == 0)
			{
				ISaveFormat isaveformat = this.mc.getSaveLoader();
				isaveformat.renameWorld(this.field_146584_g, this.field_146583_f.getText().trim());
				this.mc.displayGuiScreen(this.field_146585_a);
			}
		}
	}

	protected void keyTyped(char par1, int par2)
	{
		this.field_146583_f.textboxKeyTyped(par1, par2);
		((GuiButton)this.buttonList.get(0)).enabled = this.field_146583_f.getText().trim().length() > 0;

		if (par2 == 28 || par2 == 156)
		{
			this.actionPerformed((GuiButton)this.buttonList.get(0));
		}
	}

	protected void mouseClicked(int par1, int par2, int par3)
	{
		super.mouseClicked(par1, par2, par3);
		this.field_146583_f.mouseClicked(par1, par2, par3);
	}

	public void drawScreen(int par1, int par2, float par3)
	{
		this.drawDefaultBackground();
		this.drawCenteredString(this.fontRendererObj, I18n.format("selectWorld.renameTitle", new Object[0]), this.width / 2, 20, 16777215);
		this.drawString(this.fontRendererObj, I18n.format("selectWorld.enterName", new Object[0]), this.width / 2 - 100, 47, 10526880);
		this.field_146583_f.drawTextBox();
		super.drawScreen(par1, par2, par3);
	}
}