import math

import pandas as pd
import plotly.offline
import plotly.graph_objects as go
from plotly import subplots
import os

traces = []
titles = []

colorCounter = 0

for csv_file in os.listdir('ResultsCSV'):
    if "keep.me" not in csv_file:
        df = pd.read_csv("ResultsCSV/" + csv_file)

        if colorCounter % 2 == 0:
            color = "blue"
        else:
            color = "red"
        colorCounter += 1

        trace = go.Scatter(x=df[df.columns[0]], y=df[df.columns[1]],
                           name="", line=go.scatter.Line(color=color), mode="markers+lines")

        traces.append(trace)
        titles.append(csv_file[:-4].replace("_", " "))

if traces:
    fig = plotly.subplots.make_subplots(rows=math.ceil(len(traces) / 2),
                                        cols=2,
                                        print_grid=True,
                                        vertical_spacing=0.040,
                                        horizontal_spacing=0.045,
                                        subplot_titles=titles)

    for i in range(len(traces)):
        fig.append_trace(traces[i], math.ceil((i + 1) / 2), (i % 2) + 1)
        if "LATENCY" in titles[i].upper():
            fig.update_yaxes(title_text="Latency (ms)", row=math.ceil((i + 1) / 2), col=1)
        else:
            fig.update_yaxes(title_text="Samples (per minute)", row=math.ceil((i + 1) / 2), col=2)

    fig.update_layout(height=300 * len(titles), plot_bgcolor='rgb(245, 245, 255)',
                      showlegend=False)

    plotly.offline.plot(fig, filename='index.html')
