import typer
from acemist.state import State
from acemist.context import CLIContext
from acemist.mist.mist import Mist
from acemist.commands import orgs, sites, context, networktemplates,device
from acemist.commands._self import self_cmd

app = typer.Typer()


@app.callback()
def main(ctx: typer.Context):
    cli_context = CLIContext()
    active = cli_context.get_active_context()
    ctx.obj = State(
        mist=Mist(
            baseurl=active.get("apiurl") if active else None,
            token=active.get("api_token") if active else None,
            org_id=active.get("org_id") if active else None,
            site_id=active.get("site_id") if active else None
        ),
        context=cli_context
    )


app.command("self")(self_cmd)
app.add_typer(context.app, name="context")
app.add_typer(sites.app, name="site")
app.add_typer(orgs.app, name="org")
app.add_typer(networktemplates.app, name="network_templates")
app.add_typer(networktemplates.app, name="nt")
app.add_typer(device.app, name="device")


def cli():
    app()


if __name__ == "__main__":
    cli()



