from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.core.logging import get_logger
from parallels.plesk.source.mail.actions.base.mail_migration import MailMigrationAction
from parallels.plesk.source.mail.status_storage import StatusStorage
from parallels.plesk.source.mail import messages

logger = get_logger(__name__)


class WriteFinalStatus(MailMigrationAction):
    """Finish migration - set finish statuses for all migrated objects"""

    def get_description(self):
        """Get short description of action as string

        :rtype: str | unicode
        """
        return messages.ACTION_WRITE_FINAL_STATUS_DESCRIPTION

    def get_failure_message(self, global_context):
        """Get message for situation when action failed

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: str | unicode
        """
        return messages.ACTION_WRITE_FINAL_STATUS_FAILURE

    def get_logging_properties(self):
        """Get how action should be logged to migration tools end-user

        :rtype: parallels.core.actions.utils.logging_properties.LoggingProperties
        """
        return LoggingProperties(info_log=False, compound=False)

    def is_critical(self):
        """If action is critical or not

        If action is critical and it failed for a subscription, migration tool
        won't run the next operations for the subscription.

        :rtype: bool
        """
        return False

    def filter_action(self, global_context):
        """Check whether we should run this action or not. By default True - action should be executed.

        :type global_context: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
        :rtype: bool
        """
        return (
            global_context.status_storage is not None and
            global_context.migration_list_mail_address_pair is not None
        )

    def run(self, global_context):
        """Run action

        :type global_context: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
        :rtype: None
        """
        mail_address_pair = global_context.migrated_mail_address_pair
        status = self._get_status_value(global_context)

        if global_context.status_storage.get_reporting_mode() == StatusStorage.REPORTING_MODE_ASYNC:
            global_context.status_storage.set_global_status(
                mail_address_pair.source_email, mail_address_pair.target_mailname, status
            )
        else:
            global_context.status_storage.set_task_status(status)

    @staticmethod
    def _get_status_value(global_context):
        """
        :rtype: str | unicode
        """
        if global_context.stop_mark.is_set():
            return StatusStorage.STATUS_CANCELLED
        elif global_context.status_storage.has_errors():
            return StatusStorage.STATUS_FAILURE
        elif global_context.status_storage.has_warnings():
            return StatusStorage.STATUS_WARNING
        else:
            return StatusStorage.STATUS_SUCCESS
