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

import de.frajul.endlessroll.entities.particles.attributes.attributeValues.ImagePath;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.Options;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.ParticleAttributeValueType;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.Range;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.SpawnShape;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.Timeline;
import de.frajul.endlessroll.entities.particles.attributes.attributeValues.TintTimeline;

/**
 * Created by Julian on 02.08.2016.
 */
public class AttributeValueReader {

    public void addValueForAttribute(Attribute attribute, String line) throws Exception {
        //RANGE
        if (line.startsWith("lowMin:") || line.startsWith("min:"))
            ((Range) attribute.get(ParticleAttributeValueType.RANGE)).setLowMin(parseFloat(line));
        else if (line.startsWith("lowMax:") || line.startsWith("max:"))
            ((Range) attribute.get(ParticleAttributeValueType.RANGE)).setLowMax(parseFloat(line));
        else if (line.startsWith("highMin:"))
            ((Range) attribute.get(ParticleAttributeValueType.RANGE)).setHighMin(parseFloat(line));
        else if (line.startsWith("highMax:"))
            ((Range) attribute.get(ParticleAttributeValueType.RANGE)).setHighMax(parseFloat(line));
            //TIMELINE
        else if (!line.startsWith("scalingCount") && line.startsWith("scaling"))
            ((Timeline) attribute.get(ParticleAttributeValueType.TIMELINE)).setValueOfPoint(parseTimeLineIndex("scaling", line), parseFloat(line));
        else if (!line.startsWith("timelineCount") && line.startsWith("timeline") && attribute.getType() != ParticleAttributeType.TINT)
            ((Timeline) attribute.get(ParticleAttributeValueType.TIMELINE)).setTimeOfPoint(parseTimeLineIndex("timeline", line), parseFloat(line));
            //TINT_TIMELINE
        else if (!line.startsWith("colorsCount") && line.startsWith("colors")) {
            int index = parseTimeLineIndex("colors", line);
            ((TintTimeline) attribute.get(ParticleAttributeValueType.TINT_TIMELINE)).setValueOfPoint((int) (index / 3f), index % 3, parseFloat(line));
        } else if (!line.startsWith("timelineCount") && line.startsWith("timeline") && attribute.getType() == ParticleAttributeType.TINT)
            ((TintTimeline) attribute.get(ParticleAttributeValueType.TINT_TIMELINE)).setTimeOfPoint(parseTimeLineIndex("timeline", line), parseFloat(line));
            //SPAWN_SHAPE
        else if (line.startsWith("shape:"))
            ((SpawnShape) attribute.get(ParticleAttributeValueType.SPAWN_SHAPE)).setShape(SpawnShape.Shape.byName(parseString(line)));
            //OPTIONS
        else if (line.startsWith("attached:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setAttached(parseBoolean(line));
        else if (line.startsWith("continuous:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setContinuous(parseBoolean(line));
        else if (line.startsWith("aligned:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setAligned(parseBoolean(line));
        else if (line.startsWith("additive:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setAdditive(parseBoolean(line));
        else if (line.startsWith("behind:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setBehind(parseBoolean(line));
        else if (line.startsWith("premultipliedAlpha:"))
            ((Options) attribute.get(ParticleAttributeValueType.OPTIONS)).setPremultipliedAlpha(parseBoolean(line));
            //IMAGE PATH
        else if (attribute.getType() == ParticleAttributeType.IMAGE_PATH)
            ((ImagePath) attribute.get(ParticleAttributeValueType.IMAGE_PATH)).setImagePath(line);
    }

    private int parseTimeLineIndex(String start, String line) throws Exception {
        String asString = line.split(start)[1].split(":")[0];
        return Integer.parseInt(asString);
    }

    private float parseFloat(String line) throws Exception {
        String asString = line.split(" ")[1];
        return Float.parseFloat(asString);
    }

    private String parseString(String line) throws Exception {
        return line.split(" ")[1];
    }

    private boolean parseBoolean(String line) throws Exception {
        String asString = line.split(" ")[1];
        return Boolean.parseBoolean(asString);
    }

}
