package client.renderer.layers;

import org.lwjgl.opengl.GL11;

import client.renderer.ItemRenderer;
import client.renderer.entity.RendererLivingEntity;
import client.renderer.model.ModelBox;
import client.renderer.model.ModelRenderer;
import common.entity.Entity;
import common.entity.projectile.EntityArrow;
import common.entity.types.EntityLiving;
import common.rng.Random;
import common.util.ExtMath;

public class LayerArrow implements LayerRenderer<EntityLiving>
{
    private final RendererLivingEntity field_177168_a;

    public LayerArrow(RendererLivingEntity p_i46124_1_)
    {
        this.field_177168_a = p_i46124_1_;
    }

    public void doRenderLayer(EntityLiving entitylivingbaseIn, float p_177141_2_, float p_177141_3_, float partialTicks, float p_177141_5_, float p_177141_6_, float p_177141_7_, float scale)
    {
        int i = entitylivingbaseIn.getArrowCountInEntity();

        if (i > 0)
        {
            Entity entity = new EntityArrow(entitylivingbaseIn.worldObj, entitylivingbaseIn.posX, entitylivingbaseIn.posY, entitylivingbaseIn.posZ);
            Random random = new Random((long)entitylivingbaseIn.getId());
            ItemRenderer.disableStandardItemLighting();

            for (int j = 0; j < i; ++j)
            {
                GL11.glPushMatrix();
                ModelRenderer modelrenderer = this.field_177168_a.getMainModel().getRandomModelBox(random);
                ModelBox modelbox = (ModelBox)modelrenderer.cubeList.get(random.zrange(modelrenderer.cubeList.size()));
                modelrenderer.postRender(0.0625F);
                float f = random.floatv();
                float f1 = random.floatv();
                float f2 = random.floatv();
                float f3 = (modelbox.posX1 + (modelbox.posX2 - modelbox.posX1) * f) / 16.0F;
                float f4 = (modelbox.posY1 + (modelbox.posY2 - modelbox.posY1) * f1) / 16.0F;
                float f5 = (modelbox.posZ1 + (modelbox.posZ2 - modelbox.posZ1) * f2) / 16.0F;
                GL11.glTranslatef(f3, f4, f5);
                f = f * 2.0F - 1.0F;
                f1 = f1 * 2.0F - 1.0F;
                f2 = f2 * 2.0F - 1.0F;
                f = f * -1.0F;
                f1 = f1 * -1.0F;
                f2 = f2 * -1.0F;
                float f6 = ExtMath.sqrtf(f * f + f2 * f2);
                entity.prevYaw = entity.rotYaw = (float)(Math.atan2((double)f, (double)f2) * 180.0D / Math.PI);
                entity.prevPitch = entity.rotPitch = (float)(Math.atan2((double)f1, (double)f6) * 180.0D / Math.PI);
                double d0 = 0.0D;
                double d1 = 0.0D;
                double d2 = 0.0D;
                this.field_177168_a.getRenderManager().renderEntity(entity, d0, d1, d2, partialTicks);
                GL11.glPopMatrix();
            }

            ItemRenderer.enableStandardItemLighting();
        }
    }

    public boolean shouldCombineTextures()
    {
        return false;
    }
}
