import json
from datetime import datetime
from typing import Dict


class Layer:
    '''
    This class represents a single layer of the Multilayer Graph.

    :param layer_info: Information as dictionary to restore the layer object.
    '''

    def __init__(self, layer_info: Dict = None, from_db=False):
        if layer_info is not None:
            self.from_serializable_dict(layer_info, from_db)

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

    def from_serializable_dict(self, layer_info: Dict, from_db=False):
        self.layer_name = layer_info['layer_name']
        self.properties = layer_info['properties']
        self.nodes = json.loads(layer_info["nodes"]) \
            if from_db else layer_info["nodes"]

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

    def __str__(self):
        return f"Layer({self.__repr__()})"
