package com.minelittlepony.model.components;

import net.minecraft.client.model.ModelBase;
import net.minecraft.util.math.MathHelper;

import static com.minelittlepony.model.PonyModelConstants.*;

import com.minelittlepony.model.AbstractPonyModel;
import com.minelittlepony.pony.data.TailLengths;
import com.minelittlepony.render.plane.PlaneRenderer;

public class PonyTail extends PlaneRenderer {
    
    private final TailSegment[] segments = new TailSegment[4];
    
    private final AbstractPonyModel theModel;
    
    public PonyTail(AbstractPonyModel model) {
        super(model);
        theModel = model;
    }
    
    public void init(float yOffset, float stretch) {
        for (int i = 0; i < segments.length; i++) {
            addChild(segments[i] = new TailSegment(theModel, i, yOffset, stretch));
        }
    }
    
    public void setRotationAndAngles(boolean rainboom, float limbSwing, float limbSwingAmount, float bodySwing, float ticks) {
        swingZ(rainboom, limbSwing, limbSwingAmount);
        rotateAngleY = bodySwing;
        
        if (theModel.isSneak && !theModel.isFlying && !rainboom) {
            rotateSneak();
        } else if (theModel.isRiding) {
            rotationPointZ = 13;
            rotationPointY = 3;
            rotateAngleX = (float) (Math.PI * 0.2);
        } else {
            setRotationPoint(TAIL_RP_X, TAIL_RP_Y, TAIL_RP_Z_NOTSNEAK);
            if (rainboom) {
                rotateAngleX = ROTATE_90 + 0.1F * MathHelper.sin(limbSwing);
            } else {
                rotateAngleX = 0.5F * limbSwingAmount;
            }

            if (!rainboom) {
                swingX(ticks);
            }
        }

        if (rainboom) {
            rotationPointY += 6.0F;
            rotationPointZ++;
        }
    }

    public void swingZ(boolean rainboom, float move, float swing) {
        rotateAngleZ = rainboom ? 0 : MathHelper.cos(move * 0.8F) * 0.2F * swing;
    }

    public void swingX(float tick) {
        float sinTickFactor = MathHelper.sin(tick * 0.067F) * 0.05F;
        rotateAngleX += sinTickFactor;
        rotateAngleY += sinTickFactor;
    }
    
    public void rotateSneak() {
        setRotationPoint(TAIL_RP_X, TAIL_RP_Y, TAIL_RP_Z_SNEAK);
        rotateAngleX = -BODY_ROTATE_ANGLE_X_SNEAK + 0.1F;
    }

    public void render(TailLengths tail, float scale) {
        int tailStop = tail.ordinal();

        for (int i = 0; i < segments.length; i++) {
            segments[i].isHidden = i >= tailStop;
        }

        super.render(scale);
    }
    
    private class TailSegment extends PlaneRenderer {
        
        public TailSegment(ModelBase model, int index, float yOffset, float stretch) {
            super(model);
            
            this.offsetY = ((float)index)/4 + 0.063f;
            
            init(index, yOffset, stretch);
        }
        
        public void init(int index, float yOffset, float stretch) {
            int texX = (index % 2) * 4;
            
            if (index == 0) {
                setTextureOffset(32, 0).addTopPlane(-2, 0, 2, 4, 4, stretch);
            }
            
            setTextureOffset(36, texX).addEastPlane(2, 0, 2, 4, 4, stretch);
            addWestPlane(-2, 0, 2, 4, 4, stretch);
            setTextureOffset(32, texX).addBackPlane(-2, 0, 2, 4, 4, stretch);
            addFrontPlane(-2, 0, 6, 4, 4, stretch);
            setTextureOffset(32, 0).addBottomPlane(-2, 4, 2, 4, 4, stretch);
            
            setRotationPoint(TAIL_RP_X, TAIL_RP_Y + yOffset, TAIL_RP_Z);
        }
    }
}

@@ -1285,31 +1285,14 @@
 X = 
-(float) (Math.PI * 0.2)
+PI / 5
 ;%0A  
@@ -1452,15 +1452,8 @@
 90 +
- 0.1F *
  Mat
@@ -1474,16 +1474,21 @@
 mbSwing)
+ / 10
 ;%0A      
@@ -1536,15 +1536,8 @@
 eX =
- 0.5F *
  lim
@@ -1548,16 +1548,20 @@
 ngAmount
+ / 2
 ;%0A      
@@ -1713,11 +1713,8 @@
 += 6
-.0F
 ;%0A  
@@ -1897,17 +1897,17 @@
 F) * 0.2
-F
+f
  * swing
@@ -2013,17 +2013,17 @@
 .067
-F
+f
 ) * 0.05
 F;%0A 
@@ -2018,17 +2018,17 @@
 ) * 0.05
-F
+f
 ;%0A      
@@ -3006,32 +3006,19 @@
         
-setTextureOffset
+tex
 (32, 0).
@@ -3098,34 +3098,101 @@
     
-setTextureOffset(36, texX)
+around(TAIL_RP_X, TAIL_RP_Y + yOffset, TAIL_RP_Z);%0A            tex(36, texX)%0A                
 .add
@@ -3216,33 +3216,32 @@
 , 4, 4, stretch)
-;
 %0A            add
@@ -3237,16 +3237,21 @@
         
+    .
 addWestP
@@ -3297,34 +3297,38 @@
     
-setTextureOffset(32, texX)
+tex(32, texX)%0A                
 .add
@@ -3353,34 +3353,34 @@
 , 4, 4, stretch)
-;
 %0A
+ 
             addF
@@ -3375,16 +3375,20 @@
         
+   .
 addFront
@@ -3435,31 +3435,35 @@
     
-setTextureOffset(32, 0)
+tex(32, 0)%0A                
 .add
@@ -3504,94 +3504,8 @@
 h);%0A
-            %0A            setRotationPoint(TAIL_RP_X, TAIL_RP_Y + yOffset, TAIL_RP_Z);%0A
     

Tidy up ponytails