package electrosphere.client.ui.menu.debug;

import electrosphere.engine.Globals;
import electrosphere.renderer.ui.imgui.ImGuiLinePlot;
import electrosphere.renderer.ui.imgui.ImGuiLinePlot.ImGuiLinePlotDataset;
import electrosphere.renderer.ui.imgui.ImGuiWindow;
import electrosphere.renderer.ui.imgui.ImGuiWindow.ImGuiWindowCallback;

public class ImGuiNetworkMonitor {
    
    /**
     * Num datapoints
     */
    public static final int PRESSURE_GRAPH_POINT_COUNT = 100;

    /**
     * Window for viewing chunk status on server and client
     */
    protected static ImGuiWindow netMonitorWindow;

    /**
     * Creates the windows in this file
     */
    protected static void createNetworkMonitorWindows(){
        createNetworkMonitorWindow();
    }

    /**
     * Client scene entity view
     */
    protected static void createNetworkMonitorWindow(){
        netMonitorWindow = new ImGuiWindow("Network Monitor");

        //client network pressure graph
        ImGuiLinePlot clientNetworkBandwith = new ImGuiLinePlot("Client Network Pressure",400,400);
        ImGuiLinePlotDataset clientPressureDataset = new ImGuiLinePlotDataset("Client bytes per frame", PRESSURE_GRAPH_POINT_COUNT);
        clientPressureDataset.zeroOut();
        clientNetworkBandwith.addDataset(clientPressureDataset);

        //server network pressure graph
        ImGuiLinePlot serverNetworkPressureGraph = new ImGuiLinePlot("Server Network Pressure",400,400);
        ImGuiLinePlotDataset serverPressureDataset = new ImGuiLinePlotDataset("Server bytes per frame", PRESSURE_GRAPH_POINT_COUNT);
        serverPressureDataset.zeroOut();
        serverNetworkPressureGraph.addDataset(serverPressureDataset);

        netMonitorWindow.setCallback(new ImGuiWindowCallback() {
            long clientPressureLastValue = 0;
            long serverPressureLastValue = 0;

            @Override
            public void exec() {


                //client network pressure
                if(Globals.clientConnection != null){
                    long clientPressureNewTotal = Globals.clientConnection.getNumBytesRead();
                    long clientPressureDelta = clientPressureNewTotal - clientPressureLastValue;
                    clientPressureDataset.addPoint(clientPressureDelta);
                    clientPressureLastValue = clientPressureNewTotal;
                }
                clientNetworkBandwith.draw();

                //server network pressure
                if(Globals.server != null && Globals.server.getFirstConnection() != null){
                    long serverPressureNewTotal = Globals.server.getFirstConnection().getNumBytesRead();
                    long serverPressureDelta = serverPressureNewTotal - serverPressureLastValue;
                    serverPressureDataset.addPoint(serverPressureDelta);
                    serverPressureLastValue = serverPressureNewTotal;
                }
                serverNetworkPressureGraph.draw();
                
            }
        });
        netMonitorWindow.setOpen(false);
        Globals.renderingEngine.getImGuiPipeline().addImGuiWindow(netMonitorWindow);
    }

}
