package de.frajul.endlessroll.entities.particles.attributes.attributeValues;

import de.frajul.endlessroll.data.Color;
import de.frajul.endlessroll.main.GameLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Julian on 02.08.2016.
 */
public class TintTimeline extends ParticleAttributeValue {

    private List<TintTimelinePoint> points = new ArrayList<>();

    public TintTimeline() {
        super(ParticleAttributeValueType.TINT_TIMELINE);
    }

    public void setValueOfPoint(int index, int colorIndex, float value) {
        if (points.size() <= index) {
            points.add(new TintTimelinePoint());
        }
        points.get(index).setValue(colorIndex, value);
    }

    public void setTimeOfPoint(int index, float time) {
        if (points.size() <= index) {
            points.add(new TintTimelinePoint());
        }
        points.get(index).setTime(time);
    }

    public Color getValueAtTime(float time) {
        TintTimelinePoint left = null, right = null;
        for (TintTimelinePoint point : points) {
            if (point.getTime() <= time) {
                if (left == null || left.getTime() < point.getTime())
                    left = point;
            } else if (right == null || right.getTime() > point.getTime())
                right = point;
        }
        if (left != null) {
            if (right != null) {
                float leftDist = Math.abs(left.getTime() - time);
                float rightDist = Math.abs(right.getTime() - time);
                float totalDist = leftDist + rightDist;
                float leftMultiplier = leftDist / totalDist;
                float rightMultiplier = rightDist / totalDist;

                return left.getColor().interpolate(leftMultiplier,rightMultiplier, right.getColor());
            }
            return left.getColor();
        }
        return new Color();
    }

}
