from datetime import datetime, timedelta

from dateutil.relativedelta import relativedelta
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
from django_extensions.db.fields import AutoSlugField
from django_extensions.db.models import TimeStampedModel

from remember.managers import PublishedManager


class PublishExpiringModel(TimeStampedModel):
    expires_on = models.DateTimeField(_('Expires on'), default=datetime.today()+timedelta(days=7), blank=True, null=True)
    published = models.BooleanField(_('Published'), default=False)
    published_on = models.DateTimeField(_('Published on'))

    objects = PublishedManager()

    class Meta:
        abstract=True

class RemembrancePhoto(PublishExpiringModel):
    name = models.CharField(_('Name'), max_length=100)
    image = models.ImageField(_('Image'), upload_to='remember/photos/')

    class Meta:
	ordering = ('-published_on',)

    def __unicode__(self):
        return u'%s' % (self.name)
    

class InMemoriam(PublishExpiringModel):
    '''In Memorum model.
    Effectively an anniversary obit. Runs anytime, not just at TOD.'''
    title = models.CharField(_('Title'), max_length=140)
    slug = AutoSlugField(_('slug'), populate_from='title', editable=True)
    dateline = models.CharField(_('Dateline'), blank=True, null=True, max_length=100)
    name = models.CharField(_('Name'), max_length=70, blank=True, null=True)
    copy = models.TextField(_('Copy'))
    photos = models.ManyToManyField(RemembrancePhoto, blank=True, null=True)
    military = models.BooleanField(_('Military service?'), default=False)

    class Meta:
	ordering = ('-published_on',)
        verbose_name = _('In memoriam')
        verbose_name_plural = _('In memoriams')
        
    def __unicode__(self):
        return u'%s' % (self.title)    
        
    #@models.permalink
    #def get_absolute_url(self):
    #    return ('rm-inmemoriam-detail', None, {'slug': self.slug})

class Remembrance(PublishExpiringModel):
    '''Remembrance model.
       An obituary by any other name.'''
    name = models.CharField(_('Name'), max_length=100)
    slug = AutoSlugField(_('slug'), populate_from='name', editable=True)
    dateline = models.CharField(_('Dateline'), blank=True, null=True, max_length=100)
    body = models.TextField(_('Body'))
    photos = models.ManyToManyField(RemembrancePhoto, blank=True, null=True)
    military = models.BooleanField(_('Military service?'), default=False)
        
    class Meta:
	ordering = ('-published_on',)
        verbose_name = _('Remembrance')
        verbose_name_plural = _('Remembrances')
            
    def __unicode__(self):
        return u'%s' % self.name
        
    #@models.permalink
    #def get_absolute_url(self):
    #    return ('rm-remembrance-detail', None, {'slug': self.slug})
         

class Announcement(PublishExpiringModel):
    '''Announcement model.
    A simple announcement of a death.'''
    first_name = models.CharField(_('First name'), max_length=70)
    middle_name = models.CharField(_('Middle name'), max_length=70, blank=True, null=True)
    last_name = models.CharField(_('Last name'), max_length=70)
    slug = AutoSlugField(_('slug'), populate_from=['first_name', 'middle_name', 'last_name'], editable=True)
    copy = models.TextField(_('Copy'))
    '''
    nickname = models.CharField(_('Nickname'), blank=True, null=True, max_length=100)
    residence = models.CharField(_('Residence'), blank=True, null=True, max_length=100)
    fixed_age = models.IntegerField(_('Age'), max_length=3, blank=True, null=True)
    birth_year = models.IntegerField(max_length=4, blank=True, null=True)
    birth_date = models.DateField(blank=True, null=True)
    death_date = models.DateField(blank=True, null=True)
    death_location = models.CharField(_('Death location'), blank=True, null=True, max_length=200)
    remembrance = models.ForeignKey(Remembrance, blank=True, null=True)
    service_date = models.DateTimeField(blank=True, null=True)
    service_location = models.CharField(blank=True, null=True, max_length=200)
    armed_service = models.BooleanField(_('Armed service'), default=False)
    service_branch = models.CharField(_('Service branch'), blank=True, null=True, max_length=20)'''

    class Meta:
	ordering = ('-published_on',)
        verbose_name=_('announcement')
        verbose_name_plural=_('annoucements')

    @property
    def full_name(self):
        if self.middle_name:
            name = u'%s %s. %s' % (self.first_name, self.middle_name[0].capitalize(), self.last_name)
        else:
            name = u'%s %s' % (self.first_name, self.last_name)
        return name


    def __unicode__(self):
        return u'%s' % self.full_name

    '''@property
    def age(self):
        if self.fixed_age:
            age = self.fixed_age
        else:
            if self.death_date:
                DELTA=self.death_date
            else:
                DELTA=datetime.today()
            age = relativedelta(DELTA, self.birth_date).years
        return u'%s' % relativedelta(DELTA, self.birth_date).years'''

    #@models.permalink
    #def get_absolute_url(self):
    #    return ('rm-announcement-detail', None, {'slug': self.slug})

class Service(PublishExpiringModel):
    '''Service model.'''
    announcement = models.ForeignKey(Announcement, blank=True, null=True)
    name = models.CharField(_('Title'), max_length=100, blank=True, null=True)
    slug = AutoSlugField(_('slug'), populate_from='name', editable=True)
    location = models.CharField(_('Location'), max_length=200, blank=True, null=True)
    date = models.DateTimeField(_('Date and time'), blank=True, null=True)
    body = models.TextField(_('Body'))

    class Meta:
        verbose_name = _('sevice')
        verbose_name_plural = _('services')

    def __unicode__(self):
        if self.announcement:
            return self.announcement.slug + " memorial service"
        else:
            return u'%s' % self.name

    #@models.permalink
    #def get_absolute_url(self):
    #    return ('rm-service-detail', None, {'slug': self.slug})
