package client.renderer.model;

import org.lwjgl.opengl.GL11;

import common.entity.Entity;
import common.util.ExtMath;

public class ModelHumanoid extends ModelBiped
{
    public ModelRenderer bipedLeftArmwear;
    public ModelRenderer bipedRightArmwear;
    public ModelRenderer bipedLeftLegwear;
    public ModelRenderer bipedRightLegwear;
    public ModelRenderer bipedBodyWear;

    public ModelRenderer slimLeftArmwear;
    public ModelRenderer slimRightArmwear;
    public ModelRenderer slimLeftArm;
    public ModelRenderer slimRightArm;
    
    private boolean slim;
    public boolean raisedArms;

//    public ModelHumanoid()
//    {
//    	this(64, 64);
//    }

//    public ModelHumanoid(int x, int y)
//    {
//    	this(x, y, 12, 12);
//    }

    public ModelHumanoid(int x, int y, int arms, int legs)
    {
        super(x, y, arms, legs);
//        if(legs != 12 || arms != 12) {
        this.bipedHead = new ModelRenderer(this, 0, 0);
        this.bipedHead.addBox(-4.0F, -8.0F, -4.0F, 8, 8, 8, 0.0F);
        this.bipedHead.setRotationPoint(0.0F, 24.0f - arms - legs, 0.0F);
        this.bipedHeadwear = new ModelRenderer(this, 32, 0);
        this.bipedHeadwear.addBox(-4.0F, -8.0F, -4.0F, 8, 8, 8, 0.5F);
        this.bipedHeadwear.setRotationPoint(0.0F, 24.0f - arms - legs, 0.0F);
        
        this.bipedRightArm = new ModelRenderer(this, 0, 16);
        this.bipedRightArm.addBox(-3.0F, -2.0F, -2.0F, 4, arms, 4, 0.0F);
        this.bipedRightArm.setRotationPoint(-5.0F, 26.0f - arms - legs, 0.0F);
        this.slimRightArm = new ModelRenderer(this, 0, 16);
        this.slimRightArm.addBox(-2.0F, -2.0F, -2.0F, 3, arms, 4, -4, arms, 4, 0.0f);
        this.slimRightArm.setRotationPoint(-5.0F, 26.5f - arms - legs, 0.0F);
        this.bipedBody = new ModelRenderer(this, 16, 16);
        this.bipedBody.addBox(-4.0F, 0.0F, -2.0F, 8, arms, 4, 0.0F);
        this.bipedBody.setRotationPoint(0.0F, 24.0f - arms - legs, 0.0F);
        this.bipedLeftArm = new ModelRenderer(this, 40, 16);
        this.bipedLeftArm.addBox(-1.0F, -2.0F, -2.0F, 4, arms, 4, 0.0f);
        this.bipedLeftArm.setRotationPoint(5.0F, 26.0f - arms - legs, 0.0F);
        this.slimLeftArm = new ModelRenderer(this, 40, 16);
        this.slimLeftArm.addBox(-1.0F, -2.0F, -2.0F, 3, arms, 4, 4, arms, 4, 0.0f);
        this.slimLeftArm.setRotationPoint(5.0F, 26.5f - arms - legs, 0.0F);

        this.bipedRightArmwear = new ModelRenderer(this, 0, 16 + 4 + arms);
        this.bipedRightArmwear.addBox(-3.0F, -2.0F, -2.0F, 4, arms, 4, 0.25F);
        this.bipedRightArmwear.setRotationPoint(-5.0F, 26.0f - arms - legs, 10.0F);
        this.slimRightArmwear = new ModelRenderer(this, 0, 16 + 4 + arms);
        this.slimRightArmwear.addBox(-2.0F, -2.0F, -2.0F, 3, arms, 4, -4, arms, 4, 0.25F);
        this.slimRightArmwear.setRotationPoint(-5.0F, 26.5f - arms - legs, 10.0F);
        this.bipedBodyWear = new ModelRenderer(this, 16, 16 + 4 + arms);
        this.bipedBodyWear.addBox(-4.0F, 0.0F, -2.0F, 8, arms, 4, 0.25F);
        this.bipedBodyWear.setRotationPoint(0.0F, 24.0f - arms - legs, 0.0F);
        this.bipedLeftArmwear = new ModelRenderer(this, 40, 16 + 4 + arms);
        this.bipedLeftArmwear.addBox(-1.0F, -2.0F, -2.0F, 4, arms, 4, 0.25F);
        this.bipedLeftArmwear.setRotationPoint(5.0F, 26.0f - arms - legs, 0.0F);
        this.slimLeftArmwear = new ModelRenderer(this, 40, 16 + 4 + arms);
        this.slimLeftArmwear.addBox(-1.0F, -2.0F, -2.0F, 3, arms, 4, 4, arms, 4, 0.25F);
        this.slimLeftArmwear.setRotationPoint(5.0F, 26.5f - arms - legs, 0.0F);
        
        this.bipedRightLegwear = new ModelRenderer(this, 0, 16 + (4 + arms) * 2);
        this.bipedRightLegwear.addBox(-2.0F, 0.0F, -2.0F, 4, legs, 4, 0.25F);
        this.bipedRightLegwear.setRotationPoint(-1.9F, 24.0f - legs, 0.0F);
        this.bipedRightLeg = new ModelRenderer(this, 16, 16 + (4 + arms) * 2);
        this.bipedRightLeg.addBox(-2.0F, 0.0F, -2.0F, 4, legs, 4, 0.0F);
        this.bipedRightLeg.setRotationPoint(-1.9F, 24.0f - legs, 0.0F);
        this.bipedLeftLeg = new ModelRenderer(this, 32, 16 + (4 + arms) * 2);
        this.bipedLeftLeg.addBox(-2.0F, 0.0F, -2.0F, 4, legs, 4, 0.0f);
        this.bipedLeftLeg.setRotationPoint(1.9F, 24.0f - legs, 0.0F);
        this.bipedLeftLegwear = new ModelRenderer(this, 48, 16 + (4 + arms) * 2);
        this.bipedLeftLegwear.addBox(-2.0F, 0.0F, -2.0F, 4, legs, 4, 0.25F);
        this.bipedLeftLegwear.setRotationPoint(1.9F, 24.0f - legs, 0.0F);
//        }
//        else {
//            this.bipedHead = new ModelRenderer(this, 0, 0);
//            this.bipedHead.addBox(-4.0F, -8.0F, -4.0F, 8, 8, 8, 0.0F);
//            this.bipedHead.setRotationPoint(0.0F, 0.0F, 0.0F);
//            this.bipedHeadwear = new ModelRenderer(this, 32, 0);
//            this.bipedHeadwear.addBox(-4.0F, -8.0F, -4.0F, 8, 8, 8, 0.5F);
//            this.bipedHeadwear.setRotationPoint(0.0F, 0.0F, 0.0F);
//            this.bipedBody = new ModelRenderer(this, 16, 16);
//            this.bipedBody.addBox(-4.0F, 0.0F, -2.0F, 8, 12, 4, 0.0F);
//            this.bipedBody.setRotationPoint(0.0F, 0.0F, 0.0F);
//            this.bipedBodyWear = new ModelRenderer(this, 16, 32);
//            this.bipedBodyWear.addBox(-4.0F, 0.0F, -2.0F, 8, 12, 4, 0.25F);
//            this.bipedBodyWear.setRotationPoint(0.0F, 0.0F, 0.0F);
//            this.bipedRightArm = new ModelRenderer(this, 40, 16);
//            this.bipedRightArm.addBox(-3.0F, -2.0F, -2.0F, 4, 12, 4, 0.0F);
//            this.bipedRightArm.setRotationPoint(-5.0F, 2.0F, 0.0F);
//            this.bipedRightLeg = new ModelRenderer(this, 0, 16);
//            this.bipedRightLeg.addBox(-2.0F, 0.0F, -2.0F, 4, 12, 4, 0.0F);
//            this.bipedRightLeg.setRotationPoint(-1.9F, 12.0F, 0.0F);
//            this.bipedLeftArm = new ModelRenderer(this, 32, 48);
//            this.bipedLeftArm.addBox(-1.0F, -2.0F, -2.0F, 4, 12, 4, 0.0f);
//            this.bipedLeftArm.setRotationPoint(5.0F, 2.0F, 0.0F);
//            this.bipedLeftArmwear = new ModelRenderer(this, 48, 48);
//            this.bipedLeftArmwear.addBox(-1.0F, -2.0F, -2.0F, 4, 12, 4, 0.25F);
//            this.bipedLeftArmwear.setRotationPoint(5.0F, 2.0F, 0.0F);
//            this.bipedRightArmwear = new ModelRenderer(this, 40, 32);
//            this.bipedRightArmwear.addBox(-3.0F, -2.0F, -2.0F, 4, 12, 4, 0.25F);
//            this.bipedRightArmwear.setRotationPoint(-5.0F, 2.0F, 10.0F);
//	        this.bipedLeftLeg = new ModelRenderer(this, 16, 48);
//	        this.bipedLeftLeg.addBox(-2.0F, 0.0F, -2.0F, 4, 12, 4, 0.0f);
//	        this.bipedLeftLeg.setRotationPoint(1.9F, 12.0F, 0.0F);
//	        this.bipedLeftLegwear = new ModelRenderer(this, 0, 48);
//	        this.bipedLeftLegwear.addBox(-2.0F, 0.0F, -2.0F, 4, 12, 4, 0.25F);
//	        this.bipedLeftLegwear.setRotationPoint(1.9F, 12.0F, 0.0F);
//	        this.bipedRightLegwear = new ModelRenderer(this, 0, 32);
//	        this.bipedRightLegwear.addBox(-2.0F, 0.0F, -2.0F, 4, 12, 4, 0.25F);
//	        this.bipedRightLegwear.setRotationPoint(-1.9F, 12.0F, 0.0F);
//	        this.slimRightArm = new ModelRenderer(this, 40, 16);
//	        this.slimRightArm.addBox(-2.0F, -2.0F, -2.0F, 3, 12, 4, -4, 12, 4, 0.0f);
//	        this.slimRightArm.setRotationPoint(-5.0F, 2.5F, 0.0F);
//	        this.slimRightArmwear = new ModelRenderer(this, 40, 32);
//	        this.slimRightArmwear.addBox(-2.0F, -2.0F, -2.0F, 3, 12, 4, -4, 12, 4, 0.25F);
//	        this.slimRightArmwear.setRotationPoint(-5.0F, 2.5F, 10.0F);
//	        this.slimLeftArm = new ModelRenderer(this, 32, 48);
//	        this.slimLeftArm.addBox(-1.0F, -2.0F, -2.0F, 3, 12, 4, 4, 12, 4, 0.0f);
//	        this.slimLeftArm.setRotationPoint(5.0F, 2.5F, 0.0F);
//	        this.slimLeftArmwear = new ModelRenderer(this, 48, 48);
//	        this.slimLeftArmwear.addBox(-1.0F, -2.0F, -2.0F, 3, 12, 4, 4, 12, 4, 0.25F);
//	        this.slimLeftArmwear.setRotationPoint(5.0F, 2.5F, 0.0F);
//        }
    }

//    public ModelRenderer getRandomModelBox(Random rand)
//    {
//        return this.boxList.get(1 + rand.zrange(this.boxList.size() - (1 + this.powerRods.length)));
//    }

    /**
     * Sets the models various rotation angles then renders the model.
     */
    public void render(Entity entityIn, float p_78088_2_, float p_78088_3_, float p_78088_4_, float p_78088_5_, float p_78088_6_, float scale)
    {
        super.render(entityIn, p_78088_2_, p_78088_3_, p_78088_4_, p_78088_5_, p_78088_6_, scale);
        GL11.glPushMatrix();

//        if (this.isChild)
//        {
//            float f = 2.0F;
//            SKC.glScalef(1.0F / f, 1.0F / f, 1.0F / f);
//            SKC.glTranslatef(0.0F, 24.0F * scale, 0.0F);
////            this.slimLeftArm.render(scale);
////            this.slimRightArm.render(scale);
////            this.bipedLeftLegwear.render(scale);
////            this.bipedRightLegwear.render(scale);
////            this.bipedLeftArmwear.render(scale);
////            this.bipedRightArmwear.render(scale);
////            this.slimLeftArmwear.render(scale);
////            this.slimRightArmwear.render(scale);
////            this.bipedBodyWear.render(scale);
//        }
//        else
//        {
        if (this.isSneak) // entityIn.isSneaking())
        {
            GL11.glTranslatef(0.0F, 0.2F, 0.0F);
//            }
        }

        this.slimLeftArm.render(scale);
        this.slimRightArm.render(scale);
        this.bipedLeftLegwear.render(scale);
        this.bipedRightLegwear.render(scale);
        this.bipedLeftArmwear.render(scale);
        this.slimLeftArmwear.render(scale);
        this.slimRightArmwear.render(scale);
        this.bipedRightArmwear.render(scale);
        this.bipedBodyWear.render(scale);
        
//        if(this.sticks) {
//        }

        GL11.glPopMatrix();
    }

//    public void renderCape(float scale)
//    {
//        this.bipedCape.render(scale);
//    }

//    public void renderRods(float scale, float ageInTicks)
//    {
//    }

    /**
     * Sets the model's various rotation angles. For bipeds, par1 and par2 are used for animating the movement of arms
     * and legs, where par1 represents the time(so that arms and legs swing back and forth) and par2 represents how
     * "far" arms and legs can swing at most.
     */
    public void setRotationAngles(float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scaleFactor, Entity entityIn)
    {
        super.setRotationAngles(limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor, entityIn);
        
        if(this.raisedArms) {
            float f = ExtMath.sin(this.swingProgress * (float)Math.PI);
            float f1 = ExtMath.sin((1.0F - (1.0F - this.swingProgress) * (1.0F - this.swingProgress)) * (float)Math.PI);
            this.bipedRightArm.rotateAngleZ = 0.0F;
            this.bipedLeftArm.rotateAngleZ = 0.0F;
            this.bipedRightArm.rotateAngleY = -(0.1F - f * 0.6F);
            this.bipedLeftArm.rotateAngleY = 0.1F - f * 0.6F;
            this.bipedRightArm.rotateAngleX = -((float)Math.PI / 2F);
            this.bipedLeftArm.rotateAngleX = -((float)Math.PI / 2F);
            this.bipedRightArm.rotateAngleX -= f * 1.2F - f1 * 0.4F;
            this.bipedLeftArm.rotateAngleX -= f * 1.2F - f1 * 0.4F;
            this.bipedRightArm.rotateAngleZ += ExtMath.cos(ageInTicks * 0.09F) * 0.05F + 0.05F;
            this.bipedLeftArm.rotateAngleZ -= ExtMath.cos(ageInTicks * 0.09F) * 0.05F + 0.05F;
            this.bipedRightArm.rotateAngleX += ExtMath.sin(ageInTicks * 0.067F) * 0.05F;
            this.bipedLeftArm.rotateAngleX -= ExtMath.sin(ageInTicks * 0.067F) * 0.05F;
        }
//        else {
//        	  this.bipedRightArm.rotateAngleZ = 0.0F;
//            this.bipedLeftArm.rotateAngleZ = 0.0F;
//            this.bipedRightArm.rotateAngleY = 0.0F;
//            this.bipedLeftArm.rotateAngleY = 0.0F;
//            this.bipedRightArm.rotateAngleX = 0.0F;
//            this.bipedLeftArm.rotateAngleX = 0.0F;
//        }
        copyModelAngles(this.bipedLeftLeg, this.bipedLeftLegwear);
        copyModelAngles(this.bipedRightLeg, this.bipedRightLegwear);
        copyModelAngles(this.bipedLeftArm, this.bipedLeftArmwear);
        copyModelAngles(this.bipedRightArm, this.bipedRightArmwear);
        copyModelAngles(this.bipedLeftArm, this.slimLeftArmwear);
        copyModelAngles(this.bipedRightArm, this.slimRightArmwear);
        copyModelAngles(this.bipedLeftArm, this.slimLeftArm);
        copyModelAngles(this.bipedRightArm, this.slimRightArm);
        copyModelAngles(this.bipedBody, this.bipedBodyWear);

//        if (this.isSneak) // entityIn.isSneaking())
//        {
//            this.bipedCape.rotationPointY = 2.0F;
//        }
//        else
//        {
//            this.bipedCape.rotationPointY = 0.0F;
//        }
        
//        if(this.sticks) {
//        }
    }
    
    private void shiftRender(ModelRenderer model, float shift) {
    	float orig = model.rotationPointY;
    	model.rotationPointY += shift;
        model.render(0.0625F);
        model.rotationPointY = orig;
    }

    public void renderRightArm(float shift)
    {
        this.shiftRender(this.bipedRightArm, shift);
        this.shiftRender(this.slimRightArm, shift);
        this.shiftRender(this.bipedRightArmwear, shift);
        this.shiftRender(this.slimRightArmwear, shift);
    }

//    public void renderLeftArm()
//    {
//        this.bipedLeftArm.render(0.0625F);
//        this.slimLeftArm.render(0.0625F);
//        this.bipedLeftArmwear.render(0.0625F);
//        this.slimLeftArmwear.render(0.0625F);
//    }
    
    public void setSlim(boolean slim) {
    	this.slim = slim;
    }

    public void setVisible(boolean vis)
    {
        super.setVisible(vis);
        
        this.bipedLeftArm.showModel = vis && !this.slim;
        this.bipedRightArm.showModel = vis && !this.slim;
        this.bipedLeftArmwear.showModel = vis && !this.slim;
        this.bipedRightArmwear.showModel = vis && !this.slim;

        this.slimLeftArm.showModel = vis && this.slim;
        this.slimRightArm.showModel = vis && this.slim;
        this.slimLeftArmwear.showModel = vis && this.slim;
        this.slimRightArmwear.showModel = vis && this.slim;
        
        this.bipedLeftLegwear.showModel = vis;
        this.bipedRightLegwear.showModel = vis;
        this.bipedBodyWear.showModel = vis;
//        this.bipedCape.showModel = vis;
    }

    public void postRenderArm(float scale)
    {
        this.bipedRightArm.postRender(scale);
        this.slimRightArm.postRender(scale);
    }
}
