// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.worldgen.cave.shape.distorted;

import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.procedurallib.logic.GeneralNoise;
import javax.annotation.Nonnull;
import com.hypixel.hytale.math.vector.Vector3d;

public class DistortedPipeShape extends DistortedCylinderShape
{
    private final double compensation;
    
    public DistortedPipeShape(@Nonnull final Vector3d o, @Nonnull final Vector3d v, final double startWidth, final double startHeight, final double midWidth, final double midHeight, final double endWidth, final double endHeight, final double maxWidth, final double maxHeight, final double compensation, final GeneralNoise.InterpolationFunction interpolation) {
        super(o, v, startWidth, startHeight, midWidth, midHeight, endWidth, endHeight, maxWidth, maxHeight, interpolation);
        this.compensation = compensation;
    }
    
    @Override
    public double getWidthAt(final double t) {
        return getCompensatedDim(t, this.startWidth, this.midWidth, this.endWidth, this.compensation, this.interpolation);
    }
    
    @Override
    public double getHeightAt(final double t) {
        return getCompensatedDim(t, this.startHeight, this.midHeight, this.endHeight, this.compensation, this.interpolation);
    }
    
    @Override
    public boolean isValidProjection(final double t) {
        return true;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "DistortedPipeShape{origin=" + String.valueOf(this.o) + ", direction=" + String.valueOf(this.v) + ", startWidth=" + this.startWidth + ", startHeight=" + this.startHeight + ", midWidth=" + this.midWidth + ", midHeight=" + this.midHeight + ", endWidth=" + this.endWidth + ", endHeight=" + this.endHeight;
    }
    
    protected static double getCompensatedDim(double t, final double startDim, final double midDim, final double endDim, final double compensation, @Nonnull final GeneralNoise.InterpolationFunction interpolation) {
        if (t <= 0.0) {
            double fade = 1.0 - MathUtil.clamp(t, -0.5, 0.0) * -2.0;
            fade = interpolation.interpolate(fade);
            return MathUtil.lerp(startDim, startDim * fade, compensation);
        }
        if (t >= 1.0) {
            double fade = 1.0 - MathUtil.clamp(t - 1.0, 0.0, 0.5) * 2.0;
            fade = interpolation.interpolate(fade);
            return MathUtil.lerp(endDim, endDim * fade, compensation);
        }
        if (t <= 0.5) {
            t = interpolation.interpolate(t * 2.0);
            return MathUtil.lerpUnclamped(startDim, midDim, t);
        }
        t = interpolation.interpolate((t - 0.5) * 2.0);
        return MathUtil.lerpUnclamped(midDim, endDim, t);
    }
    
    public static class Factory implements DistortedShape.Factory
    {
        @Nonnull
        @Override
        public DistortedShape create(@Nonnull final Vector3d origin, @Nonnull final Vector3d direction, final double length, final double startWidth, double startHeight, final double midWidth, double midHeight, final double endWidth, double endHeight, final GeneralNoise.InterpolationFunction interpolation) {
            final double compensation = DistortedCylinderShape.getCompensationFactor(direction);
            final double scale = DistortedCylinderShape.getHeightCompensation(compensation);
            startHeight *= scale;
            midHeight *= scale;
            endHeight *= scale;
            final double maxWidth = MathUtil.maxValue(startWidth, midWidth, endWidth);
            final double maxHeight = MathUtil.maxValue(startHeight, midHeight, endHeight);
            return new DistortedPipeShape(origin, direction, startWidth, startHeight, midWidth, midHeight, endWidth, endHeight, maxWidth, maxHeight, compensation, interpolation);
        }
    }
}
