import click
import traceback

from wurker.settings import conf
from wurker.plugin import load_plugins
from wurker.cli.base import CommandNotReady

from .command import ShowCommand


def register_show_command(wurk):

    @wurk.command()
    @click.argument("option", default="all", type=click.Choice([
        'all', 'bee', 'cron', 'command', 'ondemand', 'scheduled', 'supervised', 'hive',
    ]))
    @click.option("--slug", "key", help="Filter by slug (only: command, cron, bee)")
    @click.option("-d", "--drone", "drones", multiple=True, help="Slug or ID of drone(s) to show (repeatable)")
    @click.pass_context
    def show(ctx, option, key, drones):
        """Show Wurker definitions"""

        try:
            logger = ctx.obj.get("logger")

            load_plugins(logger=logger)

            cmd = ShowCommand(logger=logger)
            cmd.run(option, key=key, drones=drones)
        except CommandNotReady as ex:
            if conf.debug:
                click.echo(traceback.format_exc(), err=True)
            raise click.ClickException(f"The 'wurk show' command is not ready. Run 'wurk admin --help'. {ex}")
        except Exception as ex:
            if conf.debug:
                click.echo(traceback.format_exc(), err=True)
            raise click.ClickException(str(ex))
