from django.conf import settings
from coffin.shortcuts import render_to_response
from coffin.template import dict_from_django_context
from coffin.template.loader import get_template, select_template, render_to_string

'''Monkeypatch Django to mimic Jinja2 behaviour'''
from django.utils import safestring
if not hasattr(safestring, '__html__'):
    safestring.SafeString.__html__ = lambda self: str(self)
    safestring.SafeUnicode.__html__ = lambda self: unicode(self)

class Theme(object):
    def __init__(self, theme):
        self.set_theme(theme);

    def set_theme(self, theme):
        if theme not in settings.INSTALLED_THEMES:
            raise ValueError('"%s" is not correct theme name.' % theme)
        if theme[0] == '_':
            raise ValueError('"%s" is a template package, not a theme.' % theme[1:])
        self._theme = theme;

    def reset_theme(self):
        self._theme = settings.INSTALLED_THEMES[0]

    def get_theme(self):
        return self._theme

    def prefix_templates(self, templates):
        if isinstance(templates, str):
            return ('%s/%s' % (self._theme, templates), templates)
        else:
            prefixed = []
            for template in templates:
                prefixed.append('%s/%s' % (self._theme, template))
            prefixed += templates
            return prefixed

    def render_to_string(self, templates, *args, **kwargs):
        templates = self.prefix_templates(templates)
        return render_to_string(templates, *args, **kwargs)

    def render_to_response(self, templates, *args, **kwargs):
        templates = self.prefix_templates(templates)
        return render_to_response(templates, *args, **kwargs)

    def macro(self, templates, macro, dictionary={}, context_instance=None):
        templates = self.prefix_templates(templates)
        template = select_template(templates)
        if context_instance:
            context_instance.update(dictionary)
        else:
            context_instance = dictionary
        context_instance = dict_from_django_context(context_instance)
        _macro = getattr(template.make_module(context_instance), macro)
        return unicode(_macro()).strip()

    def get_email_templates(self, template, contex={}):
            email_type_plain = '_email/%s.txt' % template
            email_type_html = '_email/%s.html' % template
            return (
                    select_template(('%s/%s' % (self._theme, email_type_plain[1:]), email_type_plain)),
                    select_template(('%s/%s' % (self._theme, email_type_html[1:]), email_type_html)),
                    )
