package ctbrec.ui.action;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import ctbrec.GlobalThreadPool;
import ctbrec.Model;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Node;

public class ModelMassEditAction {

    protected List<? extends Model> models;
    protected Consumer<Model> action;
    protected Node source;

    protected ModelMassEditAction(Node source, List<? extends Model> models) {
        this.source = source;
        this.models = models;
    }

    public ModelMassEditAction(Node source, List<? extends Model> models, Consumer<Model> action) {
        this.source = source;
        this.models = models;
        this.action = action;
    }

    public void execute() {
        execute(m -> {});
    }

    public void execute(Consumer<Model> callback) {
        Consumer<Model> cb = Objects.requireNonNull(callback, "Callback is null, call execute() instead");
        source.setCursor(Cursor.WAIT);
        for (Model model : models) {
            GlobalThreadPool.submit(() -> {
                action.accept(model);
                cb.accept(model);
                Platform.runLater(() -> source.setCursor(Cursor.DEFAULT));
            });
        }
    }
}
