"""Message functionality for Hermes"""
from datetime import datetime
import json


__author__ = "Michael Anckaert"
__copyright__ = "Copyright 2012, Michael Anckaert"
__credits__ = ["Michael Anckaert"]
__license__ = "GPLv3"
__version__ = "0.0.1"
__maintainer__ = "Michael Anckaert"
__email__ = "michael.anckaert@sinax.be"
__status__ = "Development"


class MessageType(object):
    types = {}

    def __init__(self, name):
        if name in MessageType.types:
            print " W: Message type '{0}' already exists".format(name)
            raise ValueError("Message type '{}' already exists.".format(name))
        self.name = name
        self.rest_enabled = False
        MessageType.types['name'] = self

    def enable_rest(self):
        self.rest_enabled = True

    def disable_rest(self):
        self.rest_enabled = False

    def get_message_type(self, name):
        if name in MessageType.types.items():
            return MessageType.types[name]
        else:
            return None



class Message(object):
    def __init__(self, type, content):
        if MessageType.get_message_type(type):
            self.type = type
            self.content = content

            self.id = None
            self.status = "UNKNOWN"
            self.received = datetime.now().strftime("%d-%m-%Y %H:%M")
            self.response = None
            return

        print " W: Unknown message type '{0}' ".format(type)
        raise ValueError("Wrong message type!")

    def __str__(self):
        return json.dumps({'message': {'id': self.id, 'status': self.status, 'received': self.received, 'response': self.response}})

