import GridObstacle from "../../../src/engine/grid/obstacle/GridObstacle.js";
import { Action } from "../../../src/core/process/undo/Action.js";

class WriteGridValueAction extends Action {
    constructor(entity, x, y, value) {
        super();

        this.entity = entity;
        this.x = x;
        this.y = y;
        this.value = value;

        this.obstacle = null;
        this.oldValue = null;
    }

    async apply(editor) {
        /**
         * @type {Engine}
         */
        const engine = editor.engine;

        /**
         *
         * @type {GridObstacle}
         */
        const obstacle = engine.entityManager.dataset.getComponent(this.entity, GridObstacle);
        this.obstacle = obstacle;


        this.oldValue = obstacle.readPoint(this.x, this.y);
        obstacle.writePoint(this.x, this.y, this.value);
    }

    async revert(editor) {

        this.obstacle.writePoint(this.x, this.y, this.oldValue);
    }
}

export default WriteGridValueAction;
