from threading import Thread
import rich_click as click
from rich.console import Console

from .utils import TimeDeltaParamType, LiveData, background_progress_viewer
from .discovery.main import ls_discovery


@click.command(short_help="Scan and display DDS entities in your network")
@click.option(
    "-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
    "-r",
    "--runtime",
    type=TimeDeltaParamType(),
    default="1s",
    help="Duration of discovery scan.",
)
@click.option(
    "-t",
    "--topic",
    type=str,
    help="Filter which entity types to display by topic name (supports regex)",
    default=".*",
)
@click.option(
    "--show-self",
    type=bool,
    is_flag=True,
    help="Show the tools own participant and subscriptions.",
)
@click.option(
    "--suppress-progress-bar",
    type=bool,
    is_flag=True,
    help="Suppress the output of the progress bar",
)
@click.option(
    "--color",
    type=click.Choice(["auto", "standard", "256", "truecolor", "windows", "none"]),
    default="auto",
    help="""Force the command to output with/without terminal colors. By default output colours if the terminal supports it."
See the [underline blue][link=https://rich.readthedocs.io/en/stable/console.html#color-systems]Rich documentation[/link][/] for more info on what the options mean.""",
)
@click.option(
    "-q",
    "--qos",
    type=bool,
    is_flag=True,
    help="Show the QoS settings of all entities.",
    default=False,
)
def ls(
    topic, id, runtime, show_self: bool, suppress_progress_bar: bool, color, qos: bool
):
    """Scan and display DDS entities in your network."""
    console = Console(color_system=None if color == "none" else color)
    live = LiveData(console)

    thread = Thread(target=ls_discovery, args=(live, id, runtime, topic, qos))
    thread.start()

    background_progress_viewer(runtime, live, suppress_progress_bar)

    thread.join()

    if live.result:
        for p in live.result:
            if p.is_self and not show_self:
                continue

            console.print(p)
            console.print()
