from agora_rest_client.core import exceptions

class RequestObject(object):
    """
    Request object
    """

    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            if not hasattr(self, k):
                raise exceptions.AttributeException("{} has no attribute '{}'".format(type(self).__name__, k))

            setattr(self, k, v)

    def __str__(self):
        return ",".join(["{}={}".format(k, v) for k, v in self.__dict__.items()])

    def to_dict(self):
        """
        Returns the object properties as a dict
        """
        result = {}

        attrs = [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")]
        for attr in attrs:
            value = getattr(self, attr)

            if value is None:
                continue

            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_json(self):
        """
        Returns the object properties as a json
        """
        return self.to_dict()

