package be.iminds.ilabt.jfed.experimenter_gui.slice.progress;

import be.iminds.ilabt.jfed.experimenter_gui.ui.status.TaskStatusIndicator;
import javafx.collections.ListChangeListener;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;

/**
 * User: twalcari
 * Date: 11/27/13
 * Time: 11:11 AM
 */
public class ProgressView extends ListView<ProgressItem> {


    public ProgressView() {
        setPrefHeight(100);
        setCellFactory(progressItemListView -> new ProgressViewItem());

        //always show the last progressitem that was added
        getItems().addListener((ListChangeListener<ProgressItem>) c -> {
            while (c.next()) {
                if (c.wasAdded()) {
                    scrollTo(getItems().size() - 1);
                }
            }
        });
    }

    public void registerProgressViewItem(ProgressItem progressItem){
        getItems().add(progressItem);
    }

    private static class ProgressViewItem extends ListCell<ProgressItem> {

        private final HBox hbox;

        private final TaskStatusIndicator taskStatusIndicator;
        private final Label textLabel;
        private final Label statusLabel;

        public ProgressViewItem() {
            hbox = new HBox();
            hbox.setSpacing(5);


            // add taskStatusIndicator as graphic
            taskStatusIndicator = new TaskStatusIndicator();
            taskStatusIndicator.setPrefHeight(16);
            taskStatusIndicator.setPrefWidth(16);

            textLabel = new Label();
            statusLabel = new Label();
            statusLabel.setStyle("-fx-text-fill: darkgrey;");

            hbox.getChildren().addAll(taskStatusIndicator, textLabel, statusLabel);

            setGraphic(hbox);
        }

        @Override
        protected void updateItem(final ProgressItem progressItem, boolean empty) {
            super.updateItem(progressItem, empty);

            if (!empty) {
                textLabel.textProperty().bind(progressItem.textProperty());
                statusLabel.textProperty().bind(progressItem.statusProperty());
                taskStatusIndicator.statusProperty().bind(progressItem.progressProperty());
                taskStatusIndicator.setVisible(true);

                setOnMouseEntered(mouseEvent -> {
                    if (progressItem.getOnDetailsView() != null) {
                        setOnMouseClicked(progressItem.getOnDetailsView());
                        setCursor(Cursor.HAND);
                    } else {
                        setOnMouseClicked(null);
                        setCursor(null);
                    }
                });

            } else {
                textLabel.textProperty().unbind();
                textLabel.setText("");
                statusLabel.textProperty().unbind();
                statusLabel.setText("");
                taskStatusIndicator.setVisible(false);
                setOnMouseClicked(null);
                setCursor(null);
            }
        }
    }
}
