import os

__author__ = 'hanl'

path = os.path.dirname(__file__)
# modify this to change the Template Directory
TEMPLATE_DIR = '/templates/'


class Handler(object):
    def register(self, key, value):
        setattr(self, key, value)


    def get(self, key):
        if hasattr(self, key):
            return getattr(self, key)



# for possible email usage
class MessageTemplate(object):
    def __init__(self, template_name=None, html=True, values={}):
        self.values = values
        self.template_name = template_name
        self.html = html

    def render(self):
        content = open(os.path.join(path, TEMPLATE_DIR, self.template_name)).read()
        for k, v in self.values.iteritems():
            content = content.replace('%%s%%' % k, v)
        return content


class HtmlBuilder(object):
    def __init__(self):
        self.title = None
        self.head = None
        self.body = None

    def appendHead(self, head):
        if not self.head:
            self.head = ""
        self.head.append("\n" + head)

    def appendBody(self, body):
        if not self.body:
            self.body = ""
        self.head.append("\n" + body)




class NotificationHandler(object):

    @staticmethod
    def isError(response):
        try:
            raw_json = response.json()
            if raw_json.get('errors') or raw_json.get('error') or raw_json.get('err'):
                return True
        except ValueError:
            if response.status_code != 200:
                return True
        return False

    @staticmethod
    def getMessage(response):
        pass

    @staticmethod
    def notify(json, notify_func, **kwargs):
        if "category" not in kwargs.keys():
            kwargs['category'] = "danger"

        if json.get('errors'):
            for error in json.get("errors"):
                notify_func(error[1], "danger")
        elif json.get('error'):
            if json.get('error_description'):
                notify_func(json.get('error_description'), **kwargs)
            else:
                notify_func(json.get('error'), **kwargs)
        elif json.get('err'):
            notify_func(json.get('errstr'), **kwargs)

    @staticmethod
    def getLocalized(code):
        # return _("status_" + str(code))
        return "this is a default message"

