
import json
from typing import Dict, List


class UserClusterGraph:
    '''This class represents a graph of users with weighted connections for a single cluster of one single layer'''

    def __init__(self, nodes: List = None, edges: List = None, dict_: Dict = None, from_db=False):
        self.nodes = nodes
        self.edges = edges

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

    def to_serializable_dict(self, for_db=False) -> Dict:
        return {
            "nodes": json.dumps(self.nodes) if for_db else self.nodes,
            "edges": json.dumps(self.edges) if for_db else self.edges
        }

    def from_serializable_dict(self, dict_: Dict, from_db=False):
        self.nodes = json.loads(dict_["nodes"]) \
            if from_db else dict_["nodes"]
        self.edges = json.loads(dict_["edges"]) \
            if from_db else dict_["edges"]

    def __repr__(self):
        return json.dumps(self.to_serializable_dict())

    def __str__(self):
        return f"UserClusterGraph({repr(self)})"
