import carb.settings
import omni.ext
import omni.kit.ui
from .main import Window

EXTENSION_NAME = "Commands"


class Extension(omni.ext.IExt):
    def on_startup(self):
        self._menu_path = f"Window/{EXTENSION_NAME}"
        self._window = None
        open_by_default = carb.settings.get_settings().get("exts/omni.kit.window.commands/windowOpenByDefault")
        self._menu = omni.kit.ui.get_editor_menu().add_item(
            self._menu_path, self._on_menu_click, True, value=open_by_default
        )
        if open_by_default:
            self._on_menu_click(None, True)

    def on_shutdown(self):
        if self._window is not None:
            self._window.on_shutdown()

        self._menu = None
        self._window = None

    def _on_menu_click(self, menu, toggled):
        if toggled:
            if self._window is None:
                self._window = Window(EXTENSION_NAME, self._menu_path)
            else:
                self._window.show()
        else:
            if self._window is not None:
                self._window.hide()
