from parallels.core import safe_format
from parallels.core.logging import get_logger
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.folder import FolderAction
from parallels.plesk.source.web.utils.copy_content.factory import create_copy_content

logger = get_logger(__name__)


class CopyFiles(FolderAction):
    """Copy website files and folders from source server"""

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

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

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

        If action is critical and it failed for a subscription, migration tool
        won't run the next operations for the subscription.

        :rtype: bool
        """
        return False

    def get_failure_message_for_folder(self, global_context, folder):
        """Get message for situation when action failed for specific folder

        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        :type folder: parallels.plesk.source.web.migrated_folder.MigratedFolder
        :rtype: str | unicode | None
        """
        return safe_format(
            messages.ACTION_COPY_FILES_FAILED_FOR_FOLDER,
            source_folder=folder.source_absolute_path,
            target_folder=folder.target_path
        )

    def run_for_folder(self, global_context, folder):
        """Run action for single migrated folder

        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        :type folder: parallels.plesk.source.web.migrated_folder.MigratedFolder
        :rtype: list[parallels.core.reports.model.issue.Issue] | None
        """
        source = global_context.conn.get_source()
        copy_content = create_copy_content(source)
        return copy_content.copy_folder(global_context, folder)
