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

import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentCommand;
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.*;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Window;
import org.controlsfx.glyphfont.FontAwesome;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class SaveOutputDialog extends GridPane {
    @FXML
    private TreeView<String> treeView;

    private final List<CheckBoxTreeItem<String>> nodesList = new ArrayList<>();
    private final Map<CheckBoxTreeItem<String>, ExperimentCommand> commands = new HashMap<>();

    private final Dialog<List<ExperimentCommand>> dialog;

    private List<ExperimentCommand> selectedCommands = null;


    public SaveOutputDialog(Window parent, List<FXRspecNode> nodes, List<ExperimentCommand> preSelection) {

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

        CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>("Your experiment");
        rootItem.setExpanded(true);
        treeView.setRoot(rootItem);
        treeView.setEditable(false);

        treeView.setCellFactory(CheckBoxTreeCell.forTreeView());

        for (FXRspecNode n : nodes) {
            CheckBoxTreeItem<String> cb = new CheckBoxTreeItem<>(n.getClientId());
            for (ExperimentCommand c : n.getCommands()) {
                CheckBoxTreeItem<String> cbc = new CheckBoxTreeItem<>(c.getTag());
                nodesList.add(cbc);
                commands.put(cbc, c);
                cb.getChildren().add(cbc);

                if (preSelection == null || preSelection.contains(c))
                    cbc.setSelected(true);
            }
            rootItem.getChildren().add(cb);
        }
        //initialize dialog


        dialog = new Dialog<>();
        dialog.initOwner(parent);
        dialog.setTitle("Save results");
        dialog.setHeaderText("Select which output you would like to save");
        dialog.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.SAVE, Color.BLUE));

        dialog.getDialogPane().setContent(this);

        ButtonType saveButtonType = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().setAll(saveButtonType, ButtonType.CANCEL);

        Node saveButton = dialog.getDialogPane().lookupButton(saveButtonType);
        saveButton.disableProperty().bind(treeView.getSelectionModel().selectedItemProperty().isNull());


        dialog.setResultConverter(param -> {
            if (param == saveButtonType) {
                return nodesList.stream()
                        .filter(CheckBoxTreeItem::isSelected)
                        .map(commands::get)
                        .collect(Collectors.toList());
            } else
                return null;
        });


    }


    public List<ExperimentCommand> showDialog() {
        return dialog.showAndWait().orElse(null);
    }
}
