import os

try:
    from django.template import TemplateDoesNotExist
    from django.template.loaders import filesystem, app_directories
    _django_available = True
except ImportError, e:
    class TemplateDoesNotExist(Exception):
        pass

    _django_available = False

from hamlpy import hamlpy
from hamlpy.template.utils import get_django_template_loaders


# Get options from Django settings
options_dict = {}

if _django_available:
    from django.conf import settings
    if hasattr(settings, 'HAMLPY_ATTR_WRAPPER'):
        options_dict.update(attr_wrapper=settings.HAMLPY_ATTR_WRAPPER)


def get_haml_loader(loader):
    if hasattr(loader, 'Loader'):
        baseclass = loader.Loader
    else:
        class baseclass(object):
            def load_template_source(self, *args, **kwargs):
                return loader.load_template_source(*args, **kwargs)

    class Loader(baseclass):
        def load_template_source(self, template_name, *args, **kwargs):
            name, _extension = os.path.splitext(template_name)
            # os.path.splitext always returns a period at the start of extension
            extension = _extension.lstrip('.')

            if extension in hamlpy.VALID_EXTENSIONS:
                try:
                    haml_source, template_path = super(Loader, self).load_template_source(
                        self._generate_template_name(name, extension), *args, **kwargs
                    )
                except TemplateDoesNotExist:
                    pass
                else:
                    hamlParser = hamlpy.Compiler(options_dict=options_dict)
                    html = hamlParser.process(haml_source)

                    return html, template_path

            raise TemplateDoesNotExist(template_name)

        def get_contents(self, origin):
            name, _extension = os.path.splitext(origin.name)
            for extension in hamlpy.VALID_EXTENSIONS:
                try:
                    origin.name = self._generate_template_name(name, extension)
                    haml_source = super(Loader, self).get_contents(origin)
                except TemplateDoesNotExist:
                    pass
                else:
                    hamlParser = hamlpy.Compiler(options_dict=options_dict)
                    html = hamlParser.process(haml_source)

                    return html

            return TemplateDoesNotExist(origin)

        load_template_source.is_usable = True

        def _generate_template_name(self, name, extension="hamlpy"):
            return "%s.%s" % (name, extension)

    return Loader


haml_loaders = dict((name, get_haml_loader(loader))
                    for (name, loader) in get_django_template_loaders())

if _django_available:
    HamlPyFilesystemLoader = get_haml_loader(filesystem)
    HamlPyAppDirectoriesLoader = get_haml_loader(app_directories)
