from plone import api
from ploneintranet.core import _
from ploneintranet.notifications.adapters.category_provider import BaseCategory
from ploneintranet.notifications.adapters.category_provider import BaseCategoryProvider
from ploneintranet.todo.notifications import messages
from ploneintranet.workspace.utils import parent_workspace


class BaseTodoUpdatedCategory(BaseCategory):
    name = "tasks"
    default_channels = {"desktop", "email"}
    title = _(
        "label_tasks_i_am_involved_in",
        default=("Tasks I am involved in"),
    )
    _app_id = "todo"

    def enhanced_security(self, notification):
        workspace = parent_workspace(self.context)
        return (
            api.portal.get_registry_record(
                name="ploneintranet.notifications.enhanced_security", default=False
            )
            and workspace
            and not workspace.accessible_to_intranet_users
            and notification.channel
            in {
                "email",
                "digest_daily",
                "digest_weekly",
            }
        )

    def get_message_for(self, notification, with_text=True, highlight=False):
        mapping = {
            "actor": self.logged_event.actor_fullname,
            "object": self.maybe_process_title(self.context.title, highlight),
        }
        workspace = parent_workspace(self.context)
        if not workspace:
            mapping["location"] = _("Personal task list")
        elif self.enhanced_security(notification):
            with_text = False
            mapping["location"] = _(
                "label_a_private_workspace", default="a private workspace"
            )
        else:
            mapping["location"] = workspace.title
        return self.message_variant(mapping, with_text)


class TodoAssignedCategory(BaseTodoUpdatedCategory):
    @property
    def recipient_ids(self):
        return {self.context.assignee}

    @property
    def message_variant(self):
        return messages.variant_assigned_a_task_to_you


class TodoAssignedCategoryProvider(BaseCategoryProvider):
    category_factories = [TodoAssignedCategory]


class TodoAssigneeChangedCategory(BaseTodoUpdatedCategory):
    @property
    def recipient_ids(self):
        return {self.context.assignee}

    @property
    def message_variant(self):
        return messages.variant_modified_your_task


class TodoAssigneeChangedCategoryProvider(BaseCategoryProvider):
    category_factories = [TodoAssigneeChangedCategory]


class TodoClosedAssigneeCategory(BaseTodoUpdatedCategory):
    @property
    def recipient_ids(self):
        return {self.context.assignee}

    @property
    def message_variant(self):
        return messages.variant_closed_your_task


class TodoClosedAssigneeCategoryProvider(BaseCategoryProvider):
    category_factories = [TodoClosedAssigneeCategory]


class TodoClosedInitiatorCategory(BaseTodoUpdatedCategory):
    @property
    def recipient_ids(self):
        return {self.context.initiator}

    @property
    def message_variant(self):
        return messages.variant_closed_a_task


class TodoClosedInitiatorCategoryProvider(BaseCategoryProvider):
    category_factories = [TodoClosedInitiatorCategory]


class TodoInitiatorChangedCategory(BaseTodoUpdatedCategory):
    @property
    def recipient_ids(self):
        return {self.context.initiator}

    @property
    def message_variant(self):
        return messages.variant_modified_a_task


class TodoInitiatorChangedCategoryProvider(BaseCategoryProvider):
    category_factories = [TodoInitiatorChangedCategory]
