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

import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentCommand;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.paint.Color;
import org.controlsfx.glyphfont.FontAwesome;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author Cedric
 */
public class CommandOutputTab extends Tab {
    private final ExperimentCommand command;
    private final TextArea output;
    private final ChangeListener<Boolean> startedListener;
    private final ChangeListener<Boolean> finishedListener;
    private final ChangeListener<String> outputListener;
    private final ChangeListener<String> errListener;

    public CommandOutputTab(ExperimentCommand c) {
        this.command = c;

        String nodeUniqueId = c.getNodeUniqueID();
        if(nodeUniqueId.contains("@"))
            nodeUniqueId = nodeUniqueId.substring(0, nodeUniqueId.indexOf('@'));

        setText(nodeUniqueId + ": " + c.getTag());
        output = new TextArea();
        output.setEditable(false);
        setContent(output);
        setGraphic(GlyphUtils.createSmallGlyph(FontAwesome.Glyph.TERMINAL, Color.rgb(120, 255, 120)));

        startedListener = (ov, t, t1) -> {
            if (!command.isSkipped() && command.isStarted()) {
                output.appendText(getTime() + ":\t" + "Your command has started.\n");
            }
        };
        command.getStartedProperty().addListener(startedListener);

        finishedListener = (ov, t, t1) -> {
            if (!command.isSkipped() && command.isFinished()) {
                output.appendText(getTime() + ":\t" + "Your command has finished.\n");
            }
        };
        command.getFinishedProperty().addListener(finishedListener);

        outputListener = (ov, t, t1) -> {
            if (!Objects.equals(t1, "")) {
                Platform.runLater(() -> output.appendText(getTime() + ":\t" + t1.substring(t.length())));

            }
        };

        command.getResultProperty().addListener(outputListener);

        errListener = (ov, t, t1) -> {
            if (!Objects.equals(t1, "")) {
                Platform.runLater(() -> output.appendText(getTime() + ": ERROR\t" + t1.substring(t.length())));

            }
        };
        command.getErrorProperty().addListener(errListener);
    }

    private String getTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    public void unregister() {
        command.getStartedProperty().removeListener(startedListener);
        command.getFinishedProperty().removeListener(finishedListener);
        command.getResultProperty().removeListener(outputListener);
        command.getErrorProperty().removeListener(errListener);
    }


}
