from django.db import models
from django.contrib.contenttypes.models import ContentType


class Module(models.Model):
    title = models.CharField(max_length=255, help_text="Title of Module. eg. Best Practices")
    slug = models.SlugField(help_text="short name of module to be used for url. eg. bestpractices")
    about = models.TextField(blank=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return "/m/" + self.slug


class ModuleTab(models.Model):
    module = models.ForeignKey(Module)
    title = models.CharField(max_length=64, help_text="Display title for tab")
    slug = models.SlugField(help_text="short name for tab, to be used in url, eg. faq")
    text = models.TextField(help_text="text to show up in LHS for tab", blank=True)
    model = models.ForeignKey("ModelExtra", help_text="Database model object tab is linked to (chose from list or ask for one to be created.)")
    is_default = models.BooleanField(default=False, help_text="Whether this should be the default displayed tab.")
    order = models.IntegerField(default=1, help_text="Make number higher to send it lower in order (default will always show first)")    
    is_displayed = models.BooleanField(default=True, help_text="Uncheck to hide tab")
    
    class Meta:
        ordering = ['-is_default', 'order']

    def __unicode__(self):
        return self.title

    def model_class(self):
        return self.model.model.model_class()

    def get_absolute_url(self): #FIXME!!
        return "/m/%s/?tab=%s" % (self.module.slug, self.slug,)

    def get_list(self, options):
        return self.model_class().get_list(options)

    def get_dict(self):
        if self.model.has_list:
          page = self.get_list({})
        else:
          page = None
        return {
            'id': self.id,
            'slug': self.slug,
            'title': self.title,
            'text': self.text,
            'has_list': self.model.has_list,
            'page': self.get_list({}),
            'sorts': [s.get_dict() for s in self.model.modelsort_set.all()]
        }

class ModelExtra(models.Model):
    model = models.ForeignKey(ContentType, help_text="Chose from the list of death at your own peril.")
    friendly_name = models.CharField(max_length=128, help_text="Used as display name in Admin, never to user. eg. Best Practices FAQ")
    has_comments = models.BooleanField(default=False, help_text="Will there be comments attached to this?")
    buttons = models.ManyToManyField('ModelButton', through='ModelExtraButton', blank=True)    
    has_list = models.BooleanField(default=True, help_text="Is there an LHS listing for this?")    
    default_image = models.ImageField(upload_to='upload/images/model_defaults/', max_length=255, blank=True, help_text="Default image to show if, either, item has no image, or image to display if model does not have LHS list.")

    def __unicode__(self):
        return self.friendly_name


class ModelSort(models.Model):
    model = models.ForeignKey(ModelExtra)
    operator = models.CharField(max_length=1, help_text="This should be either + or -.")
    field_name = models.CharField(max_length=64, help_text="Name of field on which sorting should take place, eg. title")
    friendly_name = models.CharField(max_length=128, help_text="And now, what shall we call it for the user? eg. By Title (A-Z)")

    def __unicode__(self):
        return self.friendly_name

    def get_dict(self):
        return {
            'operator': self.operator,
            'field_name': self.field_name,
            'friendly_name': self.friendly_name
        }

class ModelButton(models.Model):
    name = models.CharField(max_length=64, help_text="Name of button, eg. Download")
    icon = models.ImageField(upload_to='upload/images/button_icons/', blank=True, help_text="pretty icon for the button")        
    template = models.TextField(blank=True, help_text="you should never need to edit this field. please don't.")

    def __unicode__(self):
        return self.name


class ModelExtraButton(models.Model):
    modelextra = models.ForeignKey(ModelExtra)
    modelbutton = models.ForeignKey(ModelButton)
    parameters = models.CharField(max_length=64, blank=True, help_text="if the button needs a parameter, give it here, if you dont know what this is, leave it blank.")

