from plone import api
from ploneintranet.todo.testing import IntegrationTestCase
from Products.statusmessages.interfaces import IStatusMessage


class TestTodoView(IntegrationTestCase):
    """Test the Todo view"""

    def setUp(self):
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]
        self.todo = api.content.create(
            type="todo", title="todo1", container=self.portal
        )

    def test_add_related_item(self):
        """Check that we can manipulate the related items"""
        view = api.content.get_view("view", self.todo, self.request.clone())

        # We have no attachments
        self.assertListEqual(self.todo.relatedItems, [])

        # Add a first attachment
        api.content.create(type="Folder", title="Folder", container=self.portal)
        view.request.form["new-related-item"] = self.portal.folder.UID()
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems], ["folder"]
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["Attachment added"],
        )

        # Add another attachment
        view.request.form["new-related-item"] = self.portal.apps.UID()
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems],
            ["folder", "apps"],
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["Attachment added"],
        )

        # Now try to readd this attachment
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems],
            ["folder", "apps"],
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["Attachment already present"],
        )

        # Now try to add a non existent attachment
        view.request.form["new-related-item"] = "foobar"
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems],
            ["folder", "apps"],
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["Attachment cannot be added"],
        )

        # Now clear the request and test removal
        view.request = view.request.clone()

        # Passing a wrong UID will not remove anything
        view.request.form["obsolete-related-item"] = "foobar"
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems],
            ["folder", "apps"],
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["No attachment was removed"],
        )

        # Passing the right UID will not remove the proper related item
        view.request.form["obsolete-related-item"] = self.portal.folder.UID()
        view.update()

        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems], ["apps"]
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["Attachment removed"],
        )

        # Repeating the call will not remove anything
        view.update()
        self.assertListEqual(
            [relation.to_object.id for relation in self.todo.relatedItems], ["apps"]
        )
        self.assertListEqual(
            [item.message for item in IStatusMessage(view.request).show()],
            ["No attachment was removed"],
        )
