from parallels.core.actions.base.common_action import CommonAction
from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.core.runners.base import BaseRunner
from parallels.core.utils.config_utils import ConfigSection
from parallels.plesk.source.confixx import messages


class EnableMailShortnamesAction(CommonAction):
    """Enable ability to login by short name by IMAP/POP3 on target Plesk if corresponding config option was set.

    Short name of e-mail address is everything before '@', for example if you have e-mail address john@example.com,
    then once this action is executed you will be able to login by IMAP/POP3 using 'john' as login.

    That is necessary to preserve ability to login into mailbox by the same login when migrating from Confixx,
    where logins were mailbox names, for example 'web7p9' or 'web3p12', and so on. When this option in set,
    each mailbox is migrated into Plesk e-mail address, which starts with mailbox name, for example
    'web7p9@example.com' or 'web3p12@web3.systemdomain.tld'.
    """

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

        :rtype: str | unicode
        """
        return messages.ACTION_ENABLE_MAIL_SHORTNAMES_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_ENABLE_MAIL_SHORTNAMES_FAILURE

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

        If action is critical and it failed, migration tool completely stops.
        Otherwise it proceeds to the next steps of migrations.

        :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 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.core.global_context.GlobalMigrationContext
        :rtype: bool
        """
        source_server_config_section = ConfigSection(global_context.config, 'confixx')
        return source_server_config_section.get('mail-mapping') == 'shortnames'

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

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: None
        """
        with global_context.conn.target.plesk_server.runner() as runner:
            assert isinstance(runner, BaseRunner)
            runner.execute_command('plesk bin settings --set postfix-allow-short-mail-names=true')
            runner.execute_command('plesk bin settings --set allow_short_pop3_names=enabled')
            runner.execute_command('plesk sbin mailmng-server --set-accounts-format --account-format=both')