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

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.experiment.ExperimentController;
import be.iminds.ilabt.jfed.experiment.ExperimentControllerFactory;
import be.iminds.ilabt.jfed.experimenter_gui.call_gui.TasksWindow;
import be.iminds.ilabt.jfed.experimenter_gui.config.JFedGuiConfig;
import be.iminds.ilabt.jfed.experimenter_gui.slice.errors.ErrorDialogs;
import be.iminds.ilabt.jfed.experimenter_gui.slice.errors.ErrorsView;
import be.iminds.ilabt.jfed.experimenter_gui.slice.jobs.ExperimenterJobFactory;
import be.iminds.ilabt.jfed.experimenter_gui.util.BrowserUtil;
import be.iminds.ilabt.jfed.experimenter_gui.util.BugReportExperimentControllerListener;
import be.iminds.ilabt.jfed.preferences.JFedGuiPreferences;
import com.google.common.eventbus.EventBus;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

/**
 * User: twalcari
 * Date: 2/5/2015
 * Time: 11:08
 */
@Singleton
public class ExperimentViewControllerFactory {

    private final ExperimentControllerFactory experimentControllerFactory;
    private final ExperimentViewsFactory experimentViewsFactory;
    private final ExperimenterJobFactory experimenterJobFactory;
    private final Provider<ErrorsView> errorsViewProvider;
    private final Provider<ErrorDialogs> errorDialogsProvider;
    private final JFedGuiPreferences jFedPreferences;
    private final JFedGuiConfig jFedGuiConfig;
    private final TasksWindow tasksWindow;
    private final EventBus eventBus;

    private final ProgressItemCustomDetailsViews progressItemCustomViews;
    private final BrowserUtil browserUtil;
    private final BugReportExperimentControllerListener bugReportExperimentControllerListener;

    @Inject
    public ExperimentViewControllerFactory(ExperimentControllerFactory experimentControllerFactory,
                                           ExperimentViewsFactory experimentViewsFactory, Provider<ErrorsView> errorsViewProvider, Provider<ErrorDialogs> errorDialogsProvider,
                                           JFedGuiPreferences jFedPreferences,
                                           JFedGuiConfig jFedGuiConfig,
                                           TasksWindow tasksWindow, EventBus eventBus,
                                           ExperimenterJobFactory experimenterJobFactory,
                                           ProgressItemCustomDetailsViews progressItemCustomViews,
                                           BrowserUtil browserUtil,
                                           BugReportExperimentControllerListener bugReportExperimentControllerListener) {
        this.experimentControllerFactory = experimentControllerFactory;
        this.experimentViewsFactory = experimentViewsFactory;
        this.errorsViewProvider = errorsViewProvider;
        this.errorDialogsProvider = errorDialogsProvider;
        this.jFedPreferences = jFedPreferences;
        this.jFedGuiConfig = jFedGuiConfig;
        this.tasksWindow = tasksWindow;
        this.eventBus = eventBus;
        this.experimenterJobFactory = experimenterJobFactory;
        this.progressItemCustomViews = progressItemCustomViews;
        this.browserUtil = browserUtil;
        this.bugReportExperimentControllerListener = bugReportExperimentControllerListener;
    }

    public ExperimentViewController createExperimentViewController(ExperimentController experimentController){
        return new ExperimentViewController(experimentController, experimentViewsFactory, errorsViewProvider, errorDialogsProvider.get(), tasksWindow, jFedPreferences, jFedGuiConfig, eventBus, progressItemCustomViews, browserUtil, experimenterJobFactory);

    }

    public ExperimentViewController createExperimentViewController(Experiment experiment){
        final ExperimentController experimentController =
                experimentControllerFactory.createExperimentController(experiment);

        experimentController.setExperimentTester(experimenterJobFactory::createTestConnectivityJob);
        experimentController.addListener(bugReportExperimentControllerListener);


        return createExperimentViewController(experimentController);
    }


}
