from plone import api
from Products.Five import BrowserView
from Products.PlonePAS.interfaces.group import IGroupData


class WorkspaceGroupView(BrowserView):
    """Helper view to show the members of a group when clicked in workspace"""

    def group_details(self):
        """Get group data"""
        gid = self.request.get("id")
        default = dict(id="", title="No Group", description="", users=[], groups=[])
        if not gid:
            return default
        group = api.group.get(groupname=gid)
        if group.getProperty("state") == "secret":
            return default
        g_title = group.getProperty("title") or group.getId() or gid
        g_description = group.getProperty("description")

        members = api.user.get_users(groupname=gid)
        users = []
        groups = []
        for principal in members:
            id = principal.getId()
            if IGroupData.providedBy(principal):
                if principal.getProperty("state") == "secret":
                    continue
                typ = "group"
                title = principal.getProperty("title") or id
                path = principal.getProperty("workspace_path") or ""
                groups.append(dict(id=id, title=title, typ=typ, path=path))
            else:
                typ = "user"
                title = principal.getProperty("fullname") or id
                path = f"/profiles/{id}"
                users.append(dict(id=id, title=title, typ=typ, path=path))

        return dict(
            id=gid, title=g_title, description=g_description, users=users, groups=groups
        )
