# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.db.models import signals

from django_notify import notify
from django_notify.models import Subscription

from wiki import models as wiki_models
from wiki.models.pluginbase import ArticlePlugin
from wiki.core.plugins import registry
from wiki.plugins.notifications import settings
from wiki.plugins.notifications.util import get_title

class ArticleSubscription(ArticlePlugin, Subscription):
    
    def __unicode__(self):
        title = (_(u"%(user)s subscribing to %(article)s (%(type)s)") % 
                 {'user': self.settings.user.username,
                  'article': self.article.current_revision.title,
                  'type': self.notification_type.label})
        return unicode(title)
    
    class Meta:
        app_label = settings.APP_LABEL
    

def default_url(article, urlpath=None):
    if urlpath:
        return reverse('wiki:get', kwargs={'path': urlpath.path})
    return article.get_absolute_url()


def post_article_revision_save(**kwargs):
    instance = kwargs['instance']
    if kwargs.get('created', False):
        url = default_url(instance.article)
        filter_exclude = {'settings__user': instance.user}
        if instance.deleted:
            notify(_(u'Article deleted: %s') % get_title(instance), settings.ARTICLE_EDIT,
                   target_object=instance.article, url=url, filter_exclude=filter_exclude)
        elif instance.previous_revision:
            notify(_(u'Article modified: %s') % get_title(instance), settings.ARTICLE_EDIT,
                   target_object=instance.article, url=url, filter_exclude=filter_exclude)
        else:
            notify(_(u'New article created: %s') % get_title(instance), settings.ARTICLE_EDIT,
                   target_object=instance, url=url, filter_exclude=filter_exclude)
            
# Whenever a new revision is created, we notifý users that an article
# was edited
signals.post_save.connect(post_article_revision_save, sender=wiki_models.ArticleRevision,)

# TODO: We should notify users when the current_revision of an article is
# changed...

##################################################
# NOTIFICATIONS FOR PLUGINS
##################################################
for plugin in registry.get_plugins():
    
    notifications = getattr(plugin, 'notifications', [])
    for notification_dict in notifications:
        def plugin_notification(instance, **kwargs):
            if notification_dict.get('ignore', lambda x: False)(instance):
                return
            if kwargs.get('created', False) == notification_dict.get('created', True):
                url = None
                if notification_dict.has_key('get_url'):
                    url = notification_dict['get_url'](instance)
                else:
                    url = default_url(notification_dict['get_article'](instance))
                
                message = notification_dict['message'](instance)
                notify(message, notification_dict['key'],
                       target_object=notification_dict['get_article'](instance), url=url)

        signals.post_save.connect(plugin_notification, sender=notification_dict['model'])
