package be.iminds.jfed.gts_highlevel.tasks;

import be.iminds.ilabt.jfed.highlevel.controller.Task;
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 javax.inject.Provider;
import java.util.Collections;
import java.util.List;

/**
 * User: twalcari
 * Date: 9/25/2015
 * Time: 09:59
 */
public abstract class LoggedInGtsTask extends GtsTask {

    private final JavaFXLogger logger;

    private final GtsReservationManagerFactory gtsReservationManagerFactory;
    private final Provider<GtsLoginTask> loginGtsTaskProvider;
    protected final GtsModel gtsModel;

    public LoggedInGtsTask(String name,
                           JavaFXLogger logger, GtsReservationManagerFactory gtsReservationManagerFactory,
                           Provider<GtsLoginTask> loginGtsTaskProvider, GtsModel gtsModel) {
        super(name);
        this.logger = logger;
        this.gtsReservationManagerFactory = gtsReservationManagerFactory;
        this.loginGtsTaskProvider = loginGtsTaskProvider;
        this.gtsModel = gtsModel;
    }

    @Override
    protected final void doGtsTask(TaskExecution taskExecution) throws GtsException {
        assert gtsModel.getCredentials() != null;

        GtsReservationManager reservationManager =
                gtsReservationManagerFactory.
                        createGtsReservationManager(gtsModel.getCredentials(), JavaFXLogger.wrap(logger, taskExecution));

        doGtsRMTask(taskExecution, reservationManager);

    }

    protected abstract void doGtsRMTask(TaskExecution taskExecution, GtsReservationManager reservationManager) throws GtsException;


    @Override
    public List<Task> initDependsOn() {
        return Collections.singletonList(loginGtsTaskProvider.get());
    }
}
