from parallels.core.actions.base.compound_action import CompoundAction
from parallels.core.actions.content.web.check_ssh_options import CheckSSHOptionsAction
from parallels.core.actions.content.web.copy_unix_content import CopyUnixWebContent
from parallels.core.actions.read_dump_agent_options import ReadDumpAgentOptions
from parallels.core.actions.utils.logging_properties import LoggingProperties
from parallels.core.utils.backup_adapter import SubscriptionBackup
from parallels.plesk.source.cpanel import messages
from parallels.core.actions.base.legacy_action import LegacyAction
from parallels.core.workflow.extension import WorkflowExtensionBase
from parallels.core.actions.content.web.fix_unix_htaccess import FixUnixHtaccess
from parallels.core.actions.content.web.fix_unix_vhost_permissions import FixUnixVhostPermissions
from parallels.plesk.source.cpanel.actions.fix_spamassassin_patterns import FixSpamassassinPatterns


class WorkflowExtension(WorkflowExtensionBase):
    def extend_workflow(self, workflow):
        """Extend shared hosting workflow with source-panel specific actions

        :type workflow: parallels.core.workflow.base_workflow.BaseWorkflow
        """
        workflow.replace_shared_action('fetch-source-shallow', CompoundAction())
        workflow.get_shared_action('fetch-source-shallow').insert_action(
            'read-agent-options', ReadDumpAgentOptions()
        )
        workflow.get_shared_action('fetch-source-shallow').insert_action(
            'fetch-shallow-backup', LegacyAction(
                description=messages.ACTION_FETCH_SHALLOW_SOURCE,
                function=lambda ctx: ctx.migrator.fetch_source_shallow(
                    ctx.options, ctx.options.reload_source_data
                ),
                logging_properties=LoggingProperties(info_log=False)
            )
        )
        workflow.get_shared_action('fetch-source').insert_action(
            'fetch-data', LegacyAction(
                description=messages.ACTION_FETCH_SOURCE,
                function=lambda ctx: ctx.migrator._fetch_source(
                    ctx.options, ctx.options.reload_source_data
                ),
                logging_properties=LoggingProperties(info_log=False)
            )
        )
        workflow.get_shared_action('copy-web-files').insert_action(
            'copy-files', CopyUnixWebContent()
        )
        workflow.get_path('check').insert_action(
            'check-ssh-options', CheckSSHOptionsAction(), before='cleanup'
        )
        workflow.get_shared_action('sync-web-content-assets').insert_action(
            'fix-unix-htaccess', FixUnixHtaccess()
        )
        workflow.get_shared_action('sync-web-content-assets').insert_action(
            'fix-unix-vhost-permissions', FixUnixVhostPermissions()
        )
        workflow.get_shared_action('convert-hosting').insert_action(
            'fix-spamassassin-patterns', FixSpamassassinPatterns(SubscriptionBackup()),
            before='save-backup'
        )
