package be.iminds.ilabt.jfed.experimenter_gui.timeline.controller.action;

import be.iminds.ilabt.jfed.experimenter_gui.timeline.model.TimelineModel;
import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.TimelineComponent;
import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.UpdateEvent;
import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentCommand;

/**
 * @author Cedric
 */
public class DeleteCommandAction implements IAction {
    private final ExperimentCommand command;

    public DeleteCommandAction(ExperimentCommand command) {
        this.command = command;
    }

    @Override
    public boolean execute(TimelineModel model, TimelineComponent view) {
        //can't remove a command that's already started execution
        if (command.isStarted())
            return false;

        model.deleteCommand(command);
        view.update(UpdateEvent.DELETE_COMMAND, command);
        return true;
    }

    @Override
    public boolean undo(TimelineModel model, TimelineComponent view) {
        //Can't add if the starting time is already in the past
        if (model.getElapsedTime() > command.getStartingTime().toMillis())
            return false;

        model.addCommand(command);
        view.update(UpdateEvent.ADD_COMMAND, command);
        return true;
    }
}
