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

export class LightSerializationAdapter extends BinaryClassSerializationAdapter {

    klass = Light;
    version = 0;

    /**
     *
     * @param buffer
     * @param {Light} value
     */
    serialize(buffer, value) {

        buffer.writeUint8(value.type.getValue());
        buffer.writeUint32(value.color.toUint());
        value.intensity.toBinaryBuffer(buffer);
        value.castShadow.toBinaryBuffer(buffer);

        if (value.type.getValue() === LightType.SPOT) {
            value.angle.toBinaryBuffer(buffer);
            value.penumbra.toBinaryBuffer(buffer);
            value.distance.toBinaryBuffer(buffer);
        }
    }

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

        value.type.set(buffer.readUint8());
        value.color.fromUint(buffer.readUint32());
        value.intensity.fromBinaryBuffer(buffer);
        value.castShadow.fromBinaryBuffer(buffer);

        if (value.type.getValue() === LightType.SPOT) {
            value.angle.fromBinaryBuffer(buffer);
            value.penumbra.fromBinaryBuffer(buffer);
            value.distance.fromBinaryBuffer(buffer);
        }
    }
}
