package net.minecraft.client;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.shader.Framebuffer;
import net.minecraft.util.IProgressUpdate;
import net.minecraft.util.MinecraftError;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class LoadingScreenRenderer implements IProgressUpdate
{
    private String field_73727_a = "";
    /** A reference to the Minecraft object. */
    private Minecraft mc;
    /** The text currently displayed (i.e. the argument to the last call to printText or func_73722_d) */
    private String currentlyDisplayedText = "";
    private long field_73723_d = Minecraft.getSystemTime();
    private boolean field_73724_e;
    private ScaledResolution field_146587_f;
    private Framebuffer field_146588_g;
    private static final String __OBFID = "CL_00000655";

    public LoadingScreenRenderer(Minecraft mcIn)
    {
        this.mc = mcIn;
        this.field_146587_f = new ScaledResolution(mcIn, mcIn.displayWidth, mcIn.displayHeight);
        this.field_146588_g = new Framebuffer(mcIn.displayWidth, mcIn.displayHeight, false);
        this.field_146588_g.setFramebufferFilter(9728);
    }

    /**
     * this string, followed by "working..." and then the "% complete" are the 3 lines shown. This resets progress to 0,
     * and the WorkingString to "working...".
     */
    public void resetProgressAndMessage(String p_73721_1_)
    {
        this.field_73724_e = false;
        this.func_73722_d(p_73721_1_);
    }

    /**
     * Shows the 'Saving level' string.
     */
    public void displaySavingString(String message)
    {
        this.field_73724_e = true;
        this.func_73722_d(message);
    }

    private void func_73722_d(String p_73722_1_)
    {
        this.currentlyDisplayedText = p_73722_1_;

        if (!this.mc.running)
        {
            if (!this.field_73724_e)
            {
                throw new MinecraftError();
            }
        }
        else
        {
            GlStateManager.clear(256);
            GlStateManager.matrixMode(5889);
            GlStateManager.loadIdentity();

            if (OpenGlHelper.isFramebufferEnabled())
            {
                int i = this.field_146587_f.getScaleFactor();
                GlStateManager.ortho(0.0D, (double)(this.field_146587_f.getScaledWidth() * i), (double)(this.field_146587_f.getScaledHeight() * i), 0.0D, 100.0D, 300.0D);
            }
            else
            {
                ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
                GlStateManager.ortho(0.0D, scaledresolution.getScaledWidth_double(), scaledresolution.getScaledHeight_double(), 0.0D, 100.0D, 300.0D);
            }

            GlStateManager.matrixMode(5888);
            GlStateManager.loadIdentity();
            GlStateManager.translate(0.0F, 0.0F, -200.0F);
        }
    }

    /**
     * Displays a string on the loading screen supposed to indicate what is being done currently.
     */
    public void displayLoadingString(String message)
    {
        if (!this.mc.running)
        {
            if (!this.field_73724_e)
            {
                throw new MinecraftError();
            }
        }
        else
        {
            this.field_73723_d = 0L;
            this.field_73727_a = message;
            this.setLoadingProgress(-1);
            this.field_73723_d = 0L;
        }
    }

    /**
     * Updates the progress bar on the loading screen to the specified amount. Args: loadProgress
     */
    public void setLoadingProgress(int progress)
    {
        if (!this.mc.running)
        {
            if (!this.field_73724_e)
            {
                throw new MinecraftError();
            }
        }
        else
        {
            long j = Minecraft.getSystemTime();

            if (j - this.field_73723_d >= 100L)
            {
                this.field_73723_d = j;
                ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
                int k = scaledresolution.getScaleFactor();
                int l = scaledresolution.getScaledWidth();
                int i1 = scaledresolution.getScaledHeight();

                if (OpenGlHelper.isFramebufferEnabled())
                {
                    this.field_146588_g.framebufferClear();
                }
                else
                {
                    GlStateManager.clear(256);
                }

                this.field_146588_g.bindFramebuffer(false);
                GlStateManager.matrixMode(5889);
                GlStateManager.loadIdentity();
                GlStateManager.ortho(0.0D, scaledresolution.getScaledWidth_double(), scaledresolution.getScaledHeight_double(), 0.0D, 100.0D, 300.0D);
                GlStateManager.matrixMode(5888);
                GlStateManager.loadIdentity();
                GlStateManager.translate(0.0F, 0.0F, -200.0F);

                if (!OpenGlHelper.isFramebufferEnabled())
                {
                    GlStateManager.clear(16640);
                }

                try
                {
                if (!net.minecraftforge.fml.client.FMLClientHandler.instance().handleLoadingScreen(scaledresolution)) //FML Don't render while FML's pre-screen is rendering
                {
                Tessellator tessellator = Tessellator.getInstance();
                WorldRenderer worldrenderer = tessellator.getWorldRenderer();
                this.mc.getTextureManager().bindTexture(Gui.optionsBackground);
                float f = 32.0F;
                worldrenderer.startDrawingQuads();
                worldrenderer.func_178991_c(4210752);
                worldrenderer.addVertexWithUV(0.0D, (double)i1, 0.0D, 0.0D, (double)((float)i1 / f));
                worldrenderer.addVertexWithUV((double)l, (double)i1, 0.0D, (double)((float)l / f), (double)((float)i1 / f));
                worldrenderer.addVertexWithUV((double)l, 0.0D, 0.0D, (double)((float)l / f), 0.0D);
                worldrenderer.addVertexWithUV(0.0D, 0.0D, 0.0D, 0.0D, 0.0D);
                tessellator.draw();

                if (progress >= 0)
                {
                    byte b0 = 100;
                    byte b1 = 2;
                    int j1 = l / 2 - b0 / 2;
                    int k1 = i1 / 2 + 16;
                    GlStateManager.func_179090_x();
                    worldrenderer.startDrawingQuads();
                    worldrenderer.func_178991_c(8421504);
                    worldrenderer.addVertex((double)j1, (double)k1, 0.0D);
                    worldrenderer.addVertex((double)j1, (double)(k1 + b1), 0.0D);
                    worldrenderer.addVertex((double)(j1 + b0), (double)(k1 + b1), 0.0D);
                    worldrenderer.addVertex((double)(j1 + b0), (double)k1, 0.0D);
                    worldrenderer.func_178991_c(8454016);
                    worldrenderer.addVertex((double)j1, (double)k1, 0.0D);
                    worldrenderer.addVertex((double)j1, (double)(k1 + b1), 0.0D);
                    worldrenderer.addVertex((double)(j1 + progress), (double)(k1 + b1), 0.0D);
                    worldrenderer.addVertex((double)(j1 + progress), (double)k1, 0.0D);
                    tessellator.draw();
                    GlStateManager.func_179098_w();
                }

                GlStateManager.enableBlend();
                GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
                this.mc.fontRendererObj.func_175063_a(this.currentlyDisplayedText, (float)((l - this.mc.fontRendererObj.getStringWidth(this.currentlyDisplayedText)) / 2), (float)(i1 / 2 - 4 - 16), 16777215);
                this.mc.fontRendererObj.func_175063_a(this.field_73727_a, (float)((l - this.mc.fontRendererObj.getStringWidth(this.field_73727_a)) / 2), (float)(i1 / 2 - 4 + 8), 16777215);
                }
                }
                catch (java.io.IOException e)
                {
                    com.google.common.base.Throwables.propagate(e);
                } //FML End
                this.field_146588_g.unbindFramebuffer();

                if (OpenGlHelper.isFramebufferEnabled())
                {
                    this.field_146588_g.framebufferRender(l * k, i1 * k);
                }

                this.mc.func_175601_h();

                try
                {
                    Thread.yield();
                }
                catch (Exception exception)
                {
                    ;
                }
            }
        }
    }

    public void setDoneWorking() {}
}