from coffin import template
from coffin.template.loader import get_template
register = template.Library()
from django.conf import settings
from cropduster.models import Size
from cropduster.models import AUTO_SIZE
from os.path import exists
from raven import Client
import sys

CROPDUSTER_CROP_ONLOAD = getattr(settings, "CROPDUSTER_CROP_ONLOAD", True)
CROPDUSTER_PLACEHOLDER_MODE = getattr(settings, "CROPDUSTER_PLACEHOLDER_MODE", False)


# preload a map of image sizes so it doesn"t make a DB call for each templatetag use
IMAGE_SIZE_MAP = {}
for size in Size.objects.all():
	IMAGE_SIZE_MAP[(size.size_set_id, size.slug)] = size


@register.object
def get_image(image, size_name=None, template_name="image.html", retina=False, **kwargs):
	""" Templatetag to get the HTML for an image from a cropduster image object """

	if image:
		
		if CROPDUSTER_CROP_ONLOAD:
		# If set, will check for thumbnail existence
		# if not there, will create the thumb based on predefiend crop/size settings
		
			thumb_path = image.thumbnail_path(size_name)
			if not exists(thumb_path) and exists(image.image.path):
				try:
					size = image.size_set.size_set.get(slug=size_name)
				except Size.DoesNotExist:
					return ""
				try:
					image.create_thumbnail(size, force_crop=True)
				except:
					client = Client(dsn=settings.RAVEN_CONFIG['dsn'])
				        client.captureException(exc_info=sys.exc_info())			
					return ""
		
		if retina:	
			image_url = image.retina_thumbnail_url(size_name)
		else:
			image_url = image.thumbnail_url(size_name)
			
		
		if not image_url:
			return ""
			
		try:
			image_size = IMAGE_SIZE_MAP[(image.size_set_id, size_name)]
		except KeyError:
			return ""
	
		# Set all the args that get passed to the template
		
		kwargs["image_url"] = image_url

		if hasattr(image_size, "auto_size") and image_size.auto_size != AUTO_SIZE:
			kwargs["width"] = image_size.width if hasattr(image_size, "width") else ""
			kwargs["height"] = image_size.height if hasattr(image_size, "height") else ""
		
		
		if CROPDUSTER_PLACEHOLDER_MODE:
			kwargs["image_url"] = "http://placehold.it/%sx%s" % (kwargs["width"], kwargs["height"])
		
		kwargs["size_name"] = size_name
		
		kwargs["attribution"] = image.attribution
		
		if hasattr(image, "caption"): kwargs["alt"] = image.caption 
		
		if "title" not in kwargs: kwargs["title"] = kwargs["alt"] 

		tmpl = get_template("templatetags/" + template_name)
		context = template.Context(kwargs)
		return tmpl.render(context)
	else:
		return ""

	
		
		
