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

import be.iminds.ilabt.jfed.bugreport.ConnectivityTestResult;
import be.iminds.ilabt.jfed.bugreport.LogLine;
import be.iminds.ilabt.jfed.ui.javafx.dialogs.JFDialogs;
import javafx.event.EventHandler;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.util.Callback;

public class ConnectivityTestTable extends TableView<ConnectivityTestResult> {
    private final TableColumn nameCol = new TableColumn("Name");
    private final TableColumn statusCol = new TableColumn("Status");
    private final TableColumn messageCol = new TableColumn("Message");
    private final TableColumn throwableCol = new TableColumn("Exception");

    public ConnectivityTestTable() {
        setEditable(false);

        final EventHandler<MouseEvent> cellDoubleClickHandler = event -> {
            if (event.getClickCount() > 1) {
                TableCell c = (TableCell) event.getSource();
                TableColumn col = c.getTableColumn();
                String text = c.getText();
                if (col == throwableCol)
                    System.err.print("\nUser requested print of this stacktrace:\n" + text + "\n");
                if (col == messageCol)
                    JFDialogs.create()
                            .message("LOG Message:\n\n" + text)
                            .masthead(null)
                            .title(null)
                            .showInformation();

                double targetWidth = c.prefWidthProperty().get();
                if (targetWidth <= 0) {
                    final Text javaFXText = new Text(text);
                    //if css needs to be applied, this would require some more tricks:
                    //                                        new Scene(new Group(text));
                    //
                    //                                            // java 7 =>
                    //                                            //    text.snapshot(null, null);
                    //                                            // java 8 =>
                    //                                            text.applyCss();

                    targetWidth = javaFXText.getLayoutBounds().getWidth();
                }

                double maxColSize = 0.6 * ConnectivityTestTable.this.widthProperty().get();
                if (col != throwableCol && targetWidth > maxColSize) {
                    targetWidth = maxColSize;
                }
                col.setPrefWidth(targetWidth + 5);
                col.setMinWidth(targetWidth + 5);
            }
        };

        Callback<TableColumn, TableCell> cellFactory =
                new Callback<TableColumn, TableCell>() {
                    public TableCell call(TableColumn p) {
                        final TableCell cell = new TableCell<LogLine, Object>() {
                            @Override
                            public void updateItem(Object item, boolean empty) {
                                super.updateItem(item, empty);
                                setText(empty ? null : getString());
                                setGraphic(null);
                                setWrapText(true);
                            }

                            private String getString() {
                                return getItem() == null ? "" : getItem().toString();
                            }
                        };

                        cell.addEventFilter(MouseEvent.MOUSE_CLICKED, cellDoubleClickHandler);
                        return cell;
                    }
                };

        statusCol.setCellFactory(cellFactory);
        nameCol.setCellFactory(cellFactory);
        messageCol.setCellFactory(cellFactory);
        throwableCol.setCellFactory(cellFactory);


        getColumns().addAll(statusCol, nameCol, messageCol, throwableCol);

        statusCol.setCellValueFactory(new PropertyValueFactory<ConnectivityTestResult, ConnectivityTestResult.Status>("status"));
        nameCol.setCellValueFactory(new PropertyValueFactory<ConnectivityTestResult, String>("name"));
        messageCol.setCellValueFactory(new PropertyValueFactory<ConnectivityTestResult, String>("message"));
        throwableCol.setCellValueFactory(new PropertyValueFactory<ConnectivityTestResult, String>("exception"));


        statusCol.setPrefWidth(50);
        nameCol.setPrefWidth(100);
        messageCol.setPrefWidth(400);
        throwableCol.setPrefWidth(500);
    }

}
