package me.koba1.led.utils.serializer;

import me.koba1.led.model.colors.KobaGradient;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ser.jackson.JsonValueSerializer;
import tools.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class KobaGradientSerializer extends StdSerializer<KobaGradient> {

    public KobaGradientSerializer() {
        super(KobaGradient.class);
    }

    @Override
    public void serialize(KobaGradient value, JsonGenerator gen, SerializationContext provider) throws JacksonException {
        gen.writeStartArray();
        for (KobaGradient.GradientStop stop : value.getStops()) {
            gen.writeStartObject();
            // "id" mező
            gen.writeNumberProperty("id", stop.id());
            // "position" mező (százalékban)
            gen.writeNumberProperty("position", (int) (stop.position() * 100));
            // "color" mező – delegálás a megfelelő serializernek
            gen.writePOJOProperty("color", stop.color());
            gen.writeEndObject();
        }
        gen.writeEndArray();
    }
}