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 import messages
from parallels.plesk.source.mail.status_storage import StatusStorage

logger = get_logger(__name__)


class WriteInitialStatus(MailMigrationAction):
    """Start migration - initialize statuses of migrated objects"""

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

        :rtype: str | unicode
        """
        return messages.ACTION_WRITE_INITIAL_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_INITIAL_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 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
        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, StatusStorage.STATUS_IN_PROGRESS
            )
        else:
            global_context.status_storage.set_task_status(StatusStorage.STATUS_IN_PROGRESS)

