from parallels.ppa_sn import messages
import argparse

from parallels.core.utils.common import unused
from parallels.core.cli.common_cli import Command, CommandTypes, \
	MigratorHelpCommand, MigratorShellCommand, MigratorScriptCommand, \
	create_arguments_parser, run as run_migrator
from parallels.core.cli.migration_cli import _get_list_options
from .migrator import Migrator


def run(base_dir, var_dir, execution_path, args):
	unused(var_dir)
	run_migrator(
		base_dir=base_dir,
		var_dir=var_dir,
		execution_path=execution_path,
		args_parser=_create_migrator_arguments_parser(),
		create_migrator_func=Migrator,
		args=args,
	)


def _create_migrator_arguments_parser():
	indent = ' ' * 4

	migration_list_file_opt = argparse.ArgumentParser(add_help=False)
	migration_list_file_opt.add_argument(
		'--move-list-file', 
		help=messages.MOVE_LIST_OPTION
	)

	commands = []

	# standard commands - help, shell, script
	commands.extend([
		MigratorHelpCommand(_get_migrator_command_name(), commands, indent),
		MigratorShellCommand(),
		MigratorScriptCommand(),
	])

	# migrator-specific commands
	for command in Migrator.flat_iter_commands_tree(Migrator.get_commands_tree()):
		commands.append(Command(
			command.name, command.command_type,
			command.brief_description,
			lambda m, o, command_name=command.name: m.execute_command(command_name, o),
			[migration_list_file_opt],
			is_legacy=True
		))
	# Used by bash completion
	get_options_opts = argparse.ArgumentParser(add_help=False)
	get_options_opts.add_argument('--command')
	commands.append(
		Command(
			'list-options', CommandTypes.MICRO,
			messages.RETURN_LIST_OF_AVAILABLE_OPTIONS,
			lambda o: _get_list_options(commands, o),
			[get_options_opts],
			is_migrator_required=False,
		),
	)
	
	return create_arguments_parser(
		migrator_command_name=_get_migrator_command_name(),
		description=messages.MOVE_SUBSCRIPTIONS_BETWEEN_PPA_APACHE_NODES,
		commands=commands,
		indent=indent
	)


def _get_migrator_command_name():
	return 'ppa-move-subscriptions'
