##############################################################################
# Copyright (c) 2004-2005 Black Rock City, LLC
# Copyright (c) 2004-2005 ObjectRealms, LLC
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""
Description:

   TeamsSpaces are collaborative environments associated
   with a Team.

$Id: space.py 50694 2007-10-03 03:09:12Z rafrombrc $
"""
from zope.interface import implements

from Products.TeamSpace.relations import TeamSpaceTeamRelation
from Products.TeamSpace import permissions
from Products.TeamSpace.zimport import *
from Products.TeamSpace.interfaces.space import ISpace
from Products.TeamSpace.interfaces.security import ITeamSecurity

TeamSpaceSchema = Schema((
    ReferenceField( 'space_teams',
                    relationship = TeamSpaceTeamRelation.relationship,
                    referenceClass = TeamSpaceTeamRelation,
                    vocabulary = 'getAllTeamsDisplayList',
                    mutator    = 'setSpaceTeams',
                    accessor   = 'getSpaceTeams',
                    multiValued = 1,
                    widget = InAndOutWidget( label="Teams",
                                             label_msgid="label_teams",
                                             description="The teams that are associated with this Team Space",
                                             description_msgid = "description_teams",
                                             ),
                    ),
    ))


class TeamSpaceMixin(object):
    """ TeamSpace is a container for collaboration """

    security = ClassSecurityInfo()

    implements(ISpace, ITeamSecurity)

    def getTeams(self):
        """
        return teams associated with this teamspace
        """
        teams = self.getRefs(relationship=TeamSpaceTeamRelation.relationship )
        return teams

    security.declareProtected(permissions.ManageTeamSpace, 'editTeams')
    def editTeams(self, team_ids):
        """
        edit the teams associated with this teamspace, defer to schema mutator
        """
        team_ids = filter(None, team_ids)
        teams_tool = getToolByName(self, 'portal_teams')
        team_uids = [ teams_tool._getOb(tid).UID() for tid in team_ids \
                      if teams_tool._getOb(tid, None)]
        return self.Schema()['space_teams'].getMutator(self)(team_uids)

    #################################
    # display lists for the ui

    security.declareProtected(permissions.ManageTeamSpace, 'getAllTeamsDisplayList')
    def getAllTeamsDisplayList(self):
        team_tool = getToolByName(self, 'portal_teams')
        display_vars = tuple([(team.UID(), team.title_or_id()) \
                              for team in team_tool.getTeams()])
        return DisplayList(display_vars)

    security.declareProtected(permissions.ManageTeamSpace, 'getSpaceTeamsDisplayList')
    def getSpaceTeamsDisplayList(self):
        """
        """
        display_vars = tuple([(team.UID(), team.title_or_id()) for team in self._getTeamsForLocalRoles()])
        return DisplayList(display_vars)

    #################################
    # security mixin contract
    def _getTeamsForLocalRoles(self):
        return self.getTeams()


    #################################
    # member event notifications
    def _updateMember(self, operation, member, membership, team):
        """
        Stub method called by Team.addMember.  Hacky replacement for
        an event handler, can be overridden in subclasses.
        """
        pass

InitializeClass(TeamSpaceMixin)

class TeamSpace(TeamSpaceMixin, BaseFolder):
    meta_type = 'Team Space'
    archetype_name = meta_type
    content_icon = 'teamspace_icon.png'
    _at_rename_after_creation = True
    schema = BaseSchema.copy() + TeamSpaceSchema

    def __init__(self, id, title=''):
        BaseFolder.__init__(self, id)
        self.title = title or self.meta_type

registerType(TeamSpace)

