package client.renderer.layers;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.entity.RenderHumanoid;
import client.renderer.model.ModelRenderer;
import common.entity.npc.EntityNPC;
import common.util.ExtMath;

public class LayerCape implements LayerRenderer<EntityNPC>
{
    private final RenderHumanoid renderer;
    private final ModelRenderer bipedCape;
    private final float shift;
    private final int height;

    public LayerCape(RenderHumanoid npcRendererIn, int shift, int height)
    {
        this.renderer = npcRendererIn;
        this.shift = (float)shift; // 0.0F
        this.height = height;
        this.bipedCape = new ModelRenderer(0, 0, 64, 32);
        this.bipedCape.addBox(-5.0F, 0.0F, -1.0F, 10, height, 1, 0.0f);
    }

    public void doRenderLayer(EntityNPC 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)
    {
    	if(/* !entitylivingbaseIn.isInvisible() && */ // entitylivingbaseIn.isWearing(ModelPart.CAPE) 
//    			&& 
    			entitylivingbaseIn.getCape() != null) {
            GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
            this.renderer.bindTexture("textures/npc/cape_" + entitylivingbaseIn.getCape() + ".png");
            GL11.glPushMatrix();
            GL11.glTranslatef(0.0F, 0.0F, 0.125F);
    		if(entitylivingbaseIn.isPlayer()) {
    			EntityNPC player = (EntityNPC)entitylivingbaseIn;
                double d0 = player.prevChasingPosX + (player.chasingPosX - player.prevChasingPosX) * (double)partialTicks - (entitylivingbaseIn.prevX + (entitylivingbaseIn.posX - entitylivingbaseIn.prevX) * (double)partialTicks);
                double d1 = player.prevChasingPosY + (player.chasingPosY - player.prevChasingPosY) * (double)partialTicks - (entitylivingbaseIn.prevY + (entitylivingbaseIn.posY - entitylivingbaseIn.prevY) * (double)partialTicks);
                double d2 = player.prevChasingPosZ + (player.chasingPosZ - player.prevChasingPosZ) * (double)partialTicks - (entitylivingbaseIn.prevZ + (entitylivingbaseIn.posZ - entitylivingbaseIn.prevZ) * (double)partialTicks);
                float f = entitylivingbaseIn.prevYawOffset + (entitylivingbaseIn.yawOffset - entitylivingbaseIn.prevYawOffset) * partialTicks;
                double d3 = (double)ExtMath.sin(f * (float)Math.PI / 180.0F);
                double d4 = (double)(-ExtMath.cos(f * (float)Math.PI / 180.0F));
                float f1 = (float)d1 * 10.0F;
                f1 = ExtMath.clampf(f1, -6.0F, 32.0F);
                float f2 = (float)(d0 * d3 + d2 * d4) * 100.0F;
                float f3 = (float)(d0 * d4 - d2 * d3) * 100.0F;

                if (f2 < 0.0F)
                {
                    f2 = 0.0F;
                }

                float f4 = player.prevCameraYaw + (player.cameraYaw - player.prevCameraYaw) * partialTicks;
                f1 = f1 + ExtMath.sin((entitylivingbaseIn.prevWalkDistMod + (entitylivingbaseIn.walkDistMod - entitylivingbaseIn.prevWalkDistMod) * partialTicks) * 6.0F) * 32.0F * f4;

                if (entitylivingbaseIn.isSneakingVisually())
                {
                    f1 += 25.0F;
                }

                GL11.glRotatef(6.0F + f2 / 2.0F + f1, 1.0F, 0.0F, 0.0F);
                GL11.glRotatef(f3 / 2.0F, 0.0F, 0.0F, 1.0F);
                GL11.glRotatef(-f3 / 2.0F, 0.0F, 1.0F, 0.0F);
            }
    		else {
	        	GL11.glRotatef(entitylivingbaseIn.isSneakingVisually() ? 25.0F : 5.0F, 1.0F, 0.0F, 0.0F);
	        }
            GL11.glRotatef(180.0F, 0.0F, 1.0F, 0.0F);
            this.bipedCape.rotationPointY = this.shift + (entitylivingbaseIn.isSneakingVisually() ? 2.0f : 0.0f);
            this.bipedCape.render(0.0625F);
            GL11.glPopMatrix();
    	}
    }

    public boolean shouldCombineTextures()
    {
        return false;
    }
}
