package eu.jonahbauer.wizard.client.libgdx.actions;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pools;
import lombok.Setter;

/** Removes an actor from the stage.
 * @author Nathan Sweet */
public class ChangeParentAction extends Action {
    private final Pool<Vector2> vectorPool = Pools.get(Vector2.class);

    @Setter
    private Group parent;
    private boolean finished;

    public boolean act (float delta) {
        if (!finished) {
            finished = true;
            var pos = vectorPool.obtain();
            pos.set(0, 0);
            target.localToStageCoordinates(pos);
            parent.stageToLocalCoordinates(pos);
            target.setPosition(pos.x, pos.y);
            parent.addActor(target);
            vectorPool.free(pos);
        }
        return true;
    }

    public void restart () {
        finished = false;
    }
}
