from parallels.common.actions.base.compound_action import CompoundAction
from parallels.common.actions.base.condition_action_pointer import ConditionActionPointer
from parallels.common.actions.base.legacy_action import LegacyAction
from parallels.common.actions.restore.plesk.import_clients import ImportClients
from parallels.common.actions.restore.plesk.import_subscriptions import ImportSubscriptions
from parallels.common.target_panels import TargetPanels


class ImportClientsAndSubscriptions(ConditionActionPointer):
	def __init__(self):
		description = 'Import clients and subscriptions'

		ppa_action = LegacyAction(
			description=description,
			function=lambda ctx: ctx.migrator._restore_impl(ctx.options, finalize=False)
		)

		plesk_action = CompoundAction(description=description)
		plesk_action.insert_action('import-clients', ImportClients())
		plesk_action.insert_action('import-subscriptions', ImportSubscriptions())

		self._actions = {
			TargetPanels.PPA: ppa_action,
			TargetPanels.PLESK: plesk_action,
			TargetPanels.PPA_WEB_SN: ppa_action,
		}

	def resolve(self, global_context):
		"""
		:type global_context: parallels.common.global_context.GlobalMigrationContext
		"""

		if global_context.target_panel not in self._actions:
			raise Exception(
				"Concrete import clients and subscriptions actions "
				"are not defined for target panel '%s'" % (
					global_context.target_panel,
				)
			)
		return self._actions[global_context.target_panel]
