from datetime import *
from dateutil import relativedelta

from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.localflavor.us.models import PhoneNumberField, USStateField
from django_extensions.db.fields import AutoSlugField

from django_extensions.db.models import TimeStampedModel, TitleSlugDescriptionModel
from directory.models import Point, Person, Town, Place
from marketplace.models import Business, Guide
from newsroom.models import Story
from darkroom.models import Photo, Gallery, Slideshow, Movie
from documents.models import PDFDocument

class Category(TitleSlugDescriptionModel):
    """Categories model."""
    graphic=models.ImageField(_('Graphic'), upload_to='visitors/category/')
    brief_description=models.CharField(_('Brief description'), max_length=255,
            blank=True, null=True)
    teaser_name=models.CharField(_('Teaser name'), max_length=100,
            blank=True, null=True)
    template_name = models.CharField(_('template name'), max_length=70, 
                                  blank=True, 
                                  help_text=_("Example: 'visitors/categories/shopping.html'. If this isn't provided, the system will use 'visitors/categories/default.html'."))
    
    class Meta:
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')
        ordering=('title',)
        
    def __unicode__(self):
        return u'%s' % self.title
        

    def get_absolute_url(self):
        return reverse ('vs-category-index', args=[self.slug])


class Feature(TimeStampedModel):
    name=models.CharField(_('name'), max_length=200)
    slug=models.SlugField(_('slug'), unique=True)
    description=models.CharField(_('description'), max_length=255, blank=True, null=True) 
      
    class Meta:
	verbose_name=_('feature')
	verbose_name_plural=_('features')
	ordering=('name',)
  
    def __unicode__(self):
        return u'%s' % self.name

class Attraction(TimeStampedModel):
    '''
    Base class for attractions
    '''
        
    name=models.CharField(_('name'), max_length=255)
    slug=AutoSlugField(_('slug'), editable=True,
            populate_from='name')
    published=models.BooleanField(_('Published'), default=False)
    pub_date=models.DateField(_('Publish date'))
    point=models.ForeignKey(Point, blank=True, null=True)
    town=models.ForeignKey(Town, blank=True, null=True,
            help_text="Point above will override this if attraction given a point.")
    place=models.ForeignKey(Place, blank=True, null=True, 
            help_text='Use EITHER this or town or point fields.')
    owner=models.ForeignKey(Person, blank=True, null=True) 
    brief_hours=models.CharField(_('brief hours'), blank=True, null=True, 
            max_length=255)
    logo=models.ImageField(_('logo or photo'), upload_to="portal/logos/", 
            blank=True)
    phone=PhoneNumberField(_('primary phone'), blank=True, null=True)
    url=models.URLField(_('url'), blank=True, verify_exists=False)
    brief_hours=models.CharField(_('brief hours'), blank=True, null=True, 
            max_length=255)
    directions=models.CharField(_('directions'), blank=True, null=True, 
            max_length=255)
    admission=models.CharField(_('Admission'), blank=True, null=True,
            max_length=100)
    description=models.TextField(_('description'), blank=True, null=True)
    features=models.ManyToManyField(Feature, blank=True, null=True)
    categories=models.ManyToManyField(Category)
    related_business=models.ForeignKey(Business, blank=True, null=True)

    class Meta:
		verbose_name=_('Attraction')
		verbose_name_plural=_('Attractions')
		ordering=('point__town', 'town', 'name')
		get_latest_by='created'
        
    def __unicode__(self):
        return u'%s' % self.name
        
    def get_absolute_url(self):
        return reverse('vs-attraction-detail', args=[self.slug])

class Section(TimeStampedModel):
    '''
    Section model.

    A collection of attractions with a specific name that maps to a printed
    publication.

    Businesses may be selected using either whole categories of businesses, or
    by specifically specifying businesses to include, or a mix of both.
    '''
    title = models.CharField(_('Title'), max_length=255)
    slug=AutoSlugField(_('Slug'), editable=True, 
            populate_from='title')
    published=models.BooleanField(_('Published'), default=False)
    pub_date=models.DateField(_('Publish date'))
    featured=models.BooleanField(_('Featured'), default=False)
    featured_on=models.DateTimeField(_('Featured on'), default=datetime.now(), 
                                     blank=True, null=True)
    featured_until=models.DateTimeField(_('Featured until'), 
                                        blank=True, null=True)
    logo=models.ImageField(_('Logo'), upload_to="marketplace/section/logos/", 
                           blank=True)
    logo_thumbnail=models.ImageField(_('Thumbnail of logo (optionl)'), 
                        upload_to="marketplace/section/logos/thumbnails/", 
                        blank=True, null=True, 
                        help_text='An optional thumbnail of the logo. If not used, a thumbnail will be generated from the logo.')
    tag_line=models.CharField(_('Tag line'), max_length=255, 
                              blank=True, null=True)
    brief_description=models.CharField(_('Brief description'), max_length=255,
                                       blank=True, null=True)	
    description=models.TextField(_('Description'),blank=True,null=True)	
    categories=models.ManyToManyField(Category, blank=True, null=True)
    businesses=models.ManyToManyField(Business, blank=True, null=True)
    attractions=models.ManyToManyField(Attraction, blank=True, null=True)
    template_name = models.CharField(_('template name'), max_length=70, 
                                  blank=True, 
                                  help_text=_("Example: 'visitors/sections/outdoor_handbook.html'. If this isn't provided, the system will use 'visitors/sections/default.html'."))
        
    class Meta:
		verbose_name=_('Section')
		verbose_name_plural=_('Sections')
		get_latest_by='featured_on'
        
    def __unicode__(self):
        return u'%s' % (self.title)
        
    def get_absolute_url(self):
        args=[self.slug]
        return reverse('vs-section-detail', args=args)

class VisitorsEdition(TimeStampedModel):
    '''
    Visitors portal edition model.

    Similar to editions in the Newsroom application.
    '''

    title=models.CharField(_('Title'), max_length=100)
    brief_description=models.CharField(_('Brief description'), blank=True,
            null=True, max_length=255)
    featured_story=models.ForeignKey(Story, related_name='ve_featured_story', blank=True, null=True)
    featured_photo=models.ForeignKey(Photo, related_name='ve_featured_photo', blank=True, null=True)
    published_on=models.DateField(_('publish on'))
    published=models.BooleanField(_('published'), default=False)
    stories=models.ManyToManyField(Story, blank=True, null=True, help_text="Include the featured story in this list of all stories in this edition.")
    photos=models.ManyToManyField(Photo, blank=True, null=True, related_name="ve_photos")
    galleries=models.ManyToManyField(Gallery, blank=True, null=True, related_name="ve_galleries")
    movies=models.ManyToManyField(Movie, blank=True, null=True, related_name="ve_movies")
    slideshows=models.ManyToManyField(Slideshow, blank=True, null=True, related_name="ve_slideshows")
    pdfdocuments=models.ManyToManyField(PDFDocument, blank=True, null=True, related_name="ve_pdfdocs")
    guides=models.ManyToManyField(Guide, blank=True, null=True, related_name="ve_guides")
    sections=models.ManyToManyField(Section, blank=True, null=True, related_name="ve_sections")
    
    class Meta:
		verbose_name=_('Visitors edition')
		verbose_name_plural=_('Visitors editions')
		get_latest_by='published_on'
        
    def __unicode__(self):
        return u'Visitors edition - %s' % (self.published_on)
        
    def get_absolute_url(self):
        args=[self.slug]
        return reverse('vs-visitors-edition-detail', args=args)


