from collective.workspace.interfaces import IWorkspace
from dexterity.membrane.behavior.group import MembraneGroup
from plone import api
from Products.membrane.interfaces import IGroup
from Products.membrane.interfaces import IMembraneGroupProperties
from Products.PlonePAS.sheet import MutablePropertySheet
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface


class IMembraneGroupMarker(Interface):
    """Marker interface."""


class IMembraneGroup(Interface):
    """Marker interface for Membrane Group"""


@implementer(IGroup)
@adapter(IMembraneGroupMarker)
class MembraneWorkspaceGroup:
    def __init__(self, context):
        self.context = context

    def getId(self):
        return self.context.getId()

    def getGroupId(self):
        return self.context.getId()

    def getGroupName(self):
        return self.context.title

    def getRoles(self):
        return ()

    def getGroupMembers(self):
        ws = IWorkspace(self.context)
        if not ws:
            return []
        return tuple({id for (id, details) in ws.members.items()})


@implementer(IMembraneGroupProperties)
@adapter(IMembraneGroupMarker)
class MembraneGroupProperties(MembraneGroup):
    """Properties for our membrane group"""

    def getPropertiesForUser(self, user, request=None):
        """Get properties for this group."""
        properties = {}
        ws_obj = api.portal.get().workspaces.get(user.getId(), None)
        # Pick the first result. Ignore potential catalog problems
        if ws_obj:
            portal_path = api.portal.get().getPhysicalPath()
            properties["title"] = ws_obj.title
            properties["description"] = ws_obj.description
            path = "/" + "/".join(ws_obj.getPhysicalPath()[len(portal_path) :])
            properties["workspace_path"] = path
            state = api.content.get_state(ws_obj)
            if ws_obj.visible_to_intranet_users:
                if ws_obj.accessible_to_intranet_users:
                    state = "open"
                else:
                    state = "visible"
            properties["state"] = state
            properties["uid"] = ws_obj.UID()
            properties["type"] = "workspace"
            properties["portal_type"] = ws_obj.portal_type
            # We need to explicitly cast the type to a string type, since
            # the property sheet pukes on zope.i18nmessageid.message.Message
            typ = ws_obj.Type()
            properties["workspace_type"] = typ
        return MutablePropertySheet(self.context.getId(), **properties)

    def setPropertiesForUser(self, user, propertysheet):
        """
        Set modified properties on the group persistently.
        Currently, we do nothing, since all properties are handled via
        directly editing the workspace properties
        """

    def deleteUser(self, user_id):
        """
        Remove properties stored for a user

        Note that membrane itself does not do anything here.  This
        indeed seems unneeded, as the properties are stored on the
        content item, so they get removed anyway without needing
        special handling.
        """


# Membrane Groups provider plugin for our workspaces is provided by
# ploneintranet.userprofile.adapters.MembraneGroupsProvider
