# Path: golem/core/responses/responses.py
# class MessageElement:
#     """
#     Base class for message elements.
#     """
#     def to_message(self, fbid):
#         return {"recipient": {"id": fbid}, "message": self.to_response()}
# 
#     def to_response(self):
#         pass
# 
#     def get_response_for(self, platform='fb'):
#         return str(self)  # TODO
# 
#     def set_message_tag(self, tag):
#         self.tag = tag
# 
#     def get_message_tag(self):
#         """
#         See https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags
#         :return: FB message tag
#         """
#         if hasattr(self, "tag"):
#             return self.tag
#         return None
# 
#     def __str__(self):
#         return str(self.__dict__)
# 
#     def __repr__(self):
#         return str(self)
# 
# def _get_payload_string(payload):
#     text = ''
#     for entity, values in payload.items():
#         if isinstance(values, list):
#             for value in values:
#                 text += '/{}/{}/ '.format(entity, value)
#             break
#         text += '/{}/{}/ '.format(entity, values)
#     return text
# 
# Path: golem/core/serialize.py
# def json_serialize(obj):
#     from datetime import datetime
#     # from golem.core.entities import Entity
#     if isinstance(obj, datetime):
#         return {'__type__':'datetime', 'value': obj.isoformat()}
#     elif isinstance(obj, EntityValue):
#         data = b64encode(pickle.dumps(obj))
#         return {"__data__": data.decode('utf8'), '__type__': 'entity'}
#     raise TypeError ("Error saving entity value. Type %s not serializable: %s" % (type(obj), obj))


import json
from golem.core.responses.responses import MessageElement, _get_payload_string
from golem.core.serialize import json_serialize



class QuickReply(MessageElement):
    """
    A button with text that gets sent as message when clicked.
    """

    def __init__(self, title=None, payload=None, image_url=None):
        self.title = title
        self.payload = payload
        self.image_url = image_url

    def to_response(self):
        response = {
            "content_type": 'text',
            'title': self.title[:20],
            'payload': json.dumps(self.payload if self.payload else {}, default=json_serialize)
        }
        if self.image_url:
            response['image_url'] = self.image_url
        return response

    def __str__(self):
        text = 'quick_reply: '
        if self.title:
            text += self.title + ': '
        if self.payload:

# Path: golem/core/responses/responses.py
# class MessageElement:
#     """
#     Base class for message elements.
#     """
#     def to_message(self, fbid):
#         return {"recipient": {"id": fbid}, "message": self.to_response()}
# 
#     def to_response(self):
#         pass
# 
#     def get_response_for(self, platform='fb'):
#         return str(self)  # TODO
# 
#     def set_message_tag(self, tag):
#         self.tag = tag
# 
#     def get_message_tag(self):
#         """
#         See https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags
#         :return: FB message tag
#         """
#         if hasattr(self, "tag"):
#             return self.tag
#         return None
# 
#     def __str__(self):
#         return str(self.__dict__)
# 
#     def __repr__(self):
#         return str(self)
# 
# def _get_payload_string(payload):
#     text = ''
#     for entity, values in payload.items():
#         if isinstance(values, list):
#             for value in values:
#                 text += '/{}/{}/ '.format(entity, value)
#             break
#         text += '/{}/{}/ '.format(entity, values)
#     return text
# 
# Path: golem/core/serialize.py
# def json_serialize(obj):
#     from datetime import datetime
#     # from golem.core.entities import Entity
#     if isinstance(obj, datetime):
#         return {'__type__':'datetime', 'value': obj.isoformat()}
#     elif isinstance(obj, EntityValue):
#         data = b64encode(pickle.dumps(obj))
#         return {"__data__": data.decode('utf8'), '__type__': 'entity'}
#     raise TypeError ("Error saving entity value. Type %s not serializable: %s" % (type(obj), obj))


# Path: golem/core/responses/quick_reply.py
import json
from golem.core.responses.responses import MessageElement, _get_payload_string
from golem.core.serialize import json_serialize




class QuickReply(MessageElement):
    """
    A button with text that gets sent as message when clicked.
    """

    def __init__(self, title=None, payload=None, image_url=None):
        self.title = title
        self.payload = payload
        self.image_url = image_url

    def to_response(self):
        response = {
            "content_type": 'text',
            'title': self.title[:20],
            'payload': json.dumps(self.payload if self.payload else {}, default=json_serialize)
        }
        if self.image_url:
            response['image_url'] = self.image_url
        return response

    def __str__(self):
        text = 'quick_reply: '
        if self.title:
            text += self.title + ': '
        if self.payload:

            text += _get_payload_string(self.payload)