package client.renderer.entity;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.blockmodel.IBakedModel;
import client.renderer.texture.TextureMap;
import common.entity.item.EntityItem;
import common.item.Item;
import common.item.ItemStack;
import common.rng.Random;
import common.util.ExtMath;

public class RenderEntityItem extends Render<EntityItem>
{
    private final RenderItem itemRenderer;
    private Random field_177079_e = new Random();

    public RenderEntityItem(RenderManager renderManagerIn, RenderItem p_i46167_2_)
    {
        super(renderManagerIn);
        this.itemRenderer = p_i46167_2_;
//        this.shadowSize = 0.15F;
//        this.shadowOpaque = 0.75F;
    }

    private int func_177077_a(EntityItem itemIn, double p_177077_2_, double p_177077_4_, double p_177077_6_, float p_177077_8_, IBakedModel p_177077_9_)
    {
        ItemStack itemstack = itemIn.getEntityItem();
        Item item = itemstack.getItem();

        if (item == null)
        {
            return 0;
        }
        else
        {
            boolean flag = p_177077_9_.isGui3d();
            int i = this.getMultiplier(itemstack);
            float f = 0.25F;
            float f1 = ExtMath.sin(((float)itemIn.getAge() + p_177077_8_) / 10.0F + itemIn.hoverStart) * 0.1F + 0.1F;
            GL11.glTranslatef((float)p_177077_2_, (float)p_177077_4_ + f1 + 0.25F, (float)p_177077_6_);

            if (flag || this.manager.gm != null)
            {
                float f3 = (((float)itemIn.getAge() + p_177077_8_) / 20.0F + itemIn.hoverStart) * (180F / (float)Math.PI);
                GL11.glRotatef(f3, 0.0F, 1.0F, 0.0F);
            }

            if (!flag)
            {
                float f6 = -0.0F * (float)(i - 1) * 0.5F;
                float f4 = -0.0F * (float)(i - 1) * 0.5F;
                float f5 = -0.046875F * (float)(i - 1) * 0.5F;
                GL11.glTranslatef(f6, f4, f5);
            }

            GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
            return i;
        }
    }

    private int getMultiplier(ItemStack stack)
    {
        int i = 1;

        if (stack.getSize() > 48)
        {
            i = 5;
        }
        else if (stack.getSize() > 32)
        {
            i = 4;
        }
        else if (stack.getSize() > 16)
        {
            i = 3;
        }
        else if (stack.getSize() > 1)
        {
            i = 2;
        }

        return i;
    }

    /**
     * Renders the desired {@code T} type Entity.
     */
    public void doRender(EntityItem entity, double x, double y, double z, float partialTicks)
    {
        ItemStack itemstack = entity.getEntityItem();
        this.field_177079_e.setSeed(187L);
//        boolean flag = false;

//        if (
        this.bindEntityTexture(entity);
//        		)
//        {
//            this.renderManager.renderEngine.getTexture(this.getEntityTexture(entity)).unsetMipmap();
//            flag = true;
//        }

        GlState.enableRescaleNormal();
        GlState.alphaFunc(GL11.GL_GREATER, 0.1F);
        GlState.enableBlend();
        GlState.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        GL11.glPushMatrix();
        IBakedModel ibakedmodel = this.itemRenderer.getItemModelMesher().getItemModel(itemstack);
        int i = this.func_177077_a(entity, x, y, z, partialTicks, ibakedmodel);

        for (int j = 0; j < i; ++j)
        {
            if (ibakedmodel.isGui3d())
            {
                GL11.glPushMatrix();

                if (j > 0)
                {
                    float f = (this.field_177079_e.floatv() * 2.0F - 1.0F) * 0.15F;
                    float f1 = (this.field_177079_e.floatv() * 2.0F - 1.0F) * 0.15F;
                    float f2 = (this.field_177079_e.floatv() * 2.0F - 1.0F) * 0.15F;
                    GL11.glTranslatef(f, f1, f2);
                }

                GL11.glScalef(0.5F, 0.5F, 0.5F);
                this.itemRenderer.renderItem(itemstack, ibakedmodel);
                GL11.glPopMatrix();
            }
            else
            {
                GL11.glPushMatrix();
                this.itemRenderer.renderItem(itemstack, ibakedmodel);
                GL11.glPopMatrix();
                GL11.glTranslatef(0.0F, 0.0F, 0.046875F);
            }
        }

        GL11.glPopMatrix();
        GlState.disableRescaleNormal();
        GlState.disableBlend();
        this.bindEntityTexture(entity);

//        if (flag)
//        {
//            this.renderManager.renderEngine.getTexture(this.getEntityTexture(entity)).restoreLastMipmap();
//        }

        super.doRender(entity, x, y, z, partialTicks);
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected String getEntityTexture(EntityItem entity)
    {
        return TextureMap.BLOCKS;
    }

//    protected boolean canRenderName(EntityItem entity)
//    {
//        return entity.getEntityItem().stackSize > 1 || super.canRenderName(entity);
//    }
}
