package be.iminds.jfed.gts_highlevel.tasks;

import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.util.JavaFXLogger;
import be.iminds.jfed.gts_highlevel.controller.GtsException;
import be.iminds.jfed.gts_highlevel.controller.GtsModel;
import be.iminds.jfed.gts_highlevel.controller.GtsReservationManager;
import be.iminds.jfed.gts_highlevel.controller.GtsReservationManagerFactory;
import be.iminds.jfed.gts_highlevel.model.GtsXmlProject;
import be.iminds.jfed.gts_highlevel.model.GtsXmlReservation;
import be.iminds.jfed.gts_highlevel.model.GtsXmlType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.inject.Provider;

/**
 * User: twalcari
 * Date: 9/25/2015
 * Time: 15:11
 */
public class StopGtsResourcesTask extends LoggedInGtsTask {
    private static final Logger LOG = LoggerFactory.getLogger(StopGtsResourcesTask.class);

    private final GtsXmlReservation reservation;
    private final GtsXmlType type;
    private final GtsXmlProject project;

    StopGtsResourcesTask(@Nonnull GtsXmlReservation reservation, @Nonnull GtsXmlType type, @Nonnull GtsXmlProject project,
                         GtsReservationManagerFactory gtsReservationManagerFactory,
                         Provider<GtsLoginTask> loginGtsTaskProvider,
                         GtsModel gtsModel, JavaFXLogger logger) {
        super("Stop GTS Resources", logger, gtsReservationManagerFactory, loginGtsTaskProvider, gtsModel);
        this.reservation = reservation;
        this.type = type;
        this.project = project;
    }

    @Override
    protected void doGtsRMTask(TaskExecution taskExecution, GtsReservationManager reservationManager) throws GtsException {
        LOG.debug("Deactivating reservation {}", reservation.getId());
        reservationManager.deactivateReservation(reservation.getId());

        LOG.debug("Releasing reservation {}", reservation.getId());
        reservationManager.releaseReservation(reservation.getId());

        LOG.debug("Removing type {} from project {}", type.getName(), project.getId());
        reservationManager.removeType(project,type);

    }

    public GtsXmlReservation getGtsReservation() {
        return reservation;
    }
}
