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.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction
from parallels.plesk.source.web.site_migration_list import SiteMigrationListReader

logger = get_logger(__name__)


class ReadMigrationList(SiteMigrationAction):
    """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.web.global_context.WebGlobalMigrationContext
        :rtype: None
        """
        site_migration_list_filename = get_optional_option_value(
            global_context.options, 'site_migration_list', None
        )
        if site_migration_list_filename is None:
            site_migration_list_filename = global_context.session_files.get_default_site_migration_list_file().path

        global_context.site_migration_list = SiteMigrationListReader().read(site_migration_list_filename)
