from flask.json import jsonify
from flask_restful import Resource
from google.protobuf.json_format import MessageToDict
from common.message_broker.Factory import LOGGER
from common.orm.Database import Database
from context.proto.context_pb2 import ContextId, DeviceId, Empty, LinkId, ServiceId, TopologyId
from context.service.grpc_server.ContextServiceServicerImpl import ContextServiceServicerImpl

def grpc_context_id(context_uuid):
    return ContextId(**{
        'context_uuid': {'uuid': context_uuid}
    })

def grpc_topology_id(context_uuid, topology_uuid):
    return TopologyId(**{
        'context_id': {'context_uuid': {'uuid': context_uuid}},
        'topology_uuid': {'uuid': topology_uuid}
    })

def grpc_service_id(context_uuid, service_uuid):
    return ServiceId(**{
        'context_id': {'context_uuid': {'uuid': context_uuid}},
        'service_uuid': {'uuid': service_uuid}
    })

def grpc_device_id(device_uuid):
    return DeviceId(**{
        'device_uuid': {'uuid': device_uuid}
    })

def grpc_link_id(link_uuid):
    return LinkId(**{
        'link_uuid': {'uuid': link_uuid}
    })

def format_grpc_to_json(grpc_reply):
    return jsonify(MessageToDict(
        grpc_reply, including_default_value_fields=True, preserving_proto_field_name=True,
        use_integers_for_enums=False))

class _Resource(Resource):
    def __init__(self, database : Database) -> None:
        super().__init__()
        self.database = database
        self.servicer = ContextServiceServicerImpl(self.database, None)

class ContextIds(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListContextIds(Empty(), None))

class Contexts(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListContexts(Empty(), None))

class Context(_Resource):
    def get(self, context_uuid : str):
        return format_grpc_to_json(self.servicer.GetContext(grpc_context_id(context_uuid), None))

class TopologyIds(_Resource):
    def get(self, context_uuid : str):
        return format_grpc_to_json(self.servicer.ListTopologyIds(grpc_context_id(context_uuid), None))

class Topologies(_Resource):
    def get(self, context_uuid : str):
        return format_grpc_to_json(self.servicer.ListTopologies(grpc_context_id(context_uuid), None))

class Topology(_Resource):
    def get(self, context_uuid : str, topology_uuid : str):
        return format_grpc_to_json(self.servicer.GetTopology(grpc_topology_id(context_uuid, topology_uuid), None))

class ServiceIds(_Resource):
    def get(self, context_uuid : str):
        return format_grpc_to_json(self.servicer.ListServiceIds(grpc_context_id(context_uuid), None))

class Services(_Resource):
    def get(self, context_uuid : str):
        return format_grpc_to_json(self.servicer.ListServices(grpc_context_id(context_uuid), None))

class Service(_Resource):
    def get(self, context_uuid : str, service_uuid : str):
        return format_grpc_to_json(self.servicer.GetService(grpc_service_id(context_uuid, service_uuid), None))

class DeviceIds(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListDeviceIds(Empty(), None))

class Devices(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListDevices(Empty(), None))

class Device(_Resource):
    def get(self, device_uuid : str):
        return format_grpc_to_json(self.servicer.GetDevice(grpc_device_id(device_uuid), None))

class LinkIds(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListLinkIds(Empty(), None))

class Links(_Resource):
    def get(self):
        return format_grpc_to_json(self.servicer.ListLinks(Empty(), None))

class Link(_Resource):
    def get(self, link_uuid : str):
        return format_grpc_to_json(self.servicer.GetLink(grpc_link_id(link_uuid), None))

# Use 'path' type in Service and Sink because service_uuid and link_uuid might contain char '/' and Flask is unable to
# recognize them in 'string' type.
RESOURCES = [
    # (endpoint_name, resource_class, resource_url)
    ('api.context_ids',  ContextIds,  '/context_ids'),
    ('api.contexts',     Contexts,    '/contexts'),
    ('api.context',      Context,     '/context/<string:context_uuid>'),

    ('api.topology_ids', TopologyIds, '/context/<string:context_uuid>/topology_ids'),
    ('api.topologies',   Topologies,  '/context/<string:context_uuid>/topologies'),
    ('api.topology',     Topology,    '/context/<string:context_uuid>/topology/<string:topology_uuid>'),

    ('api.service_ids',  ServiceIds,  '/context/<string:context_uuid>/service_ids'),
    ('api.services',     Services,    '/context/<string:context_uuid>/services'),
    ('api.service',      Service,     '/context/<string:context_uuid>/service/<path:service_uuid>'),

    ('api.device_ids',   DeviceIds,   '/device_ids'),
    ('api.devices',      Devices,     '/devices'),
    ('api.device',       Device,      '/device/<string:device_uuid>'),

    ('api.link_ids',     LinkIds,     '/link_ids'),
    ('api.links',        Links,       '/links'),
    ('api.link',         Link,        '/link/<path:link_uuid>'),
]
