import argparse
from parallels.core.cli.command import CommandTypes, Command, CommandOperation
from parallels.core.cli.extension import CLIExtensionBase
from parallels.plesk.source.mail 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]
        """
        mail_migration_list_opts = argparse.ArgumentParser(add_help=False)
        mail_migration_list_opts.add_argument(
            '--mail-migration-list', action='store',
            help=messages.MAIL_MIGRATION_LIST_OPTION
        )
        is_async_status_report_opts = argparse.ArgumentParser(add_help=False)
        is_async_status_report_opts.add_argument(
            '--async-status-report', action='store_true',
            help=messages.ASYNC_STATUS_REPORT_OPTION
        )
        task_id_opts = argparse.ArgumentParser(add_help=False)
        task_id_opts.add_argument(
            '--task-id', action='store',
            help=messages.TASK_ID_OPTION
        )
        task_status_opts = argparse.ArgumentParser(add_help=False)
        task_status_opts.add_argument(
            '--task-status', action='store',
            help=messages.TASK_STATUS_OPTION
        )

        return [
            Command(
                'transfer-mail', CommandTypes.MACRO,
                messages.COMMAND_TRANSFER_MAIL,
                lambda runner: runner.run_entry_point('transfer-mail'),
                common_options.global_options_list + [
                    mail_migration_list_opts,
                    is_async_status_report_opts,
                    task_id_opts,
                    task_status_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-mail-migration-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_RUN_TASKS,
                lambda runner: runner.run_entry_point('run-mail-migration-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-mail-migration-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_ADD_TASKS,
                lambda runner: runner.run_entry_point('add-mail-migration-tasks'),
                common_options.global_options_list + [mail_migration_list_opts] + [is_async_status_report_opts],
                is_skip_progress_reporting=True,
                is_allow_parallel_execution=True,
                command_operation=CommandOperation.TASK_MANAGER,
                is_enable_profiling_by_default=False,
            ),
            Command(
                'cancel-mail-migration-tasks', CommandTypes.INTERNAL,
                messages.COMMAND_CANCEL_TASKS,
                lambda runner: runner.run_entry_point('cancel-mail-migration-tasks'),
                common_options.global_options_list + [mail_migration_list_opts],
                is_skip_progress_reporting=True,
                is_allow_parallel_execution=True,
                command_operation=CommandOperation.TASK_MANAGER,
                is_enable_profiling_by_default=False,
            )
        ]
