from django.db import models
from os.path import join
from sorl.thumbnail import get_thumbnail

def project_thumb_path(instance, filename):
    return join('project_thumbs', instance.slug, filename)


class Project(models.Model):
    position = models.IntegerField()
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    thumb_image = models.ImageField(upload_to=project_thumb_path)
    size_program = models.TextField(blank=True)
    design_statement = models.TextField(blank=True)
    extra_text = models.TextField(blank=True)
#    start_date = models.DateField(blank=True)
#    end_date = models.DateField(blank=True)
#    location = models.CharField(max_length=255, blank=True)

    def save(self, *args, **kwargs):
        model = self.__class__
        
        if self.position is None:
            # Append
            try:
                last = model.objects.order_by('-position')[0]
                self.position = last.position + 1
            except IndexError:
                # First row
                self.position = 0
        
        return super(Project, self).save(*args, **kwargs)

    class Meta:
        ordering = ['position']

    def get_info(self):
        return {
            'id': self.id,
            'title': self.title,
            'slug': self.slug,
            'thumb_image': self.thumb_image.url,
            'size_program': self.size_program,
            'design_statement': self.design_statement,
            'extra_text': self.extra_text,
            'images': self.get_images()
        }


    def get_images(self):
        imgs = []
        for i in self.projectimage_set.all():
            image_url = get_thumbnail(i.image, "x450", crop="center").url 
            imgs.append({
                'url': image_url,
                'caption': i.caption
            })
        return imgs

    def __unicode__(self):
        return self.title


class ProjectImage(models.Model):
    image = models.ImageField(upload_to='project_imgs/')
    caption = models.CharField(max_length=255, blank=True)
    project = models.ForeignKey(Project)

    def __unicode__(self):
        return self.caption

    class Meta:
        ordering = ['id']

class SliderImage(models.Model):
    image = models.ImageField(upload_to='slider_imgs/')
    caption = models.CharField(max_length=255, blank=True)
    order = models.IntegerField(default=1)

    def __unicode__(self):
        return self.caption

    class Meta:
        ordering = ['order']


class Link(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    url = models.URLField(blank=True)

    def __unicode__(self):
        return self.title


class Noteworthy(models.Model):
    position = models.IntegerField(null=True)
    image = models.ImageField(blank=True, upload_to='noteworthy_imgs/', help_text="main image")
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    url = models.URLField(blank=True)

    class Meta:
        ordering = ['position']


    def save(self, *args, **kwargs):
        model = self.__class__
        
        if self.position is None:
            # Append
            try:
                last = model.objects.order_by('-position')[0]
                self.position = last.position + 1
            except IndexError:
                # First row
                self.position = 0
        
        return super(Noteworthy, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title


class NoteworthyImage(models.Model):
    noteworthy = models.ForeignKey(Noteworthy)
    image = models.ImageField(upload_to='noteworthy_imgs/')    
    order = models.IntegerField(default=1)
    name = models.CharField(max_length=128, blank=True)

    def __unicode__(self):
        return self.image.url

    class Meta:
        ordering = ['order', 'id']


# Create your models here.

