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

export class ViewportPositionSerializationAdapter extends BinaryClassSerializationAdapter {

    version = 0;
    klass = ViewportPosition;

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {ViewportPosition} value
     */
    serialize(buffer, value) {
        value.position.toBinaryBufferFloat32(buffer);
        value.offset.toBinaryBufferFloat32(buffer);
        value.anchor.toBinaryBufferFloat32(buffer);

        buffer.writeFloat32(value.screenEdgeWidth);

        buffer.writeUint8(value.resolveGuiCollisions ? 1 : 0);
        buffer.writeUint8(value.stickToScreenEdge ? 1 : 0);
        buffer.writeUint8(value.enabled.getValue() ? 1 : 0);
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {ViewportPosition} value
     */
    deserialize(buffer, value) {
        value.position.fromBinaryBufferFloat32(buffer);
        value.offset.fromBinaryBufferFloat32(buffer);
        value.anchor.fromBinaryBufferFloat32(buffer);

        value.screenEdgeWidth = buffer.readFloat32();

        value.resolveGuiCollisions = buffer.readUint8() !== 0;
        value.stickToScreenEdge = buffer.readUint8() !== 0;
        value.enabled.set(buffer.readUint8() !== 0);
    }
}