package client.renderer.model;

import org.lwjgl.opengl.GL11;

import common.entity.Entity;
import common.entity.animal.EntityCat;
import common.entity.types.EntityLiving;
import common.util.ExtMath;

public class ModelCat extends ModelBase
{
    ModelRenderer backLeftLeg;
    ModelRenderer backRightLeg;
    ModelRenderer frontLeftLeg;
    ModelRenderer frontRightLeg;
    ModelRenderer tail;
    ModelRenderer tail2;
    ModelRenderer head;
    ModelRenderer body;
    int field_78163_i = 1;

    public ModelCat()
    {
        this.setTextureOffset("head.main", 0, 0);
        this.setTextureOffset("head.nose", 0, 24);
        this.setTextureOffset("head.ear1", 0, 10);
        this.setTextureOffset("head.ear2", 6, 10);
        this.head = new ModelRenderer(this, "head");
        this.head.addBox("main", -2.5F, -2.0F, -3.0F, 5, 4, 5);
        this.head.addBox("nose", -1.5F, 0.0F, -4.0F, 3, 2, 2);
        this.head.addBox("ear1", -2.0F, -3.0F, 0.0F, 1, 1, 2);
        this.head.addBox("ear2", 1.0F, -3.0F, 0.0F, 1, 1, 2);
        this.head.setRotationPoint(0.0F, 15.0F, -9.0F);
        this.body = new ModelRenderer(this, 20, 0);
        this.body.addBox(-2.0F, 3.0F, -8.0F, 4, 16, 6, 0.0F);
        this.body.setRotationPoint(0.0F, 12.0F, -10.0F);
        this.tail = new ModelRenderer(this, 0, 15);
        this.tail.addBox(-0.5F, 0.0F, 0.0F, 1, 8, 1);
        this.tail.rotateAngleX = 0.9F;
        this.tail.setRotationPoint(0.0F, 15.0F, 8.0F);
        this.tail2 = new ModelRenderer(this, 4, 15);
        this.tail2.addBox(-0.5F, 0.0F, 0.0F, 1, 8, 1);
        this.tail2.setRotationPoint(0.0F, 20.0F, 14.0F);
        this.backLeftLeg = new ModelRenderer(this, 8, 13);
        this.backLeftLeg.addBox(-1.0F, 0.0F, 1.0F, 2, 6, 2);
        this.backLeftLeg.setRotationPoint(1.1F, 18.0F, 5.0F);
        this.backRightLeg = new ModelRenderer(this, 8, 13);
        this.backRightLeg.addBox(-1.0F, 0.0F, 1.0F, 2, 6, 2);
        this.backRightLeg.setRotationPoint(-1.1F, 18.0F, 5.0F);
        this.frontLeftLeg = new ModelRenderer(this, 40, 0);
        this.frontLeftLeg.addBox(-1.0F, 0.0F, 0.0F, 2, 10, 2);
        this.frontLeftLeg.setRotationPoint(1.2F, 13.8F, -5.0F);
        this.frontRightLeg = new ModelRenderer(this, 40, 0);
        this.frontRightLeg.addBox(-1.0F, 0.0F, 0.0F, 2, 10, 2);
        this.frontRightLeg.setRotationPoint(-1.2F, 13.8F, -5.0F);
    }

    /**
     * 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)
    {
        this.setRotationAngles(p_78088_2_, p_78088_3_, p_78088_4_, p_78088_5_, p_78088_6_, scale, entityIn);

        if (this.isChild)
        {
            float f = 2.0F;
            GL11.glPushMatrix();
            GL11.glScalef(1.5F / f, 1.5F / f, 1.5F / f);
            GL11.glTranslatef(0.0F, 10.0F * scale, 4.0F * scale);
            this.head.render(scale);
            GL11.glPopMatrix();
            GL11.glPushMatrix();
            GL11.glScalef(1.0F / f, 1.0F / f, 1.0F / f);
            GL11.glTranslatef(0.0F, 24.0F * scale, 0.0F);
            this.body.render(scale);
            this.backLeftLeg.render(scale);
            this.backRightLeg.render(scale);
            this.frontLeftLeg.render(scale);
            this.frontRightLeg.render(scale);
            this.tail.render(scale);
            this.tail2.render(scale);
            GL11.glPopMatrix();
        }
        else
        {
            this.head.render(scale);
            this.body.render(scale);
            this.tail.render(scale);
            this.tail2.render(scale);
            this.backLeftLeg.render(scale);
            this.backRightLeg.render(scale);
            this.frontLeftLeg.render(scale);
            this.frontRightLeg.render(scale);
        }
    }

    /**
     * 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)
    {
        this.head.rotateAngleX = headPitch / (180F / (float)Math.PI);
        this.head.rotateAngleY = netHeadYaw / (180F / (float)Math.PI);

        if (this.field_78163_i != 3)
        {
            this.body.rotateAngleX = ((float)Math.PI / 2F);

            if (this.field_78163_i == 2)
            {
                this.backLeftLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F) * 1.0F * limbSwingAmount;
                this.backRightLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F + 0.3F) * 1.0F * limbSwingAmount;
                this.frontLeftLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F + (float)Math.PI + 0.3F) * 1.0F * limbSwingAmount;
                this.frontRightLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F + (float)Math.PI) * 1.0F * limbSwingAmount;
                this.tail2.rotateAngleX = 1.7278761F + ((float)Math.PI / 10F) * ExtMath.cos(limbSwing) * limbSwingAmount;
            }
            else
            {
                this.backLeftLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F) * 1.0F * limbSwingAmount;
                this.backRightLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F + (float)Math.PI) * 1.0F * limbSwingAmount;
                this.frontLeftLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F + (float)Math.PI) * 1.0F * limbSwingAmount;
                this.frontRightLeg.rotateAngleX = ExtMath.cos(limbSwing * 0.6662F) * 1.0F * limbSwingAmount;

                if (this.field_78163_i == 1)
                {
                    this.tail2.rotateAngleX = 1.7278761F + ((float)Math.PI / 4F) * ExtMath.cos(limbSwing) * limbSwingAmount;
                }
                else
                {
                    this.tail2.rotateAngleX = 1.7278761F + 0.47123894F * ExtMath.cos(limbSwing) * limbSwingAmount;
                }
            }
        }
    }

    /**
     * Used for easily adding entity-dependent animations. The second and third float params here are the same second
     * and third as in the setRotationAngles method.
     */
    public void setLivingAnimations(EntityLiving entitylivingbaseIn, float p_78086_2_, float p_78086_3_, float partialTickTime)
    {
        EntityCat cat = (EntityCat)entitylivingbaseIn;
        this.body.rotationPointY = 12.0F;
        this.body.rotationPointZ = -10.0F;
        this.head.rotationPointY = 15.0F;
        this.head.rotationPointZ = -9.0F;
        this.tail.rotationPointY = 15.0F;
        this.tail.rotationPointZ = 8.0F;
        this.tail2.rotationPointY = 20.0F;
        this.tail2.rotationPointZ = 14.0F;
        this.frontLeftLeg.rotationPointY = this.frontRightLeg.rotationPointY = 13.8F;
        this.frontLeftLeg.rotationPointZ = this.frontRightLeg.rotationPointZ = -5.0F;
        this.backLeftLeg.rotationPointY = this.backRightLeg.rotationPointY = 18.0F;
        this.backLeftLeg.rotationPointZ = this.backRightLeg.rotationPointZ = 5.0F;
        this.tail.rotateAngleX = 0.9F;

        if (cat.isSneakingVisually())
        {
            ++this.body.rotationPointY;
            this.head.rotationPointY += 2.0F;
            ++this.tail.rotationPointY;
            this.tail2.rotationPointY += -4.0F;
            this.tail2.rotationPointZ += 2.0F;
            this.tail.rotateAngleX = ((float)Math.PI / 2F);
            this.tail2.rotateAngleX = ((float)Math.PI / 2F);
            this.field_78163_i = 0;
        }
        else if (cat.isSprinting())
        {
            this.tail2.rotationPointY = this.tail.rotationPointY;
            this.tail2.rotationPointZ += 2.0F;
            this.tail.rotateAngleX = ((float)Math.PI / 2F);
            this.tail2.rotateAngleX = ((float)Math.PI / 2F);
            this.field_78163_i = 2;
        }
        else if (cat.isSitting())
        {
            this.body.rotateAngleX = ((float)Math.PI / 4F);
            this.body.rotationPointY += -4.0F;
            this.body.rotationPointZ += 5.0F;
            this.head.rotationPointY += -3.3F;
            ++this.head.rotationPointZ;
            this.tail.rotationPointY += 8.0F;
            this.tail.rotationPointZ += -2.0F;
            this.tail2.rotationPointY += 2.0F;
            this.tail2.rotationPointZ += -0.8F;
            this.tail.rotateAngleX = 1.7278761F;
            this.tail2.rotateAngleX = 2.670354F;
            this.frontLeftLeg.rotateAngleX = this.frontRightLeg.rotateAngleX = -0.15707964F;
            this.frontLeftLeg.rotationPointY = this.frontRightLeg.rotationPointY = 15.8F;
            this.frontLeftLeg.rotationPointZ = this.frontRightLeg.rotationPointZ = -7.0F;
            this.backLeftLeg.rotateAngleX = this.backRightLeg.rotateAngleX = -((float)Math.PI / 2F);
            this.backLeftLeg.rotationPointY = this.backRightLeg.rotationPointY = 21.0F;
            this.backLeftLeg.rotationPointZ = this.backRightLeg.rotationPointZ = 1.0F;
            this.field_78163_i = 3;
        }
        else
        {
            this.field_78163_i = 1;
        }
    }
}
