import Vector3 from "../../../../core/geom/Vector3.js";
import { BinaryClassSerializationAdapter } from "../../../ecs/storage/binary/BinaryClassSerializationAdapter.js";
import Path from "./Path.js";


const v0 = new Vector3();

export class PathSerializationAdapter extends BinaryClassSerializationAdapter {

    klass = Path;
    version = 3;

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {Path} value
     */
    serialize(buffer, value) {
        const numPoints = value.getPointCount();

        buffer.writeUint8(value.interpolation);

        buffer.writeUintVar(numPoints);
        buffer.writeUintVar(3); // elements per point


        for (let i = 0; i < numPoints; i++) {
            value.getPosition(i, v0);

            buffer.writeFloat32(v0.x);
            buffer.writeFloat32(v0.y);
            buffer.writeFloat32(v0.z);
        }
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {Path} value
     */
    deserialize(buffer, value) {

        value.interpolation = buffer.readUint8();

        const numPoints = buffer.readUintVar();
        const elementsPerPoint = buffer.readUintVar(); // expected 3 for 3d

        value.setPointCount(numPoints);

        for (let i = 0; i < numPoints; i++) {
            const x = buffer.readFloat32();
            const y = buffer.readFloat32();
            const z = buffer.readFloat32();

            value.setPosition(i, x, y, z);
        }
    }
}
