from glogger import util
import request_util


class GrofersLogSchema(object):
    process = None  # type: str populated by glogger configure adapter

    env = None  # type: str

    level = None  # type: str populated by structlog processor

    timestamp = None  # type: str populated by structlog processor

    event = None  # type: str populated by structlog processor

    host = None  # type: str populated by requestlog adapter

    gr_trace_id = None  # type: str populated by requestlog adapter

    user_id = None  # type: int populated by requestlog adapter

    body = None  # type: str populated by requestlog adapter

    query_params = None  # type: str populated by requestlog adapter

    headers = None  # type: str populated by requestlog adapter

    path = None  # type: str populated by requestlog adapter

    method = None  # type: str populated by requestlog adapter

    file = None  # type: str populated while logging with loglevel

    line = None  # type: int populated while logging with loglevel

    func = None  # type: str populated while logging with loglevel

    def __init__(self, **kw):
        self._set_config_variables()
        for item in kw.keys():
            setattr(self, item, kw.get(item))

    def as_dict(self):
        """
        Add all class variables and then overrride with instance variable
        Returns: Dict of all log schema key-value

        """
        return vars(self)

    def _set_config_variables(self):
        self.process = (
            util.settings.get_app()
        )
        self.env = util.settings.get_env()


class GrofersRequestLogSchemaAdapter(GrofersLogSchema):
    def __init__(self, request=None, **kwargs):
        if not kwargs:
            kwargs = {}

        if request:
            request_impl = request_util.get_logrequest(request=request)
            kwargs.update(request_impl.dict())

        super(GrofersRequestLogSchemaAdapter, self).__init__(**kwargs)
