import argparse
from parallels.core.cli.command import CommandTypes, Command, CommandOperation
from parallels.core.cli.extension import CLIExtensionBase
from parallels.plesk.source.web import messages


class CLIExtension(CLIExtensionBase):
    """Command line migrator interface extension for site migration"""

    def get_additional_commands(self, common_options):
        """
        :type common_options: parallels.core.cli.migration_cli.CommonMigrationOptions
        :rtype: list[parallels.core.cli.command.Command]
        """
        site_migration_list_opts = argparse.ArgumentParser(add_help=False)
        site_migration_list_opts.add_argument(
            '--site-migration-list', action='store',
            help=messages.SITE_MIGRATION_LIST_OPTION
        )
        reload_source_data_opts = argparse.ArgumentParser(add_help=False)
        reload_source_data_opts.add_argument(
            '--reload-source-data', action='store_true',
            help=messages.SITE_MIGRATION_RELOAD_SOURCE_DATA_OPTION
        )

        return [
            Command(
                'get-site-info', CommandTypes.MACRO,
                messages.COMMAND_GET_SITE_INFO,
                lambda runner: runner.run_entry_point('get-site-info'),
                common_options.global_options_list + [reload_source_data_opts],
                is_allow_parallel_execution=True
            ),
            Command(
                'transfer-site', CommandTypes.MACRO,
                messages.COMMAND_TRANSFER_SITE,
                lambda runner: runner.run_entry_point('transfer-site'),
                common_options.global_options_list + [site_migration_list_opts],
                is_allow_parallel_execution=True
            ),
            Command(
                'check-apps', CommandTypes.MICRO,
                messages.COMMAND_CHECK_APPS,
                lambda runner: runner.run_entry_point('check-apps'),
                common_options.global_options_list + [site_migration_list_opts],
                is_allow_parallel_execution=True
            ),
            # Task manager commands: add, cancel and run site migration tasks.
            # These commands are necessary for asynchronous integration with GUI.
            Command(
                'run-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_RUN_TASKS,
                lambda runner: runner.run_entry_point('run-tasks'),
                common_options.global_options_list,
                is_skip_progress_reporting=True,
                is_allow_parallel_execution=True,
                command_operation=CommandOperation.TASK_RUNNER,
                is_enable_profiling_by_default=False,
            ),
            Command(
                'add-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_ADD_TASKS,
                lambda runner: runner.run_entry_point('add-tasks'),
                common_options.global_options_list + [site_migration_list_opts],
                is_skip_progress_reporting=True,
                is_allow_parallel_execution=True,
                command_operation=CommandOperation.TASK_MANAGER,
                is_enable_profiling_by_default=False,
            ),
            Command(
                'cancel-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_CANCEL_TASKS,
                lambda runner: runner.run_entry_point('cancel-tasks'),
                common_options.global_options_list + [site_migration_list_opts],
                is_skip_progress_reporting=True,
                is_allow_parallel_execution=True,
                command_operation=CommandOperation.TASK_MANAGER,
                is_enable_profiling_by_default=False,
            )
        ]
