import { MarkerNodeAction } from "./MarkerNodeAction.js";
import GridPosition from "../../../engine/grid/position/GridPosition.js";
import { Transform } from "../../../engine/ecs/transform/Transform.js";
import { EntityBlueprint } from "../../../engine/ecs/EntityBlueprint.js";

export class MarkerNodeActionEntityPlacement extends MarkerNodeAction {

    /**
     *
     * @type {EntityBlueprint}
     */
    entity = new EntityBlueprint();

    /**
     *
     * @type {Transform}
     */
    transform = new Transform();

    /**
     *
     * @type {MarkerNodeEntityProcessor}
     */
    processor = null;

    /**
     *
     * @param {EntityBlueprint} blueprint
     * @param {Transform} [transform]
     * @param {MarkerNodeEntityProcessor} [processor]
     * @returns {MarkerNodeActionEntityPlacement}
     */
    static from(
        {
            blueprint,
            transform = undefined,
            processor = null
        }
    ) {


        const r = new MarkerNodeActionEntityPlacement();

        r.entity = blueprint;

        if (transform !== undefined) {
            r.transform.copy(transform);
        }

        r.processor = processor;

        return r;
    }

    initialize(grid, ecd, seed) {
        if (this.processor !== null) {
            this.processor.initialize(grid, ecd);
        }
    }

    execute(grid, ecd, node) {
        const blueprint = this.entity;

        const entityBuilder = blueprint.build(node.properties);

        // execute post-process step
        if (this.processor !== null) {
            this.processor.execute(entityBuilder, node, grid, ecd);
        }

        /**
         *
         * @type {GridPosition}
         */
        const gp = entityBuilder.getComponent(GridPosition);

        if (gp !== null) {
            gp.copy(node.position);
        }

        /**
         *
         * @type {Transform}
         */
        const t = entityBuilder.getComponent(Transform);

        if (t !== null) {
            const temp = new Transform();

            temp.multiplyTransforms(node.transform, this.transform);

            t.multiplyTransforms(temp, t);
        }

        entityBuilder.build(ecd);
    }
}
