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

import be.iminds.ilabt.jfed.experimenter_gui.tasks.RecoverSliceTask;
import be.iminds.ilabt.jfed.experimenter_gui.tasks.TasksFactory;
import be.iminds.ilabt.jfed.experimenter_gui.ui.progressbar.TaskService;
import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import be.iminds.ilabt.jfed.ui.javafx.GuiceFXMLUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.controlsfx.glyphfont.FontAwesome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import java.time.format.DateTimeFormatter;

/**
 * User: twalcari
 * Date: 12/5/13
 * Time: 9:21 PM
 */
public class RecoverSliceDialog extends Dialog<ButtonType> {
    private static final Logger LOG = LoggerFactory.getLogger(RecoverSliceDialog.class);
    private static final DateTimeFormatter EXPIRATION_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    private static final String ALL_PROJECTS = "All projects";

    private final TasksFactory tasksFactory;

    private final EventHandler<MouseEvent> mouseEventEventHandler = mouseEvent -> {
        if (mouseEvent.getClickCount() == 2) {
            recoverExperiment();
        }

    };

    private final TaskService taskService;

    private final RecoverSliceController controller;

    @Inject
    RecoverSliceDialog(GuiceFXMLUtil guiceFXMLUtil, TasksFactory tasksFactory,
                       TaskService taskService, @Named("parentStage") Provider<Stage> parentStageProvider) {
        this.tasksFactory = tasksFactory;
        this.taskService = taskService;

        GuiceFXMLUtil.Result<?, RecoverSliceController> result =
                guiceFXMLUtil.createFromFXML(RecoverSliceController.class);
        this.controller = result.getController();

        controller.setOnDoubleClickEvent(mouseEvent -> recoverExperiment());

        initOwner(parentStageProvider.get());
        setTitle("Recover an existing experiment");
        setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.AMBULANCE, Color.GREEN));
        setHeaderText("Select the experiment to recover");
        getDialogPane().setContent(result.getRoot());

        ButtonType refreshButtonType = new ButtonType("Refresh & Fetch exp. dates", ButtonBar.ButtonData.OTHER);
        ButtonType recoverButtonType = new ButtonType("Recover Experiment", ButtonBar.ButtonData.OK_DONE);
        // , false, false, true,                                 actionEvent -> recoverExperiment());
        getDialogPane().getButtonTypes().setAll(refreshButtonType, recoverButtonType, ButtonType.CANCEL);

        Button refreshButton = (Button) getDialogPane().lookupButton(refreshButtonType);
        refreshButton.setGraphic(GlyphUtils.createSmallGlyph(FontAwesome.Glyph.REFRESH, Color.GREEN).useGradientEffect());
        refreshButton.addEventFilter(ActionEvent.ACTION, event -> {
            controller.refreshSlicesList();
            event.consume(); //prevents dialog from closing
        });


        Node recoverButton = getDialogPane().lookupButton(recoverButtonType);
        recoverButton.disableProperty().bind(controller.selectedSliceProperty().isNull());
        recoverButton.addEventFilter(ActionEvent.ACTION, event -> {
            recoverExperiment();
            event.consume();
        });
    }


    public void recoverExperiment() {
        assert controller.getSelectedSlice() != null;

        final Slice recoverSlice = controller.getSelectedSlice();

        RecoverSliceTask recoverSliceTask = tasksFactory.createRecoverSliceTask(recoverSlice);
        taskService.submitTask(recoverSliceTask);
        hide();
    }
}
