# %%

import googlemaps

from gmplot import GoogleMapPlotter
import json
import pandas as pd
import ast
import random
import itertools
import time


# %%
with open("data/previous_stats_road_distance.json", "r", encoding="utf-8") as f:
    stats_previous_road = json.load(f)
stats_previous_road = {ast.literal_eval(k) if k != "overall" else k: v for k, v in stats_previous_road.items()}

with open("data/previous_stats_road_duration.json", "r", encoding="utf-8") as f:
    stats_previous_duration = json.load(f)
stats_previous_duration = {ast.literal_eval(k) if k != "overall" else k: v for k, v in stats_previous_duration.items()}
    
with open("data/new_stats_road_distance.json", "r", encoding="utf-8") as f:
    stats_new_road = json.load(f)
stats_new_road = {ast.literal_eval(k) if k != "overall" else k: v for k, v in stats_new_road.items()}

with open("data/new_stats_road_duration.json", "r", encoding="utf-8") as f:
    stats_new_duration = json.load(f)
stats_new_duration = {ast.literal_eval(k) if k != "overall" else k: v for k, v in stats_new_duration.items()}
    


# %%


print(stats_previous_road['overall'])
print(stats_previous_duration['overall'])
print(stats_new_road['overall'])
print(stats_new_duration['overall'])

overall_stats = {
    "previous": {
        "road": stats_previous_road['overall'],
        "duration": stats_previous_duration['overall'],
        "average_max_team_distance": sum([v['max_team_distance'] for k, v in stats_previous_road.items() if k != "overall"]) / len([v['max_team_distance'] for k, v in stats_previous_road.items() if k != "overall"])
    },
    "new": {
        "road": stats_new_road['overall'],
        "duration": stats_new_duration['overall'],
        "average_max_team_distance": sum([v['max_team_distance'] for k, v in stats_new_road.items() if k != "overall"]) / len([v['max_team_distance'] for k, v in stats_new_road.items() if k != "overall"])
    }
}

# %%

import plotly.graph_objects as go


# %%


""" create bar plot for overall stats """
fig = go.Figure()
fig.add_trace(go.Bar(
    x=["Distance", "Duration"],
    y=[overall_stats["previous"]["road"]['total_distance'], overall_stats["previous"]["duration"]['total_distance']],
    name="Previous",
    marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(
    x=["Distance", "Duration"],
    y=[overall_stats["new"]["road"]['total_distance'], overall_stats["new"]["duration"]['total_distance']],
    name="New",
    marker_color='rgb(26, 118, 255)'
))

fig.update_layout(
    title="Total Distances",
    xaxis_title="",
    yaxis_title="Distance in km / Time in m",
    barmode='group'
)

fig.show()

# %%


""" create bar plot for overall stats """
fig = go.Figure()
fig.add_trace(go.Bar(
    x=["Distance", "Duration", "Max Team Distance"],
    y=[overall_stats["previous"]["road"]['average_distance'], overall_stats["previous"]["duration"]['average_distance'], overall_stats["previous"]["average_max_team_distance"]],
    name="Previous",
    marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(
    x=["Distance", "Duration", "Max Team Distance"],
    y=[overall_stats["new"]["road"]['average_distance'], overall_stats["new"]["duration"]['average_distance'], overall_stats["new"]["average_max_team_distance"]],
    name="New",
    marker_color='rgb(26, 118, 255)'
))

fig.update_layout(
    title="Average Total Distance per Team",
    xaxis_title="",
    yaxis_title="Distance in km / Time in m",
    barmode='group'
)

fig.show()


# %%


""" create bar plot for overall stats """
fig = go.Figure()
fig.add_trace(go.Bar(
    x=["Distance", "Duration"],
    y=[overall_stats["previous"]["road"]['average_group_distance'], overall_stats["previous"]["duration"]['average_group_distance']],
    name="Previous",
    marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(
    x=["Distance", "Duration"],
    y=[overall_stats["new"]["road"]['average_group_distance'], overall_stats["new"]["duration"]['average_group_distance']],  
    name="New",
    marker_color='rgb(26, 118, 255)'
))

fig.update_layout(
    title="Average Total Distance per Group",
    xaxis_title="",
    yaxis_title="Distance in km / Time in m",
    barmode='group'
)

fig.show()





# %%
