# w.c.s. - web application for online forms
# Copyright (C) 2005-2010  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from django.utils.encoding import force_text
from quixote import get_publisher

from .qommon import _, misc
from .qommon.storage import StorableObject

class Role(StorableObject):
    _names = 'roles'
    _indexes = ['uuid', 'slug']

    name = None
    uuid = None
    slug = None
    internal = False
    details = None
    emails = None
    emails_to_members = False
    allows_backoffice_access = True

    def __init__(self, name=None, id=None):
        StorableObject.__init__(self, id=id)
        self.name = name

    def migrate(self):
        changed = False
        if not self.slug:
            # .store() will take care of setting the slug
            changed = True
        if changed:
            self.store()

    def store(self):
        if self.slug is None:
            # set slug if it's not yet there
            self.slug = self.get_new_slug()
        super(Role, self).store()

    def get_new_slug(self):
        new_slug = misc.simplify(self.name)
        base_new_slug = new_slug
        suffix_no = 0
        while True:
            try:
                role = self.get_on_index(new_slug, 'slug', ignore_migration=True)
            except KeyError:
                break
            if role.id == self.id:
                break
            suffix_no += 1
            new_slug = '%s-%s' % (base_new_slug, suffix_no)
        return new_slug

    def get_emails(self):
        emails = self.emails or []
        if not self.emails_to_members:
            return emails
        users_with_roles = get_publisher().user_class.get_users_with_role(self.id)
        emails.extend([x.email for x in users_with_roles if x.email])
        return emails

    def is_internal(self):
        return self.internal

    def get_substitution_variables(self, prefix=''):
        data = {}
        data[prefix + 'name'] = self.name
        data[prefix + 'details'] = self.details or ''
        data[prefix + 'emails'] = ', '.join(self.emails or [])
        return data

    def get_json_export_dict(self):
        charset = get_publisher().site_charset
        return {
            'name': force_text(self.name, charset),
            'text': force_text(self.name, charset), # generic key
            'allows_backoffice_access': self.allows_backoffice_access,
            'emails': [force_text(email, charset) for email in self.emails or []],
            'details': force_text(self.details or '', charset),
            'emails_to_members': self.emails_to_members,
            'slug': force_text(self.slug, charset),
            'id': self.id}

    @classmethod
    def resolve(cls, uuid, slug=None, name=None):
        try:
            return cls.get_on_index(uuid, 'uuid')
        except KeyError:
            pass
        try:
            return cls.get(uuid)
        except KeyError:
            pass
        try:
            return cls.get_on_index(uuid, 'slug')
        except KeyError:
            pass
        if slug:
            try:
                return cls.get_on_index(slug, 'slug')
            except KeyError:
                pass
        if name:
            for role in cls.select():
                if role.name == name:
                    return role
        return None


def logged_users_role():
    volatile_role = Role.volatile()
    volatile_role.id = 'logged-users'
    volatile_role.name = _('Logged Users')
    return volatile_role


def get_user_roles():
    t = sorted([(misc.simplify(x.name), x.id, x.name, x.id) for x in Role.select() if not x.is_internal()])
    return [x[1:] for x in t]
