package net.minecraft.client.gui;

import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.model.ModelBook;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.resources.I18n;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ContainerEnchantment;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnchantmentNameParts;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.util.glu.Project;

@SideOnly(Side.CLIENT)
public class GuiEnchantment extends GuiContainer
{
    private static final ResourceLocation field_147078_C = new ResourceLocation("textures/gui/container/enchanting_table.png");
    private static final ResourceLocation field_147070_D = new ResourceLocation("textures/entity/enchanting_table_book.png");
    private static final ModelBook field_147072_E = new ModelBook();
    private final InventoryPlayer field_175379_F;
    private Random field_147074_F = new Random();
    private ContainerEnchantment container;
    public int field_147073_u;
    public float field_147071_v;
    public float field_147069_w;
    public float field_147082_x;
    public float field_147081_y;
    public float field_147080_z;
    public float field_147076_A;
    ItemStack field_147077_B;
    private final IWorldNameable field_175380_I;
    private static final String __OBFID = "CL_00000757";

    public GuiEnchantment(InventoryPlayer p_i45502_1_, World worldIn, IWorldNameable p_i45502_3_)
    {
        super(new ContainerEnchantment(p_i45502_1_, worldIn));
        this.field_175379_F = p_i45502_1_;
        this.container = (ContainerEnchantment)this.inventorySlots;
        this.field_175380_I = p_i45502_3_;
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
     */
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    {
        this.fontRendererObj.drawString(this.field_175380_I.getDisplayName().getUnformattedText(), 12, 5, 4210752);
        this.fontRendererObj.drawString(this.field_175379_F.getDisplayName().getUnformattedText(), 8, this.ySize - 96 + 2, 4210752);
    }

    /**
     * Called from the main game loop to update the screen.
     */
    public void updateScreen()
    {
        super.updateScreen();
        this.func_147068_g();
    }

    /**
     * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
     */
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
    {
        super.mouseClicked(mouseX, mouseY, mouseButton);
        int l = (this.width - this.xSize) / 2;
        int i1 = (this.height - this.ySize) / 2;

        for (int j1 = 0; j1 < 3; ++j1)
        {
            int k1 = mouseX - (l + 60);
            int l1 = mouseY - (i1 + 14 + 19 * j1);

            if (k1 >= 0 && l1 >= 0 && k1 < 108 && l1 < 19 && this.container.enchantItem(this.mc.thePlayer, j1))
            {
                this.mc.playerController.sendEnchantPacket(this.container.windowId, j1);
            }
        }
    }

    /**
     * Args : renderPartialTicks, mouseX, mouseY
     */
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    {
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(field_147078_C);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
        GlStateManager.pushMatrix();
        GlStateManager.matrixMode(5889);
        GlStateManager.pushMatrix();
        GlStateManager.loadIdentity();
        ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
        GlStateManager.viewport((scaledresolution.getScaledWidth() - 320) / 2 * scaledresolution.getScaleFactor(), (scaledresolution.getScaledHeight() - 240) / 2 * scaledresolution.getScaleFactor(), 320 * scaledresolution.getScaleFactor(), 240 * scaledresolution.getScaleFactor());
        GlStateManager.translate(-0.34F, 0.23F, 0.0F);
        Project.gluPerspective(90.0F, 1.3333334F, 9.0F, 80.0F);
        float f1 = 1.0F;
        GlStateManager.matrixMode(5888);
        GlStateManager.loadIdentity();
        RenderHelper.enableStandardItemLighting();
        GlStateManager.translate(0.0F, 3.3F, -16.0F);
        GlStateManager.scale(f1, f1, f1);
        float f2 = 5.0F;
        GlStateManager.scale(f2, f2, f2);
        GlStateManager.rotate(180.0F, 0.0F, 0.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(field_147070_D);
        GlStateManager.rotate(20.0F, 1.0F, 0.0F, 0.0F);
        float f3 = this.field_147076_A + (this.field_147080_z - this.field_147076_A) * partialTicks;
        GlStateManager.translate((1.0F - f3) * 0.2F, (1.0F - f3) * 0.1F, (1.0F - f3) * 0.25F);
        GlStateManager.rotate(-(1.0F - f3) * 90.0F - 90.0F, 0.0F, 1.0F, 0.0F);
        GlStateManager.rotate(180.0F, 1.0F, 0.0F, 0.0F);
        float f4 = this.field_147069_w + (this.field_147071_v - this.field_147069_w) * partialTicks + 0.25F;
        float f5 = this.field_147069_w + (this.field_147071_v - this.field_147069_w) * partialTicks + 0.75F;
        f4 = (f4 - (float)MathHelper.truncateDoubleToInt((double)f4)) * 1.6F - 0.3F;
        f5 = (f5 - (float)MathHelper.truncateDoubleToInt((double)f5)) * 1.6F - 0.3F;

        if (f4 < 0.0F)
        {
            f4 = 0.0F;
        }

        if (f5 < 0.0F)
        {
            f5 = 0.0F;
        }

        if (f4 > 1.0F)
        {
            f4 = 1.0F;
        }

        if (f5 > 1.0F)
        {
            f5 = 1.0F;
        }

        GlStateManager.enableRescaleNormal();
        field_147072_E.render((Entity)null, 0.0F, f4, f5, f3, 0.0F, 0.0625F);
        GlStateManager.disableRescaleNormal();
        RenderHelper.disableStandardItemLighting();
        GlStateManager.matrixMode(5889);
        GlStateManager.viewport(0, 0, this.mc.displayWidth, this.mc.displayHeight);
        GlStateManager.popMatrix();
        GlStateManager.matrixMode(5888);
        GlStateManager.popMatrix();
        RenderHelper.disableStandardItemLighting();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        EnchantmentNameParts.func_178176_a().reseedRandomGenerator((long)this.container.xpSeed);
        int i1 = this.container.getLapisAmount();

        for (int j1 = 0; j1 < 3; ++j1)
        {
            int k1 = k + 60;
            int l1 = k1 + 20;
            byte b0 = 86;
            String s = EnchantmentNameParts.func_178176_a().generateNewRandomName();
            this.zLevel = 0.0F;
            this.mc.getTextureManager().bindTexture(field_147078_C);
            int i2 = this.container.enchantLevels[j1];
            GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);

            if (i2 == 0)
            {
                this.drawTexturedModalRect(k1, l + 14 + 19 * j1, 0, 185, 108, 19);
            }
            else
            {
                String s1 = "" + i2;
                FontRenderer fontrenderer = this.mc.standardGalacticFontRenderer;
                int j2 = 6839882;

                if ((i1 < j1 + 1 || this.mc.thePlayer.experienceLevel < i2) && !this.mc.thePlayer.capabilities.isCreativeMode)
                {
                    this.drawTexturedModalRect(k1, l + 14 + 19 * j1, 0, 185, 108, 19);
                    this.drawTexturedModalRect(k1 + 1, l + 15 + 19 * j1, 16 * j1, 239, 16, 16);
                    fontrenderer.drawSplitString(s, l1, l + 16 + 19 * j1, b0, (j2 & 16711422) >> 1);
                    j2 = 4226832;
                }
                else
                {
                    int k2 = mouseX - (k + 60);
                    int l2 = mouseY - (l + 14 + 19 * j1);

                    if (k2 >= 0 && l2 >= 0 && k2 < 108 && l2 < 19)
                    {
                        this.drawTexturedModalRect(k1, l + 14 + 19 * j1, 0, 204, 108, 19);
                        j2 = 16777088;
                    }
                    else
                    {
                        this.drawTexturedModalRect(k1, l + 14 + 19 * j1, 0, 166, 108, 19);
                    }

                    this.drawTexturedModalRect(k1 + 1, l + 15 + 19 * j1, 16 * j1, 223, 16, 16);
                    fontrenderer.drawSplitString(s, l1, l + 16 + 19 * j1, b0, j2);
                    j2 = 8453920;
                }

                fontrenderer = this.mc.fontRendererObj;
                fontrenderer.func_175063_a(s1, (float)(l1 + 86 - fontrenderer.getStringWidth(s1)), (float)(l + 16 + 19 * j1 + 7), j2);
            }
        }
    }

    /**
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
     */
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
    {
        super.drawScreen(mouseX, mouseY, partialTicks);
        boolean flag = this.mc.thePlayer.capabilities.isCreativeMode;
        int k = this.container.getLapisAmount();

        for (int l = 0; l < 3; ++l)
        {
            int i1 = this.container.enchantLevels[l];
            int j1 = this.container.field_178151_h[l];
            int k1 = l + 1;

            if (this.isPointInRegion(60, 14 + 19 * l, 108, 17, mouseX, mouseY) && i1 > 0 && j1 >= 0)
            {
                ArrayList arraylist = Lists.newArrayList();
                String s;

                if (j1 >= 0 && Enchantment.func_180306_c(j1 & 255) != null)
                {
                    s = Enchantment.func_180306_c(j1 & 255).getTranslatedName((j1 & 65280) >> 8);
                    arraylist.add(EnumChatFormatting.WHITE.toString() + EnumChatFormatting.ITALIC.toString() + I18n.format("container.enchant.clue", new Object[] {s}));
                }

                if (!flag)
                {
                    if (j1 >= 0)
                    {
                        arraylist.add("");
                    }

                    if (this.mc.thePlayer.experienceLevel < i1)
                    {
                        arraylist.add(EnumChatFormatting.RED.toString() + "Level Requirement: " + this.container.enchantLevels[l]);
                    }
                    else
                    {
                        s = "";

                        if (k1 == 1)
                        {
                            s = I18n.format("container.enchant.lapis.one", new Object[0]);
                        }
                        else
                        {
                            s = I18n.format("container.enchant.lapis.many", new Object[] {Integer.valueOf(k1)});
                        }

                        if (k >= k1)
                        {
                            arraylist.add(EnumChatFormatting.GRAY.toString() + "" + s);
                        }
                        else
                        {
                            arraylist.add(EnumChatFormatting.RED.toString() + "" + s);
                        }

                        if (k1 == 1)
                        {
                            s = I18n.format("container.enchant.level.one", new Object[0]);
                        }
                        else
                        {
                            s = I18n.format("container.enchant.level.many", new Object[] {Integer.valueOf(k1)});
                        }

                        arraylist.add(EnumChatFormatting.GRAY.toString() + "" + s);
                    }
                }

                this.drawHoveringText(arraylist, mouseX, mouseY);
                break;
            }
        }
    }

    public void func_147068_g()
    {
        ItemStack itemstack = this.inventorySlots.getSlot(0).getStack();

        if (!ItemStack.areItemStacksEqual(itemstack, this.field_147077_B))
        {
            this.field_147077_B = itemstack;

            do
            {
                this.field_147082_x += (float)(this.field_147074_F.nextInt(4) - this.field_147074_F.nextInt(4));
            }
            while (this.field_147071_v <= this.field_147082_x + 1.0F && this.field_147071_v >= this.field_147082_x - 1.0F);
        }

        ++this.field_147073_u;
        this.field_147069_w = this.field_147071_v;
        this.field_147076_A = this.field_147080_z;
        boolean flag = false;

        for (int i = 0; i < 3; ++i)
        {
            if (this.container.enchantLevels[i] != 0)
            {
                flag = true;
            }
        }

        if (flag)
        {
            this.field_147080_z += 0.2F;
        }
        else
        {
            this.field_147080_z -= 0.2F;
        }

        this.field_147080_z = MathHelper.clamp_float(this.field_147080_z, 0.0F, 1.0F);
        float f1 = (this.field_147082_x - this.field_147071_v) * 0.4F;
        float f = 0.2F;
        f1 = MathHelper.clamp_float(f1, -f, f);
        this.field_147081_y += (f1 - this.field_147081_y) * 0.9F;
        this.field_147071_v += this.field_147081_y;
    }
}