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

import be.iminds.ilabt.jfed.experimenter_gui.timeline.controller.TimelineController;
import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.TimeTextField;
import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentBarrierSegment;
import be.iminds.ilabt.jfed.rspec.model.controller.Timestamp;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Window;
import org.controlsfx.glyphfont.FontAwesome;

import java.util.HashMap;
import java.util.Map;

public class FastForwardDialog extends GridPane {
    private final TimelineController controller;
    @FXML
    private TimeTextField fastForwardTextField;
    @FXML
    private VBox radioContainer;

    private final Map<RadioButton, ExperimentBarrierSegment> segments = new HashMap<>();
    private final ToggleGroup group = new ToggleGroup();

    private final Dialog<ButtonType> dialog;

    private FastForwardDialog(Window parentWindow, TimelineController controller, Timestamp startingTime) {
        this.controller = controller;

        //initialize the Scene
        FXMLUtil.injectFXML(this);

        fastForwardTextField.setTime(startingTime.toString());

        for (ExperimentBarrierSegment b : controller.getModel().getCommandModelRspec().getBarrierSegments()) {
            RadioButton rb = new RadioButton(b.getTag());
            rb.setToggleGroup(group);
            radioContainer.getChildren().add(rb);
            segments.put(rb, b);
        }
        group.selectToggle((RadioButton) radioContainer.getChildren().get(0));

        //initialize dialog
        dialog = new Dialog<>();
        dialog.initOwner(parentWindow);
        dialog.setTitle("Fast Forward");
        dialog.setHeaderText("Jump to a specific time of the experiment");
        dialog.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.CLOCK_ALT, Color.BLACK));
        dialog.getDialogPane().setContent(this);
        dialog.getDialogPane().getButtonTypes().setAll(ButtonType.OK,ButtonType.CANCEL);

        dialog.resultProperty().addListener(observable -> {
            if(dialog.getResult() == ButtonType.OK){
                //noinspection RedundantCast
                controller.setElapsedTime(fastForwardTextField.toMillis(), segments.get((RadioButton)group.getSelectedToggle()));
            }
        });
    }

    public static void showSimpleFastForwardDialog(Window parentWindow, TimelineController controller, Timestamp startingTime) {
        FastForwardDialog dialog = new FastForwardDialog(parentWindow,controller, startingTime);
        dialog.showDialog();

    }

    private void showDialog() {
        dialog.show();
    }

}
