"""This module provide ``QuestionWall`` and ``QuestionWallIdea`` models for the ``ideas`` app"""

from django.core.urlresolvers import reverse
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import ugettext_lazy as _

from mtp.apps.broker_models_hooks.models import BrokerHookMixin
from mtp.apps.sync.models import Syncable
from mtp.apps.trainings.models import TrainingActivity

from ..managers import IdeaManager

from .ideas import Idea, IdeasActivity


class WordCloud(TrainingActivity, IdeasActivity):
    """Django model defining a "word cloud" activity

    This model does not contains any fields, besides those provided by ``TrainingActivity``,
    but the ``WordCloudIdea`` below is linked to this model.

    """

    verbose_activity_name = _('Wordcloud')

    @classmethod
    def get_create_url(cls, training):
        """Get the url to create a new ``WordCloud`` instance

        Parameters
        ----------
        training: Training
            The training we want to create a new ``WordCloud`` for.

        Returns
        -------
        string

        """

        return reverse('trainings:wordcloud-create', kwargs={'training_uuid': training.pk})

    def get_absolute_url(self):
        """Get the absolute url of the current ``WordCloud``

        It actually returns the edit url, as there is no "detail" view of a ``WordCloud``

        Returns
        -------
        string

        """

        return self.get_edit_url()

    def get_edit_url(self):
        """Get the url to edit the current ``WordCloud``

        Returns
        -------
        string

        """

        return reverse('ideas:wordcloud-update', kwargs={'wordcloud_uuid': self.pk})


class WordCloudIdea(BrokerHookMixin, Syncable, Idea):
    """Django model defining a "word cloud" idea

    Attributes
    ----------
    text: django ``CharField`` field. Maximum 30 chars.
        The textual content of this idea.
    wordcloud: django ``ForeignKey`` field to ``WordCloud``.
        The word cloud this idea was proposed into.

    """

    text = models.CharField(
        max_length=30,
        verbose_name=_('Text'),
        validators=[
            RegexValidator(
                regex=r'^[^\s]+$',
                message=_('No spaces are allowed'),
            ),
        ]
    )
    wordcloud = models.ForeignKey(
        WordCloud,
        related_name='ideas',
        verbose_name=_('Word cloud'),
        on_delete=models.CASCADE
    )

    objects = IdeaManager()

    class Meta:
        unique_together = ['wordcloud', 'session', 'sort_order']

    def get_broker_topic(self):
        """Get the broker topic to publish changes of this model

        Returns
        -------
        str

        """

        return '%s/wordclouds/%s/ideas' % (
            self.session_id,
            self.wordcloud_id
        )

    def get_serialized_representation(self):
        """Get the serialized representation of the current idea

        Returns
        -------
        dict

        """

        # Avoid circular imports
        from mtp.apps.ideas.apiv2.serializers import WordCloudIdeaSerializer

        return WordCloudIdeaSerializer(self).data
