from parallels.common.workflow.shared_hosting_workflow import SharedHostingWorkflow

from parallels.common.actions.base.legacy_action \
	import LegacyAction
from parallels.common.actions.base.entry_point_action \
	import EntryPointAction

from parallels.hsphere_migrator.actions.hosting_settings.convert.dns \
	import DNS as ActionHostingSettingsConvertDNS
from parallels.hsphere_migrator.actions.content.web.copy_windows_web_content \
	import CopyWindowsWebContent
from parallels.hsphere_migrator.actions.content.web.copy_unix_web_content \
	import CopyUnixWebContent
from parallels.common.actions.content.web.fix_unix_vhost_permissions \
	import FixUnixVhostPermissions
from parallels.plesks_migrator.actions.content.web.reset_permissions \
	import ResetPermissions
from parallels.hsphere_migrator.actions.hosting_settings.convert.remove_subdomain_mail \
	import RemoveSubdomainMail
from parallels.hsphere_migrator.actions.hosting_settings.transfer_virtual_directories \
	import TransferVirtualDirectories
from parallels.hsphere_migrator.actions.hosting_settings.transfer_error_documents \
	import TransferErrorDocuments
from parallels.hsphere_migrator.actions.hosting_settings.transfer_mime_types \
	import TransferMIMETypes
from parallels.hsphere_migrator.actions.dns_forwarding.forwarding.undo_not_supported \
	import UndoNotSupported
from parallels.hsphere_migrator.actions.hosting_settings.check.mailsystem_on_subdomains \
	import MailsystemOnSubdomains

from parallels.common.utils.backup_adapter import SubscriptionBackup
from parallels.common.utils.backup_adapter import SubscriptionBackupRaw


class FromHSphereWorkflow(SharedHostingWorkflow):
	def __init__(self):
		super(FromHSphereWorkflow, self).__init__()

		self.get_shared_action('fetch-source').insert_action(
			'fetch-data', LegacyAction(
				description='Fetch data from source H-Sphere',
				function=lambda ctx: ctx.migrator._fetch_source(
					ctx.options, ctx.options.reload_source_data
				)
			)
		)
		self.get_path('transfer-accounts/restore-hosting/convert-hosting').replace(
			'convert-dns', ActionHostingSettingsConvertDNS()
		)
		self.get_path('transfer-accounts').insert_action('transfer-resource-limits', LegacyAction(
			description='Transfer resource limits',
			function=lambda ctx: ctx.migrator._transfer_resource_limits(ctx.options)
		), after='restore')

		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'copy-files-unix', CopyUnixWebContent()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'fix-unix-vhost-permissions', FixUnixVhostPermissions()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'copy-files-windows', CopyWindowsWebContent()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'reset-permissions', ResetPermissions()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'transfer-virtual-directories', TransferVirtualDirectories()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'transfer-mime-types', TransferMIMETypes()
		)
		self.get_path('transfer-accounts/copy-content/web').insert_action(
			'transfer-error-documents', TransferErrorDocuments()
		)

		self.get_path(
			'transfer-accounts/restore-hosting/convert-hosting'
		).insert_action(
			'remove-subdomains-mail', RemoveSubdomainMail(SubscriptionBackup())
		)

		self.get_path('check/check-data').insert_action(
			'check-mail-on-subdomains', MailsystemOnSubdomains(SubscriptionBackupRaw())
		)

		self.replace_entry_point('set-dns-forwarding', EntryPointAction())
		self.replace_entry_point('undo-dns-forwarding', EntryPointAction())

		self.get_path('set-dns-forwarding').insert_action(
			'check-connections', LegacyAction(
				description='Check connections',
				function=lambda ctx: ctx.migrator._check_connections(ctx.options)
			)
		)
		self.get_path('set-dns-forwarding').insert_action(
			'read-migration-list', LegacyAction(
				description='Read migration list',
				function=lambda ctx: ctx.migrator._read_migration_list_lazy(ctx.options)
			)
		)
		self.get_path('set-dns-forwarding').insert_action(
			'forward-dns', LegacyAction(
				description='Forward DNS',
				function=lambda ctx: ctx.migrator._forward_dns(ctx.dns_forwarding_report)
			)
		)
		self.get_path('set-dns-forwarding').insert_action(
			'print-report', LegacyAction(
				description='Print DNS forwarding report',
				function=lambda ctx: ctx.migrator._print_dns_forwarding_report(ctx.dns_forwarding_report)
			)
		)

		self.get_path('undo-dns-forwarding').insert_action(
			'not-supported', UndoNotSupported()
		)