import json
from typing import List, Dict, TypeVar, Any
from datetime import date, datetime

Node = TypeVar('Node')


class TimeSlice:

    def __init__(self, time, nodes = None,
                 cluster_set_dict: Dict = None, from_db = False):
        self.time = time
        self.nodes: Dict[int, List[Node]] = {}

        # if cluster_set_dict is not None:
        #     self.from_serializable_dict(cluster_set_dict, from_db)

    def add_node_to_cluster(self, cluster_label, node):
        if cluster_label not in self.nodes:
            self.nodes[cluster_label] = []

        self.nodes[cluster_label].append(node)

    # todo

    # def to_serializable_dict(self, for_db=False) -> Dict:
    #     serialized_dict_clusters = [cluster.to_serializable_dict(for_db)
    #                                 for cluster in self.clusters]
    #     return {
    #         "layer_name": self.layer_name,
    #         "clusters": json.dumps(serialized_dict_clusters) if for_db else serialized_dict_clusters
    #     }

    # def from_serializable_dict(self, cluster_set_dict: Dict, from_db=False):
    #     self.layer_name = cluster_set_dict["layer_name"]

    #     serialized_dict_clusters = json.loads(cluster_set_dict["clusters"]) \
    #         if from_db else cluster_set_dict["clusters"]
    #     self.clusters = [Cluster(cluster_dict=cluster_dict, from_db=from_db)
    #                      for cluster_dict in serialized_dict_clusters]

    def __repr__(self):
        return self.__str__()
        # return {'time': self.time, "#nodes": len(self.nodes)}
        # json.dumps(self.to_serializable_dict())

    def __str__(self):
        return f"TimeSlice({self.time}, {[len(v) for k, v in self.nodes.items()]})"
