from django.db import models
from django.utils.translation import ugettext as _
from django.conf import settings
from django.contrib.sites.models import Site
from django.template import loader, Context, Template
from django.utils.safestring import mark_safe

from dagcategory.models import DAGCategory

INFO_PAGE_HELP_TEXT="""Info Pages can be written in <a href="http://daringfireball.net/projects/markdown/basics">Markdown</a> text.<br/>

Quick Reference:<br/><br/>

*emphasis* = <em>emphasis</em><br/><br/>
[a link](http://goeshere.com) = <a href="http://goeshere.com">a link</a><br/><br/>

Images and videos can be placed by typing the key surrounded by brackets as follows:<br/>

If your key was "mykey" you would embed it by typing: <code>{{ mykey }}</code><br/>All media will take up an entire line and as much height as required.<br/>
</code>
"""


class InfoPage(DAGCategory):
    """Info pages provide hierarchically categorizable "flat pages" with embeddable media, SEO, and customizable templates """
    body=models.TextField(_("Body"),help_text=_(INFO_PAGE_HELP_TEXT)) 
    title=models.CharField(_("Title"),help_text=_("Title on the page"),max_length=255,blank=True)
    url=models.CharField(max_length=100,db_index=True,blank=True,help_text=_("Add a url here (.e.g /about/) to override standard slug based url"))
    active=models.BooleanField(_("Document Active"),default=True)
    registration_required = models.BooleanField(_('registration required'), help_text=_("If this is checked, only logged-in users will be able to view the page."))
    template_name = models.CharField(_('template name'), max_length=70, blank=True,
        help_text=_("Example: 'infopages/about_pages.html'. If this isn't provided, the system will use 'infopages/default.html'."))

    class Meta:
        verbose_name=_("Info Page") 
        verbose_name_plural=_("Info Pages")

    def body_with_media(self):
        cx={}
        t=loader.get_template("infopages/media_substitutions.html")

        class em(dict):
            def __unicode__(self):
                return self["html"]
            def __str__(self): return self.__unicode__()

        for si in self.infoimage_set.all():
            d={"type":"image","url":si.image.url,"key":si.key,"caption":si.caption}
            cx[si.key]=em(d,html=mark_safe(t.render(Context(d))))
        for sv in self.infovideo_set.all():
            d={"type":"video","url":si.image.url,"key":si.key,"caption":si.caption}
            cx[si.key]=em(d,html=mark_safe(t.render(Context(d))))
        c=Context(cx)
        t=loader.get_template_from_string(self.body)
        return mark_safe(t.render(c))

    def get_absolute_url(self):
        if self.url: return self.url
        return "/%s/"%self.path.replace(self.DELIMETER,"/")

    def __unicode__(self):
        return self.name

class InfoImage(models.Model):
    key=models.SlugField(help_text=_("A unique string preferably lowercase with numbers,letters and dashes only, used to identify this item in the body text"))
    article=models.ForeignKey(InfoPage) 
    caption=models.TextField(blank=True)
    image=models.FileField(upload_to="infomedia/%Y/%m/%d/")
    last_updated=models.DateTimeField(_("Last Updated"),auto_now=True)
    
class InfoVideo(models.Model):
    key=models.SlugField(help_text=_("A unique string preferably lowercase with numbers,letters and dashes only, used to identify this item in the body text"))
    article=models.ForeignKey(InfoPage) 
    caption=models.TextField(blank=True)
    video=models.FileField(upload_to="infomedia/%Y/%m/%d/")
    last_updated=models.DateTimeField(_("Last Updated"),auto_now=True)
