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.Pools;
import lombok.Setter;

public class ChangeParentAction extends Action {
    private final Vector2 pos = Pools.obtain(Vector2.class);

    @Setter
    private Group parent;
    private boolean finished;

    public boolean act(float delta) {
        if (!finished) {
            finished = true;
            pos.set(target.getX(), target.getY());
            if (target.hasParent()) {
                target.getParent().localToStageCoordinates(pos);
            }
            parent.stageToLocalCoordinates(pos);
            parent.addActor(target);
            target.setPosition(pos.x, pos.y);
        }
        return true;
    }

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