package net.minecraft.client.gui.achievement;

import java.io.IOException;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiOptionButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.IProgressMeter;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.resources.I18n;
import net.minecraft.init.Blocks;
import net.minecraft.network.play.client.C16PacketClientStatus;
import net.minecraft.stats.Achievement;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatFileWriter;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Mouse;
import java.util.LinkedList;
import java.util.List;
import net.minecraftforge.common.AchievementPage;

@SideOnly(Side.CLIENT)
public class GuiAchievements extends GuiScreen implements IProgressMeter
{
    private static final int field_146572_y = AchievementList.minDisplayColumn * 24 - 112;
    private static final int field_146571_z = AchievementList.minDisplayRow * 24 - 112;
    private static final int field_146559_A = AchievementList.maxDisplayColumn * 24 - 77;
    private static final int field_146560_B = AchievementList.maxDisplayRow * 24 - 77;
    private static final ResourceLocation field_146561_C = new ResourceLocation("textures/gui/achievement/achievement_background.png");
    protected GuiScreen parentScreen;
    protected int field_146555_f = 256;
    protected int field_146557_g = 202;
    protected int field_146563_h;
    protected int field_146564_i;
    protected float field_146570_r = 1.0F;
    protected double field_146569_s;
    protected double field_146568_t;
    protected double field_146567_u;
    protected double field_146566_v;
    protected double field_146565_w;
    protected double field_146573_x;
    private int field_146554_D;
    private StatFileWriter statFileWriter;
    private boolean loadingAchievements = true;
    private static final String __OBFID = "CL_00000722";

    private int currentPage = -1;
    private GuiButton button;
    private LinkedList<Achievement> minecraftAchievements = new LinkedList<Achievement>();

    public GuiAchievements(GuiScreen p_i45026_1_, StatFileWriter p_i45026_2_)
    {
        this.parentScreen = p_i45026_1_;
        this.statFileWriter = p_i45026_2_;
        short short1 = 141;
        short short2 = 141;
        this.field_146569_s = this.field_146567_u = this.field_146565_w = (double)(AchievementList.openInventory.displayColumn * 24 - short1 / 2 - 12);
        this.field_146568_t = this.field_146566_v = this.field_146573_x = (double)(AchievementList.openInventory.displayRow * 24 - short2 / 2);
        minecraftAchievements.clear();
        for (Object achievement : AchievementList.achievementList)
        {
            if (!AchievementPage.isAchievementInPages((Achievement)achievement))
            {
                minecraftAchievements.add((Achievement)achievement);
            }
        }
    }

    /**
     * Adds the buttons (and other controls) to the screen in question.
     */
    public void initGui()
    {
        this.mc.getNetHandler().addToSendQueue(new C16PacketClientStatus(C16PacketClientStatus.EnumState.REQUEST_STATS));
        this.buttonList.clear();
        this.buttonList.add(new GuiOptionButton(1, this.width / 2 + 24, this.height / 2 + 74, 80, 20, I18n.format("gui.done", new Object[0])));
        this.buttonList.add(button = new GuiButton(2, (width - field_146555_f) / 2 + 24, height / 2 + 74, 125, 20, AchievementPage.getTitle(currentPage)));
    }

    protected void actionPerformed(GuiButton button) throws IOException
    {
        if (!this.loadingAchievements)
        {
            if (button.id == 1)
            {
                this.mc.displayGuiScreen(this.parentScreen);
            }

            if (button.id == 2)
            {
                currentPage++;
                if (currentPage >= AchievementPage.getAchievementPages().size())
                {
                    currentPage = -1;
                }
                this.button.displayString = AchievementPage.getTitle(currentPage);
            }
        }
    }

    /**
     * Fired when a key is typed (except F11 who toggle full screen). This is the equivalent of
     * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
     */
    protected void keyTyped(char typedChar, int keyCode) throws IOException
    {
        if (keyCode == this.mc.gameSettings.keyBindInventory.getKeyCode())
        {
            this.mc.displayGuiScreen((GuiScreen)null);
            this.mc.setIngameFocus();
        }
        else
        {
            super.keyTyped(typedChar, keyCode);
        }
    }

    /**
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
     */
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
    {
        if (this.loadingAchievements)
        {
            this.drawDefaultBackground();
            this.drawCenteredString(this.fontRendererObj, I18n.format("multiplayer.downloadingStats", new Object[0]), this.width / 2, this.height / 2, 16777215);
            this.drawCenteredString(this.fontRendererObj, lanSearchStates[(int)(Minecraft.getSystemTime() / 150L % (long)lanSearchStates.length)], this.width / 2, this.height / 2 + this.fontRendererObj.FONT_HEIGHT * 2, 16777215);
        }
        else
        {
            int k;

            if (Mouse.isButtonDown(0))
            {
                k = (this.width - this.field_146555_f) / 2;
                int l = (this.height - this.field_146557_g) / 2;
                int i1 = k + 8;
                int j1 = l + 17;

                if ((this.field_146554_D == 0 || this.field_146554_D == 1) && mouseX >= i1 && mouseX < i1 + 224 && mouseY >= j1 && mouseY < j1 + 155)
                {
                    if (this.field_146554_D == 0)
                    {
                        this.field_146554_D = 1;
                    }
                    else
                    {
                        this.field_146567_u -= (double)((float)(mouseX - this.field_146563_h) * this.field_146570_r);
                        this.field_146566_v -= (double)((float)(mouseY - this.field_146564_i) * this.field_146570_r);
                        this.field_146565_w = this.field_146569_s = this.field_146567_u;
                        this.field_146573_x = this.field_146568_t = this.field_146566_v;
                    }

                    this.field_146563_h = mouseX;
                    this.field_146564_i = mouseY;
                }
            }
            else
            {
                this.field_146554_D = 0;
            }

            k = Mouse.getDWheel();
            float f4 = this.field_146570_r;

            if (k < 0)
            {
                this.field_146570_r += 0.25F;
            }
            else if (k > 0)
            {
                this.field_146570_r -= 0.25F;
            }

            this.field_146570_r = MathHelper.clamp_float(this.field_146570_r, 1.0F, 2.0F);

            if (this.field_146570_r != f4)
            {
                float f6 = f4 - this.field_146570_r;
                float f5 = f4 * (float)this.field_146555_f;
                float f1 = f4 * (float)this.field_146557_g;
                float f2 = this.field_146570_r * (float)this.field_146555_f;
                float f3 = this.field_146570_r * (float)this.field_146557_g;
                this.field_146567_u -= (double)((f2 - f5) * 0.5F);
                this.field_146566_v -= (double)((f3 - f1) * 0.5F);
                this.field_146565_w = this.field_146569_s = this.field_146567_u;
                this.field_146573_x = this.field_146568_t = this.field_146566_v;
            }

            if (this.field_146565_w < (double)field_146572_y)
            {
                this.field_146565_w = (double)field_146572_y;
            }

            if (this.field_146573_x < (double)field_146571_z)
            {
                this.field_146573_x = (double)field_146571_z;
            }

            if (this.field_146565_w >= (double)field_146559_A)
            {
                this.field_146565_w = (double)(field_146559_A - 1);
            }

            if (this.field_146573_x >= (double)field_146560_B)
            {
                this.field_146573_x = (double)(field_146560_B - 1);
            }

            this.drawDefaultBackground();
            this.drawAchievementScreen(mouseX, mouseY, partialTicks);
            GlStateManager.disableLighting();
            GlStateManager.disableDepth();
            this.drawTitle();
            GlStateManager.enableLighting();
            GlStateManager.enableDepth();
        }
    }

    public void doneLoading()
    {
        if (this.loadingAchievements)
        {
            this.loadingAchievements = false;
        }
    }

    /**
     * Called from the main game loop to update the screen.
     */
    public void updateScreen()
    {
        if (!this.loadingAchievements)
        {
            this.field_146569_s = this.field_146567_u;
            this.field_146568_t = this.field_146566_v;
            double d0 = this.field_146565_w - this.field_146567_u;
            double d1 = this.field_146573_x - this.field_146566_v;

            if (d0 * d0 + d1 * d1 < 4.0D)
            {
                this.field_146567_u += d0;
                this.field_146566_v += d1;
            }
            else
            {
                this.field_146567_u += d0 * 0.85D;
                this.field_146566_v += d1 * 0.85D;
            }
        }
    }

    protected void drawTitle()
    {
        int i = (this.width - this.field_146555_f) / 2;
        int j = (this.height - this.field_146557_g) / 2;
        this.fontRendererObj.drawString(I18n.format("gui.achievements", new Object[0]), i + 15, j + 5, 4210752);
    }

    protected void drawAchievementScreen(int p_146552_1_, int p_146552_2_, float p_146552_3_)
    {
        int k = MathHelper.floor_double(this.field_146569_s + (this.field_146567_u - this.field_146569_s) * (double)p_146552_3_);
        int l = MathHelper.floor_double(this.field_146568_t + (this.field_146566_v - this.field_146568_t) * (double)p_146552_3_);

        if (k < field_146572_y)
        {
            k = field_146572_y;
        }

        if (l < field_146571_z)
        {
            l = field_146571_z;
        }

        if (k >= field_146559_A)
        {
            k = field_146559_A - 1;
        }

        if (l >= field_146560_B)
        {
            l = field_146560_B - 1;
        }

        int i1 = (this.width - this.field_146555_f) / 2;
        int j1 = (this.height - this.field_146557_g) / 2;
        int k1 = i1 + 16;
        int l1 = j1 + 17;
        this.zLevel = 0.0F;
        GlStateManager.depthFunc(518);
        GlStateManager.pushMatrix();
        GlStateManager.translate((float)k1, (float)l1, -200.0F);
        GlStateManager.scale(1.0F / this.field_146570_r, 1.0F / this.field_146570_r, 0.0F);
        GlStateManager.func_179098_w();
        GlStateManager.disableLighting();
        GlStateManager.enableRescaleNormal();
        GlStateManager.enableColorMaterial();
        int i2 = k + 288 >> 4;
        int j2 = l + 288 >> 4;
        int k2 = (k + 288) % 16;
        int l2 = (l + 288) % 16;
        boolean flag = true;
        boolean flag1 = true;
        boolean flag2 = true;
        boolean flag3 = true;
        boolean flag4 = true;
        Random random = new Random();
        float f1 = 16.0F / this.field_146570_r;
        float f2 = 16.0F / this.field_146570_r;
        int i3;
        float f3;
        int j3;
        int k3;

        for (i3 = 0; (float)i3 * f1 - (float)l2 < 155.0F; ++i3)
        {
            f3 = 0.6F - (float)(j2 + i3) / 25.0F * 0.3F;
            GlStateManager.color(f3, f3, f3, 1.0F);

            for (j3 = 0; (float)j3 * f2 - (float)k2 < 224.0F; ++j3)
            {
                random.setSeed((long)(this.mc.getSession().getPlayerID().hashCode() + i2 + j3 + (j2 + i3) * 16));
                k3 = random.nextInt(1 + j2 + i3) + (j2 + i3) / 2;
                TextureAtlasSprite textureatlassprite = this.func_175371_a(Blocks.sand);

                if (k3 <= 37 && j2 + i3 != 35)
                {
                    if (k3 == 22)
                    {
                        if (random.nextInt(2) == 0)
                        {
                            textureatlassprite = this.func_175371_a(Blocks.diamond_ore);
                        }
                        else
                        {
                            textureatlassprite = this.func_175371_a(Blocks.redstone_ore);
                        }
                    }
                    else if (k3 == 10)
                    {
                        textureatlassprite = this.func_175371_a(Blocks.iron_ore);
                    }
                    else if (k3 == 8)
                    {
                        textureatlassprite = this.func_175371_a(Blocks.coal_ore);
                    }
                    else if (k3 > 4)
                    {
                        textureatlassprite = this.func_175371_a(Blocks.stone);
                    }
                    else if (k3 > 0)
                    {
                        textureatlassprite = this.func_175371_a(Blocks.dirt);
                    }
                }
                else
                {
                    Block block = Blocks.bedrock;
                    textureatlassprite = this.func_175371_a(block);
                }

                this.mc.getTextureManager().bindTexture(TextureMap.locationBlocksTexture);
                this.func_175175_a(j3 * 16 - k2, i3 * 16 - l2, textureatlassprite, 16, 16);
            }
        }

        GlStateManager.enableDepth();
        GlStateManager.depthFunc(515);
        this.mc.getTextureManager().bindTexture(field_146561_C);
        int l3;
        int i4;
        int l4;

        List<Achievement> achievementList = (currentPage == -1 ? minecraftAchievements : AchievementPage.getAchievementPage(currentPage).getAchievements());
        for (i3 = 0; i3 < achievementList.size(); ++i3)
        {
            Achievement achievement1 = achievementList.get(i3);

            if (achievement1.parentAchievement != null && achievementList.contains(achievement1.parentAchievement))
            {
                j3 = achievement1.displayColumn * 24 - k + 11;
                k3 = achievement1.displayRow * 24 - l + 11;
                int k4 = achievement1.parentAchievement.displayColumn * 24 - k + 11;
                l4 = achievement1.parentAchievement.displayRow * 24 - l + 11;
                boolean flag5 = this.statFileWriter.hasAchievementUnlocked(achievement1);
                boolean flag6 = this.statFileWriter.canUnlockAchievement(achievement1);
                l3 = this.statFileWriter.func_150874_c(achievement1);

                if (l3 <= 4)
                {
                    i4 = -16777216;

                    if (flag5)
                    {
                        i4 = -6250336;
                    }
                    else if (flag6)
                    {
                        i4 = -16711936;
                    }

                    this.drawHorizontalLine(j3, k4, k3, i4);
                    this.drawVerticalLine(k4, k3, l4, i4);

                    if (j3 > k4)
                    {
                        this.drawTexturedModalRect(j3 - 11 - 7, k3 - 5, 114, 234, 7, 11);
                    }
                    else if (j3 < k4)
                    {
                        this.drawTexturedModalRect(j3 + 11, k3 - 5, 107, 234, 7, 11);
                    }
                    else if (k3 > l4)
                    {
                        this.drawTexturedModalRect(j3 - 5, k3 - 11 - 7, 96, 234, 11, 7);
                    }
                    else if (k3 < l4)
                    {
                        this.drawTexturedModalRect(j3 - 5, k3 + 11, 96, 241, 11, 7);
                    }
                }
            }
        }

        Achievement achievement = null;
        f3 = (float)(p_146552_1_ - k1) * this.field_146570_r;
        float f4 = (float)(p_146552_2_ - l1) * this.field_146570_r;
        RenderHelper.enableGUIStandardItemLighting();
        GlStateManager.disableLighting();
        GlStateManager.enableRescaleNormal();
        GlStateManager.enableColorMaterial();
        int i5;
        int j5;

        for (k3 = 0; k3 < achievementList.size(); ++k3)
        {
            Achievement achievement2 = (Achievement)achievementList.get(k3);
            l4 = achievement2.displayColumn * 24 - k;
            i5 = achievement2.displayRow * 24 - l;

            if (l4 >= -24 && i5 >= -24 && (float)l4 <= 224.0F * this.field_146570_r && (float)i5 <= 155.0F * this.field_146570_r)
            {
                j5 = this.statFileWriter.func_150874_c(achievement2);
                float f5;

                if (this.statFileWriter.hasAchievementUnlocked(achievement2))
                {
                    f5 = 0.75F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                }
                else if (this.statFileWriter.canUnlockAchievement(achievement2))
                {
                    f5 = 1.0F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                }
                else if (j5 < 3)
                {
                    f5 = 0.3F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                }
                else if (j5 == 3)
                {
                    f5 = 0.2F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                }
                else
                {
                    if (j5 != 4)
                    {
                        continue;
                    }

                    f5 = 0.1F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                }

                this.mc.getTextureManager().bindTexture(field_146561_C);

                GlStateManager.enableBlend(); // Forge: Specifically enable blend because it is needed here. And we fix Generic RenderItem's leakage of it.
                if (achievement2.getSpecial())
                {
                    this.drawTexturedModalRect(l4 - 2, i5 - 2, 26, 202, 26, 26);
                }
                else
                {
                    this.drawTexturedModalRect(l4 - 2, i5 - 2, 0, 202, 26, 26);
                }
                GlStateManager.disableBlend(); //Forge: Cleanup states we set.

                if (!this.statFileWriter.canUnlockAchievement(achievement2))
                {
                    f5 = 0.1F;
                    GlStateManager.color(f5, f5, f5, 1.0F);
                    this.itemRender.func_175039_a(false);
                }

                GlStateManager.disableLighting(); //Forge: Make sure Lighting is disabled. Fixes MC-33065
                GlStateManager.enableCull();
                this.itemRender.func_180450_b(achievement2.theItemStack, l4 + 3, i5 + 3);
                GlStateManager.blendFunc(770, 771);
                GlStateManager.disableLighting();

                if (!this.statFileWriter.canUnlockAchievement(achievement2))
                {
                    this.itemRender.func_175039_a(true);
                }

                GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);

                if (f3 >= (float)l4 && f3 <= (float)(l4 + 22) && f4 >= (float)i5 && f4 <= (float)(i5 + 22))
                {
                    achievement = achievement2;
                }
            }
        }

        GlStateManager.disableDepth();
        GlStateManager.enableBlend();
        GlStateManager.popMatrix();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(field_146561_C);
        this.drawTexturedModalRect(i1, j1, 0, 0, this.field_146555_f, this.field_146557_g);
        this.zLevel = 0.0F;
        GlStateManager.depthFunc(515);
        GlStateManager.disableDepth();
        GlStateManager.func_179098_w();
        super.drawScreen(p_146552_1_, p_146552_2_, p_146552_3_);

        if (achievement != null)
        {
            String s = achievement.getStatName().getUnformattedText();
            String s1 = achievement.getDescription();
            l4 = p_146552_1_ + 12;
            i5 = p_146552_2_ - 4;
            j5 = this.statFileWriter.func_150874_c(achievement);

            if (this.statFileWriter.canUnlockAchievement(achievement))
            {
                l3 = Math.max(this.fontRendererObj.getStringWidth(s), 120);
                i4 = this.fontRendererObj.splitStringWidth(s1, l3);

                if (this.statFileWriter.hasAchievementUnlocked(achievement))
                {
                    i4 += 12;
                }

                this.drawGradientRect(l4 - 3, i5 - 3, l4 + l3 + 3, i5 + i4 + 3 + 12, -1073741824, -1073741824);
                this.fontRendererObj.drawSplitString(s1, l4, i5 + 12, l3, -6250336);

                if (this.statFileWriter.hasAchievementUnlocked(achievement))
                {
                    this.fontRendererObj.func_175063_a(I18n.format("achievement.taken", new Object[0]), (float)l4, (float)(i5 + i4 + 4), -7302913);
                }
            }
            else
            {
                int j4;
                String s2;

                if (j5 == 3)
                {
                    s = I18n.format("achievement.unknown", new Object[0]);
                    l3 = Math.max(this.fontRendererObj.getStringWidth(s), 120);
                    s2 = (new ChatComponentTranslation("achievement.requires", new Object[] {achievement.parentAchievement.getStatName()})).getUnformattedText();
                    j4 = this.fontRendererObj.splitStringWidth(s2, l3);
                    this.drawGradientRect(l4 - 3, i5 - 3, l4 + l3 + 3, i5 + j4 + 12 + 3, -1073741824, -1073741824);
                    this.fontRendererObj.drawSplitString(s2, l4, i5 + 12, l3, -9416624);
                }
                else if (j5 < 3)
                {
                    l3 = Math.max(this.fontRendererObj.getStringWidth(s), 120);
                    s2 = (new ChatComponentTranslation("achievement.requires", new Object[] {achievement.parentAchievement.getStatName()})).getUnformattedText();
                    j4 = this.fontRendererObj.splitStringWidth(s2, l3);
                    this.drawGradientRect(l4 - 3, i5 - 3, l4 + l3 + 3, i5 + j4 + 12 + 3, -1073741824, -1073741824);
                    this.fontRendererObj.drawSplitString(s2, l4, i5 + 12, l3, -9416624);
                }
                else
                {
                    s = null;
                }
            }

            if (s != null)
            {
                this.fontRendererObj.func_175063_a(s, (float)l4, (float)i5, this.statFileWriter.canUnlockAchievement(achievement) ? (achievement.getSpecial() ? -128 : -1) : (achievement.getSpecial() ? -8355776 : -8355712));
            }
        }

        GlStateManager.enableDepth();
        GlStateManager.enableLighting();
        RenderHelper.disableStandardItemLighting();
    }

    private TextureAtlasSprite func_175371_a(Block p_175371_1_)
    {
        return Minecraft.getMinecraft().getBlockRendererDispatcher().func_175023_a().func_178122_a(p_175371_1_.getDefaultState());
    }

    /**
     * Returns true if this GUI should pause the game when it is displayed in single-player
     */
    public boolean doesGuiPauseGame()
    {
        return !this.loadingAchievements;
    }
}