import sys
import os
for path in ['../', './', '../../../modules/']:
    if os.path.exists(path):
        sys.path.insert(1, path)

import matplotlib.pyplot as plt
from db.repository import Repository
from db.entities import TimeSlice
from typing import List


def plt_show_circles(time_slices: List[TimeSlice], cluster_no):
    cluster_no = str(cluster_no)

    for slice_ in time_slices:

        nodes = slice_.get_nodes_for_cluster(cluster_no)
            
        # print(f"{slice_.time} number elements for cluster {cluster_no}: {len(nodes)}")

        plt.title(str(slice_.time))

        plt.scatter([n['Longitude_Destination'] if 'Longitude_Destination' in n else 0
                        for n in nodes],
                    [n['Latitude_Destination'] if 'Latitude_Destination' in n else 0
                        for n in nodes],
                    s=[len(nodes)*100]*len(nodes))

        plt.pause(0.5)


def plt_show_bars(time_slices: List[TimeSlice], cluster_no):
    cluster_no = str(cluster_no)
    
    labels = [ts.time for ts in time_slices]  
    x_axis_label_stepsize = 10  

    nodes_per_slice_for_single_cluster = \
            [len(time_slice.get_nodes_for_cluster(cluster_no))
            for time_slice
            in time_slices]

    fig, ax = plt.subplots()
    ax.bar(x=range(len(labels)),
        height=nodes_per_slice_for_single_cluster)

    ax.set_ylabel('Size')
    ax.set_title(f'Cluster-{cluster_no} size over time')
    ax.set_xticks(range(len(labels))[::x_axis_label_stepsize])
    ax.set_xticklabels(labels[::x_axis_label_stepsize])

    plt.show()


if __name__ == "__main__":
    repo = Repository()
    time_slices = repo.get_time_slices_by_name("Destination_Layer")

    # chronological order
    time_slices.sort(key=lambda ts: eval(ts.time))

    print(len(time_slices))
    plt_show_bars(time_slices, cluster_no = 0)