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

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.control.Tab;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;
import org.controlsfx.glyphfont.GlyphFont;
import org.controlsfx.glyphfont.GlyphFontRegistry;


/**
 * User: twalcari
 * Date: 7/2/2014
 * Time: 17:54
 */
public class ErrorsViewTab extends Tab {
    private static final GlyphFont fontAwesome = GlyphFontRegistry.font("FontAwesome");
    private final ErrorsView errorsView;
    private final Glyph errorIcon = fontAwesome.create(FontAwesome.Glyph.TIMES_CIRCLE);

    private final Timeline timeline = new Timeline();

    {
        setText("Errors");

        setGraphic(errorIcon);
        setClosable(false);

        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.setAutoReverse(true);

        KeyValue visibleKeyValue = new KeyValue(errorIcon.opacityProperty(), 0.0);
        KeyFrame visibleFrame = new KeyFrame(Duration.millis(700), visibleKeyValue);

        timeline.getKeyFrames().addAll(visibleFrame, visibleFrame);
    }


    public ErrorsViewTab(ErrorsView errorsView) {
        this.errorsView = errorsView;
        setContent(errorsView);

        errorsView.newItemsAvailableProperty().addListener(observable -> {
            if (ErrorsViewTab.this.errorsView.getNewItemsAvailable()) {
                if (isSelected()) {
                    ErrorsViewTab.this.errorsView.setNewItemsAvailable(false);
                } else {
                    errorIcon.setColor(Color.RED);
                    timeline.playFromStart();
                }
            } else {
                errorIcon.setColor(Color.BLACK);
                timeline.stop();
                errorIcon.setOpacity(1);
            }
        });

        selectedProperty().addListener(observable -> ErrorsViewTab.this.errorsView.setNewItemsAvailable(false));


    }
}
