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,
            "use_case": self.use_case,
            "use_case_table": self.use_case_table,
            "total_properties": self.total_properties,
        }

    @staticmethod
    def from_business_logic_dict(layer_info: Dict):
        layer = Layer()
        layer.layer_name = layer_info["name"]
        layer.properties = layer_info["cluster_properties"]
        layer.total_properties = layer_info["properties"]
        layer.use_case = layer_info["use_case"]
        layer.use_case_table = layer_info["table"]
        return layer

    def from_serializable_dict(self, layer_info: Dict, from_db=False):
        self.layer_name = layer_info['layer_name']
        self.properties = layer_info['properties']
        self.use_case = layer_info["use_case"]
        self.use_case_table = layer_info["use_case_table"]
        self.total_properties = layer_info["total_properties"] if "total_properties"in layer_info.keys() else None 

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

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