from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.plesk.source.mail.statistics import StatisticsTransferMail
from parallels.core.utils.migrator_utils import get_version
from parallels.plesk.source.mail import messages
from parallels.plesk.source.mail.actions.base.mail_migration import MailMigrationAction


class StartTransferMailStatistics(MailMigrationAction):
    """Initially write statistics once 'transfer-mail' command is started
    """

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

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

    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.FAILED_ACTION_START_TRANSFER_MAIL_STATISTICS

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

        If action is critical and it failed for a source-target mailbox pair, migration tool
        won't run the next operations.

        :rtype: bool
        """
        return False

    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_skip_error_reporting(self):
        """Whether to skip reporting action errors to status file (~ whether errors will be visible in GUI)

        :rtype: bool
        """
        return True

    def run(self, global_context):
        """
        :type global_context: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
        """
        global_context.statistics_transfer_mail = StatisticsTransferMail(
            start_time=int(global_context.start_time),
            source_email=global_context.migrated_mail_address_pair.source_email,
            target_email=global_context.migrated_mail_address_pair.target_email,
            imap_encryption=global_context.migrated_mail_address_pair.imap_encryption if global_context.migrated_mail_address_pair.imap_encryption is not None else 'auto',
            ignore_security_warnings=global_context.migrated_mail_address_pair.ignore_security_warnings,
            extension_version=get_version()
        )
        global_context.statistics_transfer_mail.write()
