import webbrowser
import os
import carb
import carb.input
import carb.settings
import omni.kit.app
import omni.kit.menu.utils
from carb.input import KeyboardInput as Key
from omni.kit.menu.utils import MenuItemDescription


class HelpExtension:
    def __init__(self):
        def get_discover_kit_sdk_path(settings):
            if omni.kit.app.get_app().is_app_external():
                return settings.get("/exts/omni.kit.menu.common/external_kit_sdk_url")
            else:
                return settings.get("/exts/omni.kit.menu.common/internal_kit_sdk_url")

        def get_manual_url_path(settings):
            if omni.kit.app.get_app().is_app_external():
                return settings.get("/exts/omni.kit.menu.common/external_kit_manual_url")
            else:
                return settings.get("/exts/omni.kit.menu.common/internal_kit_manual_url")

        def get_discover_reference_guide_path(settings):
            if omni.kit.app.get_app().is_app_external():
                return settings.get("/exts/omni.kit.menu.common/external_reference_guide_url")
            else:
                return settings.get("/exts/omni.kit.menu.common/internal_reference_guide_url")


        settings = carb.settings.get_settings()
        discover_reference_guide_path = get_discover_reference_guide_path(settings)
        discover_kit_sdk_path = get_discover_kit_sdk_path(settings)
        manual_url_path = get_manual_url_path(settings)

        self._register_actions("omni.kit.menu.common", discover_reference_guide_path, discover_kit_sdk_path, manual_url_path)

        reference_guide_name = settings.get("/exts/omni.kit.menu.common/reference_guide_name")
        kit_sdk_name = settings.get("/exts/omni.kit.menu.common/kit_sdk_name")
        kit_manual_name = settings.get("/exts/omni.kit.menu.common/kit_manual_name")

        self._help_menu = []

        if reference_guide_name:
            self._help_menu.append(MenuItemDescription(
                    name=reference_guide_name,
                    onclick_action=("omni.kit.menu.common", "OpenRefGuide"),
                    hotkey=(0, Key.F1),
                    enabled=False if reference_guide_name is None else True
                ))

        if kit_sdk_name:
            self._help_menu.append(MenuItemDescription(
                    name=kit_sdk_name,
                    onclick_action=("omni.kit.menu.common", "OpenDevKitSDK"),
                    enabled=False if discover_kit_sdk_path is None else True
                ))

        if kit_manual_name:
            self._help_menu.append(MenuItemDescription(
                    name=kit_manual_name,
                    onclick_action=("omni.kit.menu.common", "OpenDevManual"),
                    enabled=False if manual_url_path is None else True
                ))

        if self._help_menu:
            omni.kit.menu.utils.add_menu_items(self._help_menu, "Help", 99)

    def __del__(self):
        omni.kit.menu.utils.remove_menu_items(self._help_menu, "Help")
        self._deregister_actions("omni.kit.menu.common")
        self.menus = None

    def _register_actions(self, extension_id, discover_reference_guide_path, discover_kit_sdk_path, manual_url_path):
        action_registry = omni.kit.actions.core.get_action_registry()
        actions_tag = "Help Menu Actions"

        # actions
        action_registry.register_action(
            extension_id,
            "OpenRefGuide",
            lambda: webbrowser.open(discover_reference_guide_path),
            display_name="Help->Reference Guide",
            description="Reference Guide",
            tag=actions_tag,
        )

        action_registry.register_action(
            extension_id,
            "OpenDevKitSDK",
            lambda: webbrowser.open(discover_kit_sdk_path),
            display_name="Help->Discover Kit SDK",
            description="Discover Kit SDK",
            tag=actions_tag,
        )

        action_registry.register_action(
            extension_id,
            "OpenDevManual",
            lambda: webbrowser.open(manual_url_path),
            display_name="Help->Developers Manual",
            description="Developers Manual",
            tag=actions_tag,
        )

    def _deregister_actions(self, extension_id):
        action_registry = omni.kit.actions.core.get_action_registry()
        if action_registry:
            action_registry.deregister_all_actions_for_extension(extension_id)
