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.experimenter_gui.util.ui.UIUtils;
import be.iminds.ilabt.jfed.rspec.model.controller.Timestamp;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecNode;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Window;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.Validator;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddCommandDialog extends GridPane {
    private static final Logger LOG = LoggerFactory.getLogger(AddCommandDialog.class);

    private final TimelineController controller;
    private final FXRspecNode node;
    private int barrierSegmentID;
    @FXML
    private TextField tagTextField;
    @FXML
    private TextField commandTextField;
    @FXML
    private TimeTextField startTextField;

    private final Dialog<ButtonType> dialog;

    private final ValidationSupport validationSupport;


    private AddCommandDialog(Window parentWindow, TimelineController controller, FXRspecNode node, Timestamp startingTime) {
        this.controller = controller;
        this.node = node;

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

        startTextField.setTime(startingTime.toString());
        tagTextField.setText("Command " + controller.getModel().nextCommandNumber());

        this.barrierSegmentID = 0;

        //initialize dialog
        dialog = new Dialog<>();
        dialog.initOwner(parentWindow);
        dialog.setTitle(String.format("Add a command to node %s", node.getClientId()));
        dialog.setHeaderText(dialog.getTitle());
        dialog.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.PLUS, Color.GREEN));
        dialog.getDialogPane().setContent(UIUtils.wrapWithDecorationPane(this));

        ButtonType addCommandButtonType =
                new ButtonType("Add command", ButtonBar.ButtonData.OK_DONE);//                this::onAddCommandAction);
        dialog.getDialogPane().getButtonTypes().setAll(addCommandButtonType, ButtonType.CANCEL);

        dialog.resultProperty().addListener(observable -> {
            if (dialog.getResult() == addCommandButtonType)
                controller.addCommand(tagTextField.getText(), node.getUniqueId(), barrierSegmentID
                        , new Timestamp(startTextField.toMillis())
                        , new Timestamp(0)
                        , commandTextField.getText());
        });

        //initialize validation support
        validationSupport = new ValidationSupport();
        validationSupport.registerValidator(tagTextField, Validator.createEmptyValidator("Tag is a required field!"));
        validationSupport.registerValidator(commandTextField, Validator.createEmptyValidator("Command is a required field!"));
        validationSupport.setValidationDecorator(new GraphicValidationDecoration());

        Node addCommandButton = dialog.getDialogPane().lookupButton(addCommandButtonType);
        addCommandButton.disableProperty().bind(validationSupport.invalidProperty());

    }

    private AddCommandDialog(Window parentWindow, TimelineController controller, FXRspecNode node, int barrierSegmentID, Timestamp startingTime) {
        this(parentWindow, controller, node, startingTime);
        this.barrierSegmentID = barrierSegmentID;
    }

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

    public static void showSimplePropertiesDialog(Window parentWindow, TimelineController controller, FXRspecNode node, Timestamp startingTime) {
        new AddCommandDialog(parentWindow, controller, node, startingTime).showDialog();
    }

    public static void showSimplePropertiesDialog(Window parentWindow, TimelineController controller, FXRspecNode node, int barrierSegmentID, Timestamp startingTime) {
        new AddCommandDialog(parentWindow, controller, node, barrierSegmentID, startingTime).showDialog();
    }
}
