import { ParticleParameters } from "../emitter/ParticleParameters.js";
import {
    PARTICLE_ATTRIBUTE_COLOR,
    PARTICLE_ATTRIBUTE_SIZE,
    PARTICLE_ATTRIBUTE_SIZE_INITIAL
} from "../emitter/PARTICLE_ATTRIBUTES.js";

const v4 = new Float32Array(4);

/**
 *
 * @param {number} particle_index
 * @param {ParticlePool} particles
 * @param {ParticleLayer} layer
 * @param {number} normalized_age
 */
export function update_parameters(particle_index, particles, layer, normalized_age) {
    const tracks = layer.parameterTracks;

    // update color
    const param_color = tracks.getTrackByName(ParticleParameters.Color);

    if (param_color !== undefined) {
        param_color.track.sample(normalized_age, v4);
        particles.writeAttributeVector4(particle_index, PARTICLE_ATTRIBUTE_COLOR, v4[0] * 255, v4[1] * 255, v4[2] * 255, v4[3] * 255);
    }

    // update size
    const param_scale = tracks.getTrackByName(ParticleParameters.Scale);

    const initial_size = particles.readAttributeScalar(particle_index, PARTICLE_ATTRIBUTE_SIZE_INITIAL);

    if (param_scale !== undefined) {
        param_scale.track.sample(normalized_age, v4);

        const scale = v4[0];
        particles.writeAttributeScalar(particle_index, PARTICLE_ATTRIBUTE_SIZE, scale * initial_size);
    } else {
        particles.writeAttributeScalar(particle_index, PARTICLE_ATTRIBUTE_SIZE, initial_size);
    }
}
