import { BinaryClassSerializationAdapter } from "../../../../ecs/storage/binary/BinaryClassSerializationAdapter.js";
import { LightProbeVolume } from "../LightProbeVolume.js";

export class LightProbeVolumeSerializationAdapter extends BinaryClassSerializationAdapter {

    klass = LightProbeVolume
    version = 0

    /**
     *
     * @param buffer
     * @param {LightProbeVolume} value
     */
    serialize(buffer, value) {
        const count = value.count;

        buffer.writeUint32(count);
        // write probe position data
        buffer.writeFloat32Array(value.points, 0, count * 3);
        // probe values
        buffer.writeFloat32Array(value.harmonics, 0, count * 27);

        // depth
        const depth_resolution = value.depth_map_resolution;
        buffer.writeUint32(depth_resolution);
        buffer.writeFloat32Array(value.depth, 0, depth_resolution * depth_resolution * count * 2);

        value.mesh.serialize(buffer);
    }

    /**
     *
     * @param buffer
     * @param {LightProbeVolume} value
     */
    deserialize(buffer, value) {
        value.clear();

        const count = buffer.readUint32();

        value.count = count;

        buffer.readFloat32Array(value.points, 0, count * 3);
        buffer.readFloat32Array(value.harmonics, 0, count * 27);

        // depth
        const depth_resolution = buffer.readUint32();
        value.depth_map_resolution = depth_resolution;
        buffer.readFloat32Array(value.depth, 0, depth_resolution * depth_resolution * count * 2);

        value.mesh.deserialize(buffer);

        value.incrementVersion();
    }
}