package io.bitsquare.gui.components;

import ch.qos.logback.classic.Logger;
import io.bitsquare.common.Timer;
import io.bitsquare.common.UserThread;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class BusyAnimation extends Pane {
    private static final Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(BusyAnimation.class);

    private Timer timer;
    private ImageView img1, img2;
    private int increment;
    private int rotation1, rotation2;
    private BooleanProperty runningProperty = new SimpleBooleanProperty();

    public BusyAnimation() {
        this(true);
    }

    public BusyAnimation(boolean isRunning) {
        runningProperty.set(isRunning);

        setMinSize(24, 24);
        setMaxSize(24, 24);
        setMouseTransparent(true);

        increment = 360 / 12;

        img1 = new ImageView();
        img1.setId("spinner");
        img2 = new ImageView();
        img2.setId("spinner");

        getChildren().addAll(img1, img2);

        sceneProperty().addListener((obs, oldVal, newVal) -> {
            if (newVal == null)
                stop();
            else if (isRunning())
                play();
        });
        runningProperty.addListener((obs, oldVal, newVal) -> {
            if (newVal)
                play();
            else
                stop();
        });

        updateVisibility();
    }

    public void play() {
        log.error("play " + isRunning());
        runningProperty.set(true);

        if (timer != null)
            timer.stop();
        timer = UserThread.runPeriodically(this::updateAnimation, 100, TimeUnit.MILLISECONDS);

        updateVisibility();
    }

    public void stop() {
        log.error("stop " + isRunning());
        runningProperty.set(false);
        if (timer != null) {
            timer.stop();
            timer = null;
        }
        updateVisibility();
    }

    private void updateAnimation() {
        rotation1 += increment;
        rotation2 -= increment;
        img1.setRotate(rotation1);
        img2.setRotate(rotation2);
    }

    private void updateVisibility() {
        log.error("updateVisibility " + isRunning());
        setVisible(isRunning());
        setManaged(isRunning());
    }

    public boolean isRunning() {
        return runningProperty.get();
    }

    public BooleanProperty runningProperty() {
        return runningProperty;
    }

    public void setRunning(boolean running) {
        log.error("setRunning " + running);
        runningProperty.set(running);
    }
}
@@ -1595,50 +1595,8 @@
 ) %7B%0A
-        log.error(%22play %22 + isRunning());%0A
     
@@ -1618,32 +1618,32 @@
 erty.set(true);%0A
+
 %0A        if (tim
@@ -1840,50 +1840,8 @@
 ) %7B%0A
-        log.error(%22stop %22 + isRunning());%0A
     
@@ -2218,62 +2218,8 @@
 ) %7B%0A
-        log.error(%22updateVisibility %22 + isRunning());%0A
     
@@ -2443,32 +2443,32 @@
 roperty;%0A    %7D%0A%0A
+
     public void 
@@ -2501,52 +2501,8 @@
 ) %7B%0A
-        log.error(%22setRunning %22 + running);%0A
     

remove logs