package be.iminds.ilabt.jfed.experimenter_gui.tasks;

import be.iminds.ilabt.jfed.experimenter_gui.dialogs.SelectAuthoritiesDialog;
import be.iminds.ilabt.jfed.experimenter_gui.editor.ExperimentEditorTabFactory;
import be.iminds.ilabt.jfed.experimenter_gui.tutorials.TutorialViewsFactory;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ExperimentEditorTabHost;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.highlevel.controller.TaskThread;
import be.iminds.ilabt.jfed.highlevel.model.SfaModel;
import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.AggregateManagerWrapper;
import be.iminds.ilabt.jfed.lowlevel.authority.AuthorityFinder;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.GeniUrn;
import com.google.common.eventbus.EventBus;
import javafx.stage.Window;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * User: twalcari
 * Date: 12/1/2014
 * Time: 17:40
 */
@Singleton
public class TasksFactoryImpl implements TasksFactory {
    private final SfaModel sfaModel;
    private final HighLevelTaskFactory hltf;
    private final TaskThread tt;
    private final EventBus eventBus;
    private final Provider<SelectAuthoritiesDialog> selectAuthoritiesDialogProvider;
    private final Provider<TutorialViewsFactory> tutorialViewsFactoryProvider;
    private final ExperimentEditorTabFactory experimentEditorTabFactory;
    private final ExperimentEditorTabHost experimentEditorTabHost;
    private final TestbedInfoSource testbedInfoSource;
    private final AuthorityFinder authorityFinder;
    private final JFedPreferences jFedPreferences;

    @Inject
    TasksFactoryImpl(SfaModel sfaModel, HighLevelTaskFactory hltf,
                     TaskThread tt, EventBus eventBus, Provider<TutorialViewsFactory> tutorialViewsFactoryProvider,
                     TestbedInfoSource testbedInfoSource,
                     AuthorityFinder authorityFinder,
                     JFedPreferences jFedPreferences,
                     Provider<SelectAuthoritiesDialog> selectAuthoritiesDialogProvider,
                     ExperimentEditorTabFactory experimentEditorTabFactory,
                     ExperimentEditorTabHost experimentEditorTabHost) {
        this.sfaModel = sfaModel;
        this.hltf = hltf;
        this.tt = tt;
        this.eventBus = eventBus;
        this.tutorialViewsFactoryProvider = tutorialViewsFactoryProvider;
        this.testbedInfoSource = testbedInfoSource;
        this.authorityFinder = authorityFinder;
        this.jFedPreferences = jFedPreferences;
        this.selectAuthoritiesDialogProvider = selectAuthoritiesDialogProvider;
        this.experimentEditorTabFactory = experimentEditorTabFactory;
        this.experimentEditorTabHost = experimentEditorTabHost;
    }

    @Override
    public RecoverSliceTask createRecoverSliceTask(GeniUrn sliceUrn) {
        return new RecoverSliceTask(sliceUrn, sfaModel, hltf, testbedInfoSource, authorityFinder, jFedPreferences, tt, eventBus, selectAuthoritiesDialogProvider);

    }

    @Override
    public RecoverSliceTask createRecoverSliceTask(Slice slice) {
        return new RecoverSliceTask(slice, sfaModel, hltf, testbedInfoSource, authorityFinder, jFedPreferences, tt, eventBus, selectAuthoritiesDialogProvider);
    }

    @Override
    public DeleteDiskImageTask createDeleteDiskImageTask(Server auth, AggregateManagerWrapper.ImageInfo imageInfo) {
        return new DeleteDiskImageTask(auth, imageInfo, hltf);
    }

    @Override
    public OpenRequestRspecTask createOpenRequestRspecTask(Window parentWindow, File file) throws FileNotFoundException {
        return new OpenRequestRspecTask(parentWindow, file, experimentEditorTabFactory, experimentEditorTabHost);

    }

    @Override
    public OpenRequestRspecTask createOpenRequestRspecTask(Window parentWindow, InputStream inputStream, long fileLength, String name, File defaultSaveFile) {
        return new OpenRequestRspecTask(parentWindow, inputStream, fileLength, name, defaultSaveFile, experimentEditorTabFactory, experimentEditorTabHost);
    }

    @Override
    public OpenRequestRspecTask createOpenRequestRspecTask(Window parentWindow, String urlString) throws IOException {
        return new OpenRequestRspecTask(parentWindow, urlString, experimentEditorTabFactory, experimentEditorTabHost);
    }

    @Override
    public FetchAllSlicesInfoTask createFetchAllSlicesInfoTask() {
        return new FetchAllSlicesInfoTask(sfaModel, hltf, tt);
    }


    @Override
    public OpenTutorialTask createOpenTutorialTask(Window dialogOwnerWindow, String urlString) throws IOException {
        return new OpenTutorialTask(dialogOwnerWindow, urlString, tutorialViewsFactoryProvider.get());
    }
}
