package net.minecraft.client.gui;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerRepair;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import org.apache.commons.io.Charsets;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
public class GuiRepair extends GuiContainer implements ICrafting
{
	private static final ResourceLocation field_147093_u = new ResourceLocation("textures/gui/container/anvil.png");
	private ContainerRepair field_147092_v;
	private GuiTextField field_147091_w;
	private InventoryPlayer field_147094_x;
	private static final String __OBFID = "CL_00000738";

	public GuiRepair(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
	{
		super(new ContainerRepair(par1InventoryPlayer, par2World, par3, par4, par5, Minecraft.getMinecraft().thePlayer));
		this.field_147094_x = par1InventoryPlayer;
		this.field_147092_v = (ContainerRepair)this.inventorySlots;
	}

	public void initGui()
	{
		super.initGui();
		Keyboard.enableRepeatEvents(true);
		int i = (this.width - this.xSize) / 2;
		int j = (this.height - this.ySize) / 2;
		this.field_147091_w = new GuiTextField(this.fontRendererObj, i + 62, j + 24, 103, 12);
		this.field_147091_w.setTextColor(-1);
		this.field_147091_w.setDisabledTextColour(-1);
		this.field_147091_w.setEnableBackgroundDrawing(false);
		this.field_147091_w.setMaxStringLength(40);
		this.inventorySlots.removeCraftingFromCrafters(this);
		this.inventorySlots.addCraftingToCrafters(this);
	}

	public void onGuiClosed()
	{
		super.onGuiClosed();
		Keyboard.enableRepeatEvents(false);
		this.inventorySlots.removeCraftingFromCrafters(this);
	}

	protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
	{
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_BLEND);
		this.fontRendererObj.drawString(I18n.format("container.repair", new Object[0]), 60, 6, 4210752);

		if (this.field_147092_v.maximumCost > 0)
		{
			int k = 8453920;
			boolean flag = true;
			String s = I18n.format("container.repair.cost", new Object[] {Integer.valueOf(this.field_147092_v.maximumCost)});

			if (this.field_147092_v.maximumCost >= 40 && !this.mc.thePlayer.capabilities.isCreativeMode)
			{
				s = I18n.format("container.repair.expensive", new Object[0]);
				k = 16736352;
			}
			else if (!this.field_147092_v.getSlot(2).getHasStack())
			{
				flag = false;
			}
			else if (!this.field_147092_v.getSlot(2).canTakeStack(this.field_147094_x.player))
			{
				k = 16736352;
			}

			if (flag)
			{
				int l = -16777216 | (k & 16579836) >> 2 | k & -16777216;
				int i1 = this.xSize - 8 - this.fontRendererObj.getStringWidth(s);
				byte b0 = 67;

				if (this.fontRendererObj.getUnicodeFlag())
				{
					drawRect(i1 - 3, b0 - 2, this.xSize - 7, b0 + 10, -16777216);
					drawRect(i1 - 2, b0 - 1, this.xSize - 8, b0 + 9, -12895429);
				}
				else
				{
					this.fontRendererObj.drawString(s, i1, b0 + 1, l);
					this.fontRendererObj.drawString(s, i1 + 1, b0, l);
					this.fontRendererObj.drawString(s, i1 + 1, b0 + 1, l);
				}

				this.fontRendererObj.drawString(s, i1, b0, k);
			}
		}

		GL11.glEnable(GL11.GL_LIGHTING);
	}

	protected void keyTyped(char par1, int par2)
	{
		if (this.field_147091_w.textboxKeyTyped(par1, par2))
		{
			this.func_147090_g();
		}
		else
		{
			super.keyTyped(par1, par2);
		}
	}

	private void func_147090_g()
	{
		String s = this.field_147091_w.getText();
		Slot slot = this.field_147092_v.getSlot(0);

		if (slot != null && slot.getHasStack() && !slot.getStack().hasDisplayName() && s.equals(slot.getStack().getDisplayName()))
		{
			s = "";
		}

		this.field_147092_v.updateItemName(s);
		this.mc.thePlayer.sendQueue.addToSendQueue(new C17PacketCustomPayload("MC|ItemName", s.getBytes(Charsets.UTF_8)));
	}

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

	public void drawScreen(int par1, int par2, float par3)
	{
		super.drawScreen(par1, par2, par3);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_BLEND);
		this.field_147091_w.drawTextBox();
	}

	protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
	{
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		this.mc.getTextureManager().bindTexture(field_147093_u);
		int k = (this.width - this.xSize) / 2;
		int l = (this.height - this.ySize) / 2;
		this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
		this.drawTexturedModalRect(k + 59, l + 20, 0, this.ySize + (this.field_147092_v.getSlot(0).getHasStack() ? 0 : 16), 110, 16);

		if ((this.field_147092_v.getSlot(0).getHasStack() || this.field_147092_v.getSlot(1).getHasStack()) && !this.field_147092_v.getSlot(2).getHasStack())
		{
			this.drawTexturedModalRect(k + 99, l + 45, this.xSize, 0, 28, 21);
		}
	}

	public void sendContainerAndContentsToPlayer(Container par1Container, List par2List)
	{
		this.sendSlotContents(par1Container, 0, par1Container.getSlot(0).getStack());
	}

	public void sendSlotContents(Container par1Container, int par2, ItemStack par3ItemStack)
	{
		if (par2 == 0)
		{
			this.field_147091_w.setText(par3ItemStack == null ? "" : par3ItemStack.getDisplayName());
			this.field_147091_w.setEnabled(par3ItemStack != null);

			if (par3ItemStack != null)
			{
				this.func_147090_g();
			}
		}
	}

	public void sendProgressBarUpdate(Container par1Container, int par2, int par3) {}
}