# -*- coding: utf-8 -*-
from django.db import models
from polymorphic import PolymorphicModel
from coin.offers.models import OfferSubscription

"""
Implementation note : Configuration is a PolymorphicModel.
The childs of Configuration are the differents models to store
technical informations of a subscibtion.

To add a new configuration backend, you have to create a new app with a model
which inherit from Configuration.
Your model can implement Meta verbose_name to have human readable name and a 
url_namespace variable to specify the url namespace used by this model.
"""

class Configuration(PolymorphicModel):

    offersubscription = models.OneToOneField(OfferSubscription, blank=True, 
                                             null=True,
                                             related_name='configuration',
                                             verbose_name='Abonnement')

    @staticmethod
    def get_configurations_choices_list():
        """
        Génère automatiquement la liste de choix possibles de configurations
        en fonction des classes enfants de Configuration
        """
        return tuple((x().__class__.__name__,x()._meta.verbose_name) 
            for x in Configuration.__subclasses__())
    
    def model_name(self):
        return self.__class__.__name__
    model_name.short_description = 'Nom du modèle'

    def configuration_type_name(self):
        return self._meta.verbose_name
    configuration_type_name.short_description = 'Type'

    def get_absolute_url(self):
        """
        Renvoi l'URL d'accès à la page "details" de l'objet
        Une url doit être nommée "details"
        """
        from django.core.urlresolvers import reverse
        return reverse('%s:details' % self.get_url_namespace(), 
                       args=[str(self.id)])

    def get_url_namespace(self):
        """
        Renvoi le namespace utilisé par la configuration. Utilise en priorité
        celui définit dans la classe enfant dans url_namespace sinon
        par défaut utilise le nom de la classe en minuscule
        """
        if self.url_namespace:
            return self.url_namespace
        else:
            return self.model_name().lower()
