import sys
import os
modules_path = '../../../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)

import json
from datetime import datetime, date
from db.repository import Repository
from db.entities.timeslice import TimeSlice
from db.entities import ClusterSet
from typing import Tuple, Dict, Any


def convert_to_time_slice_key(timestamp: str) -> Tuple[int, int]:
    '''Returns the tuple (year, week_of_year) from a timestamp. This is used as the key for the slicing.'''
    timestamp = datetime.fromtimestamp(float(timestamp[0:10]))
    (y, w, _) = timestamp.isocalendar()
    return (y, w)


def split_clustersets_by_time(clustersets) -> Dict[Any, TimeSlice]:
    '''
    Partitions all nodes of each clusterset into idividual time slices based on their timestamp. The information about the cluster is kept.

    :params clustersets: The clustersets whichs nodes are split
    :returns: A dict of time slices where the key is the time info and value is the information about the time slice
    '''
    cnt = 0
    time_slices: Dict[Any, TimeSlice] = {} 
    for clusterset in clustersets:
        for cluster_no in clusterset.clusters:
            for node in cluster_no.nodes:
                # assign the nodes to time slices and recreate the clusters there
                # TODO use start and end time for assignment
                time_key = convert_to_time_slice_key(str(node['Finished_time']))
                
                if time_key not in time_slices:
                    time_slices[time_key] = TimeSlice(time_key, clusterset.layer_name)

                time_slices[time_key].add_node_to_cluster(cluster_no.cluster_label, node)
    return time_slices
        

if __name__ == "__main__":
    repo = Repository()

    clustersets = [repo.get_clusterset('Destination_Layer')]
    time_slices = split_clustersets_by_time(clustersets)

    # sort chronologically
    keys = list(time_slices.keys())
    keys.sort()

    repo.remove_all_time_slices()
    for k,v in time_slices.items():
        repo.add_time_slice(v)
