package telinc.telicraft.client.renderer.entity;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.util.Icon;
import net.minecraft.util.ResourceLocation;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import telinc.telicraft.client.model.ModelPetrify;
import telinc.telicraft.configuration.TelicraftConfigurationSettings;
import telinc.telicraft.item.TelicraftItems;
import telinc.telicraft.lib.TextureReferences;

@SideOnly(Side.CLIENT)
public class RenderPetrify extends Render {
    protected ModelPetrify modelPetrify;

    public RenderPetrify() {
        this.modelPetrify = new ModelPetrify();
        this.shadowSize = 0.5F;
    }

    /**
     * The render method used in RenderPetrify that renders the potion model.
     */
    public void renderPetrifyNew(Entity par1Entity, double par2, double par4, double par6, float par8, float par9) {
        GL11.glPushMatrix();
        GL11.glTranslatef((float)par2, (float)par4, (float)par6);
        GL11.glRotatef(180.0F - par8, 0.0F, 1.0F, 0.0F);

        float f4 = 0.75F;
        GL11.glScalef(f4, f4, f4);
        GL11.glScalef(1.0F / f4, 1.0F / f4, 1.0F / f4);
        this.func_110777_b(par1Entity);
        GL11.glScalef(-1.0F, -1.0F, 1.0F);
        this.modelPetrify.renderPetrify();
        GL11.glPopMatrix();
    }

    public void renderPetrifyClassic(Entity par1Entity, double par2, double par4, double par6, float par8, float par9) {
        Icon icon = TelicraftItems.potionCustom.getIconFromDamage(2);

        GL11.glPushMatrix();
        GL11.glTranslatef((float)par2, (float)par4, (float)par6);
        GL11.glEnable(GL12.GL_RESCALE_NORMAL);
        GL11.glScalef(0.5F, 0.5F, 0.5F);
        this.func_110777_b(par1Entity);
        Tessellator tessellator = Tessellator.instance;
        this.func_77026_a(tessellator, icon);
        GL11.glDisable(GL12.GL_RESCALE_NORMAL);
        GL11.glPopMatrix();
    }

    @Override
    public void doRender(Entity par1Entity, double par2, double par4, double par6, float par8, float par9) {
        if(TelicraftConfigurationSettings.classicPetrifyModel){
            this.renderPetrifyClassic(par1Entity, par2, par4, par6, par8, par9);
        }else{
            this.renderPetrifyNew(par1Entity, par2, par4, par6, par8, par9);
        }
    }

    @Override
    protected ResourceLocation func_110775_a(Entity par1Entity) {
        return null;
    }

    @Override
    protected void func_110777_b(Entity par1Entity) {
        this.func_110776_a(TelicraftConfigurationSettings.classicPetrifyModel ? TextureMap.field_110576_c : TextureReferences.POTION_PETRIFY_NEW);
    }

    private void func_77026_a(Tessellator par1Tessellator, Icon par2Icon) {
        float f = par2Icon.getMinU();
        float f1 = par2Icon.getMaxU();
        float f2 = par2Icon.getMinV();
        float f3 = par2Icon.getMaxV();
        float f4 = 1.0F;
        float f5 = 0.5F;
        float f6 = 0.25F;
        GL11.glRotatef(180.0F - this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        par1Tessellator.startDrawingQuads();
        par1Tessellator.setNormal(0.0F, 1.0F, 0.0F);
        par1Tessellator.addVertexWithUV(0.0F - f5, 0.0F - f6, 0.0D, f, f3);
        par1Tessellator.addVertexWithUV(f4 - f5, 0.0F - f6, 0.0D, f1, f3);
        par1Tessellator.addVertexWithUV(f4 - f5, f4 - f6, 0.0D, f1, f2);
        par1Tessellator.addVertexWithUV(0.0F - f5, f4 - f6, 0.0D, f, f2);
        par1Tessellator.draw();
    }
}
