"""
Tests for ploneintranet.workspace forms
"""

from plone import api
from plone.app.event.base import localized_now
from ploneintranet.workspace.tests.base import WorkflowBaseTestCase
from zope.event import notify
from zope.lifecycleevent import ObjectModifiedEvent


class TestWorkspaceGrouping(WorkflowBaseTestCase):
    def get_workspaces_view(self, context=None, request=None):
        """Return the workspaces view"""
        if not context:
            context = self.workspace_container
        if not request:
            request = self.request.clone()
        return api.content.get_view(
            name="workspaces.html", context=context, request=request
        )

    def setUp(self):
        super().setUp()
        workspaces = self.portal.workspaces
        ws1 = api.content.create(
            workspaces,
            "ploneintranet.workspace.workspacefolder",
            "tag1",
            subject="tag1",
        )
        self.add_user_to_workspace("wsadmin", ws1)
        ws2 = api.content.create(
            workspaces,
            "ploneintranet.workspace.workspacefolder",
            "tag-1-and-2",
            subject=("tag1", "tag2"),
        )
        self.add_user_to_workspace("wsadmin", ws2)
        self.solr_commit()

    def test_workspace_tag_grouping(self):
        """Test that workspaces_by_tag() returns a dict of workspaces grouped
        by tag
        """
        self.login("wsadmin")
        workspaces = self.portal.workspaces
        request = self.request.clone()
        request.grouping = "tag"
        view = self.get_workspaces_view(workspaces, request)
        tagged_workspaces = view.workspaces_by_tag()
        self.assertEqual(len(tagged_workspaces), 3)
        self.assertEqual(len(tagged_workspaces["tag1"]), 2)
        self.assertEqual(sorted(tagged_workspaces.keys()), ["tag1", "tag2", "untagged"])

    def test_should_see_only_my_workspaces(self):
        self.login("wsadmin")
        view = self.get_workspaces_view()
        # By default show all workspaces
        self.assertFalse(view.should_see_only_my_workspaces)

        view.request.__annotations__.clear()

        view.request.set("member", "1")
        self.assertTrue(view.should_see_only_my_workspaces)

        # nothing is stored by default
        with self.assertRaises(AttributeError):
            view.userprofile.only_my_workspaces

        view.request.__annotations__.clear()

        # unless we pass the marker parameter
        view.request.set("only-my-workspaces-empty-marker", "1")
        self.assertTrue(view.should_see_only_my_workspaces)
        self.assertTrue(view.userprofile.only_my_workspaces)

        view.request = self.request.clone()
        view.request.set("only-my-workspaces-empty-marker", "1")
        self.assertFalse(view.should_see_only_my_workspaces)
        self.assertFalse(view.userprofile.only_my_workspaces)

        # Intranet member by default should see only their workspaces
        self.portal.groups.intranet.members.remove("wsadmin")
        notify(ObjectModifiedEvent(self.portal.groups.intranet))
        view.request = self.request.clone()
        self.assertTrue(view.should_see_only_my_workspaces)
        # but for them nothing is persisted
        self.assertFalse(view.userprofile.only_my_workspaces)

    def test_only_my_workspaces_vs_archived(self):
        view = self.get_workspaces_view()
        view.request.set("member", "1")

        # By default selecting only the workspaces does not show the archived ones ...
        self.workspace_folder.archival_date = localized_now()
        self.workspace_folder.reindexObject()
        self.solr_commit()

        with api.env.adopt_user("wsowner"):
            self.assertListEqual([ws.title for ws in view.workspaces()], [])

        # ... unless we request them explicitly
        view.request.__annotations__.clear()
        view.request.set("archived", True)
        with api.env.adopt_user("wsowner"):
            self.assertListEqual(
                [ws.title for ws in view.workspaces()], ["Demo Workspace"]
            )
