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

import be.iminds.ilabt.jfed.experimenter_gui.timeline.model.TimelineModel;
import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentBarrierSegment;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

/**
 *
 * @author Cedric
 */
public class TimelineBarrier extends Rectangle implements TimelineComponent {
    private final ExperimentBarrierSegment barrierSegment;
    private final TimelineModel model;
    public static final double DEFAULT_WIDTH = 10;
    
    private final ChangeListener<Boolean> startedListener = new ChangeListener<Boolean>() {

        @Override
        public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
            if(t1){
                getStyleClass().add("barrierInProgress");
                if(model.isStarted())
                    animation.play();
            } else {
                setStyle("");
                width = 50;
                getStyleClass().remove("barrierInProgress");
                animation.stop();
            }
        }
    };
    
    private final ChangeListener<Boolean> completedListener = new ChangeListener<Boolean>() {

        @Override
        public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
            if(t1){
                animation.stop();
                setStyle("");
                getStyleClass().add("barrierCompleted");
            } else {
                setStyle("");
                width = 50;
                getStyleClass().remove("barrierCompleted");
            }
        }
    };
    
    //Animation things
    private final Timeline animation;
    private int width = 50;
    private final int diff = 2;
    
    public TimelineBarrier(TimelineModel model, ExperimentBarrierSegment barrierSegment){
        this.model = model;
        this.barrierSegment = barrierSegment;
        this.setWidth(DEFAULT_WIDTH);
        this.getStyleClass().add("barrier");
        
        barrierSegment.getStarted().addListener(startedListener);
        
        barrierSegment.completedProperty().addListener(completedListener);
        
        animation = new Timeline(new KeyFrame(Duration.millis(32), actionEvent -> {
            setStyle("-fx-fill: linear-gradient(to right"
                    + ", #001414 " + (width - 40)// < 0 ? 0 : width - 40)
                    + "%, orange " + (width)
                    + "%, #001414 " + (width + 40) + "%);");

            width+= diff;
            if(width >= 160 )
                width = -60;

        }));
        animation.setCycleCount(Timeline.INDEFINITE);
    }

    @Override
    public void update(UpdateEvent event, Object o) {
        if(event == UpdateEvent.UNREGISTER){
            barrierSegment.getStarted().removeListener(startedListener);
        
            barrierSegment.completedProperty().removeListener(completedListener);
        } else if(event == UpdateEvent.STOP_TIME){
            animation.stop();
            if(barrierSegment.getStarted().get() && !barrierSegment.isCompleted()){
                setStyle("-fx-fill: linear-gradient(to right, #001414 0.1, orange 0.5, #001414 0.9);"); 
                width = 50;
            }
        } else if(event == UpdateEvent.START_TIME && barrierSegment.getStarted().get() && !barrierSegment.isCompleted()){
            animation.play();
        } else if(event == UpdateEvent.UPDATE_TIME){
            if(barrierSegment.getStarted().get() && !barrierSegment.isCompleted() && model.isStarted())
                animation.play();
        }
    }
}
