from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.core.logging import get_logger
from parallels.core.utils.migrator_utils import get_optional_option_value
from parallels.plesk.source.mail import messages
from parallels.plesk.source.mail.actions.base.mail_migration import MailMigrationAction
from parallels.plesk.source.mail.mail_migration_list import MailMigrationListReader

logger = get_logger(__name__)


class ReadMigrationList(MailMigrationAction):
    """Read site migration list - with selected applications, files and databases to migrate"""

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

        :rtype: str | unicode
        """
        return messages.ACTION_READ_MIGRATION_LIST_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_READ_MIGRATION_LIST_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(compound=False)

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

        :type global_context: parallels.plesk.source.mail.global_context.MailGlobalMigrationContext
        :rtype: None
        """
        mail_migration_list_filename = get_optional_option_value(
            global_context.options, 'mail_migration_list', None
        )
        if mail_migration_list_filename is None:
            mail_migration_list_filename = global_context.session_files.get_default_mail_migration_list_file().path

        migration_list = MailMigrationListReader().read(mail_migration_list_filename)

        if len(migration_list.mail_address_pairs) != 1:
            raise Exception(messages.MIGRATION_LIST_EXACTLY_ONE_MAIL_ADDRESS_PAIR)

        global_context.migration_list_mail_address_pair = migration_list.mail_address_pairs[0]

        if global_context.migration_list_mail_address_pair.source_password is not None:
            global_context.settings_storage.set_settings(
                global_context.migration_list_mail_address_pair.source_email,
                global_context.migration_list_mail_address_pair.target_mailname,
                global_context.migration_list_mail_address_pair.encrypted_source_password,
                global_context.migration_list_mail_address_pair.encrypted_target_password,
                global_context.migration_list_mail_address_pair.imap_host,
                global_context.migration_list_mail_address_pair.imap_port,
                global_context.migration_list_mail_address_pair.ignore_security_warnings,
                global_context.migration_list_mail_address_pair.imap_encryption,
                global_context.migration_list_mail_address_pair.imap_timeout,
                global_context.migration_list_mail_address_pair.imap_folder_separator
            )
