package com.cisco.trex.stl.gui.controllers.dashboard.charts;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

import com.exalttech.trex.util.Initialization;


public class ChartContainerController extends AnchorPane {
    @FXML
    private AnchorPane root;

    @FXML
    private void handleClicked(MouseEvent event) {
        contextMenu.show(root, event.getScreenX(), event.getScreenY());
    }

    private ContextMenu contextMenu;
    private FlowChartController chart;
    private StringProperty chartType;
    private IntegerProperty interval;

    public ChartContainerController(String selectedType, IntegerProperty interval) {
        Initialization.initializeFXML(this, "/fxml/dashboard/charts/ChartContainer.fxml");

        this.interval = interval;

        chartType = new SimpleStringProperty();
        chartType.addListener(this::handleChartTypeChanged);
        chartType.set(selectedType);

        contextMenu = new ContextMenu();
        contextMenu.getItems().addAll(
                createContextMenuItem(ChartsFactory.ChartTypes.TX_PPS),
                createContextMenuItem(ChartsFactory.ChartTypes.RX_PPS),
                createContextMenuItem(ChartsFactory.ChartTypes.TX_BPS_L1),
                createContextMenuItem(ChartsFactory.ChartTypes.TX_BPS_L2),
                createContextMenuItem(ChartsFactory.ChartTypes.RX_BPS_L2),
                new SeparatorMenuItem(),
                createContextMenuItem(ChartsFactory.ChartTypes.MAX_LATENCY),
                createContextMenuItem(ChartsFactory.ChartTypes.AVG_LATENCY),
                createContextMenuItem(ChartsFactory.ChartTypes.JITTER_LATENCY),
                createContextMenuItem(ChartsFactory.ChartTypes.TEMPORARY_MAX_LATENCY),
                createContextMenuItem(ChartsFactory.ChartTypes.LATENCY_HISTOGRAM)
        );
    }

    public void setActive(final boolean isActive) {
        chart.setActive(isActive);
    }

    private MenuItem createContextMenuItem(String chartType) {
        MenuItem item = new MenuItem(chartType);
        item.setOnAction(this::handleContextMenuAction);
        return item;
    }

    private void handleContextMenuAction(ActionEvent event) {
        MenuItem source = (MenuItem) event.getSource();
        chartType.set(source.getText());
        chart.setActive(true);
    }

    private void handleChartTypeChanged(
            ObservableValue<? extends String> observable,
            String oldValue,
            String newValue
    ) {
        chart = ChartsFactory.create(newValue, interval);

        AnchorPane.setTopAnchor((Node) chart, 0.0);
        AnchorPane.setRightAnchor((Node) chart, 0.0);
        AnchorPane.setBottomAnchor((Node) chart, 0.0);
        AnchorPane.setLeftAnchor((Node) chart, 0.0);

        root.getChildren().clear();
        root.getChildren().add((Node) chart);
    }
}

@@ -55,16 +55,213 @@
 harts;%0A%0A
+import com.cisco.trex.stl.gui.util.RunningConfiguration;%0Aimport com.exalttech.trex.application.TrexApp;%0Aimport com.exalttech.trex.util.Initialization;%0Aimport javafx.beans.property.BooleanProperty;%0A
 import j
@@ -729,56 +729,8 @@
 e;%0A%0A
-import com.exalttech.trex.util.Initialization;%0A%0A
 %0Apub
@@ -1114,24 +1114,145 @@
  interval;%0A%0A
+    private final RunningConfiguration runningConfiguration = TrexApp.injector.getInstance(RunningConfiguration.class);%0A%0A
     public C
@@ -2154,32 +2154,79 @@
 ypes.MAX_LATENCY
+, runningConfiguration.latencyEnabledProperty()
 ),%0A             
@@ -2278,32 +2278,79 @@
 ypes.AVG_LATENCY
+, runningConfiguration.latencyEnabledProperty()
 ),%0A             
@@ -2405,32 +2405,79 @@
 s.JITTER_LATENCY
+, runningConfiguration.latencyEnabledProperty()
 ),%0A             
@@ -2547,16 +2547,63 @@
 _LATENCY
+, runningConfiguration.latencyEnabledProperty()
 ),%0A     
@@ -2677,16 +2677,63 @@
 ISTOGRAM
+, runningConfiguration.latencyEnabledProperty()
 )%0A      
@@ -3029,24 +3029,265 @@
  item;%0A    %7D
+%0A    private MenuItem createContextMenuItem(String chartType, BooleanProperty enabledProperty) %7B%0A        MenuItem item = createContextMenuItem(chartType);%0A        item.disableProperty().bind(enabledProperty.not());%0A        return item;%0A    %7D
 %0A%0A    privat

Enable latency charts only if latency is enabled at least in one stream