from pathlib import Path

import click

from tgzr.cli.utils import TGZRCliGroup
from tgzr.shell.app_sdk.nice_app import ShellNiceApp

from . import run_native, run_dev
from . import pages


class ManagerPanelApp(ShellNiceApp):
    def cli_run_cmd_installed(
        self, created_cmd: click.Command, root_group: TGZRCliGroup
    ):
        """
        Called when tgzr.shell.cli_plugin.app_cli has created and
        registered a cli command to execute this app.

        Overridden to set our command as default command if no higher
        order default command was set.
        """
        # If the cli's default command was set by tgzr.cli or tgzr.shell,
        # we want to override it with our own command:
        cmd, kwargs, setter = root_group.get_default_command()
        if setter and (
            setter.startswith("tgzr.cli") or setter.startswith("tgzr.shell")
        ):
            # print("tgzr.shell_app.manager_panel uninstalling default cmd from", setter)
            root_group.set_default_command(created_cmd)
            # cmd, kwargs, setter = root_group.get_default_command()
            # print("  new setter:", setter)


app = ManagerPanelApp(
    "manager",
    run_native_module=run_native,
    run_dev_module=run_dev,
    static_file_path=Path(pages.__file__).parent / "static_files",
)
