#!/usr/bin/env python
"""A complex example demonstrating a variety of methods to load CommandSets using a mix of command decorators.

Includes examples of how to integrate tab completion with argparse-based commands.
"""

import argparse
from collections.abc import Iterable

from modular_commands.commandset_basic import (
    BasicCompletionCommandSet,
)
from modular_commands.commandset_complex import (
    CommandSetA,
)
from modular_commands.commandset_custominit import (
    CustomInitCommandSet,
)

from cmd2 import (
    Choices,
    Cmd,
    Cmd2ArgumentParser,
    CommandSet,
    with_argparser,
)


class WithCommandSets(Cmd):
    def __init__(self, command_sets: Iterable[CommandSet] | None = None) -> None:
        """Cmd2 application to demonstrate a variety of methods for loading CommandSets."""
        super().__init__(command_sets=command_sets)
        self.sport_item_strs = ["Bat", "Basket", "Basketball", "Football", "Space Ball"]

    def choices_provider(self) -> Choices:
        """A choices provider is useful when the choice list is based on instance data of your application."""
        return Choices.from_values(self.sport_item_strs)

    # Parser for example command
    example_parser = Cmd2ArgumentParser(
        description="Command demonstrating tab completion with argparse\nNotice even the flags of this command tab complete"
    )

    # Tab complete from a list using argparse choices. Set metavar if you don't
    # want the entire choices list showing in the usage text for this command.
    example_parser.add_argument(
        "--choices", choices=["some", "choices", "here"], metavar="CHOICE", help="tab complete using choices"
    )

    # Tab complete from choices provided by a choices provider
    example_parser.add_argument(
        "--choices_provider", choices_provider=choices_provider, help="tab complete using a choices_provider"
    )

    # Tab complete using a completer
    example_parser.add_argument("--completer", completer=Cmd.path_complete, help="tab complete using a completer")

    @with_argparser(example_parser)
    def do_example(self, _: argparse.Namespace) -> None:
        """An example command."""
        self.poutput("I do nothing")


if __name__ == "__main__":
    import sys

    print("Starting")
    my_sets = [BasicCompletionCommandSet(), CommandSetA(), CustomInitCommandSet("First argument", "Second argument")]
    app = WithCommandSets(command_sets=my_sets)
    sys.exit(app.cmdloop())
