# Copyright (c) 2025 The Foundry Visionmongers Ltd. All Rights Reserved.
"""
Module containing an example UsdSuperLayer ContextMenuPlugin.
"""

import logging
from typing import TYPE_CHECKING

import usg
from PySide6 import QtGui

from NodesUsdAPI.UsdSuperLayer import ContextMenuPlugin
from NodesUsdAPI.UsdSuperLayer.Node import UsdLayerSuperTool

if TYPE_CHECKING:
    from NodesUsdAPI.UsdSuperLayer.Editor import UsdSuperLayerEditor

log = logging.getLogger("SampleContextMenuPlugin")


class UsdSuperLayerSampleContextMenuPlugin(ContextMenuPlugin.ContextMenuPluginBase):
    @classmethod
    def callbackOnClick(
        cls,
        menuPath: str,
        controller: usg.StageSubtreeController,
        selectedPrimPaths: usg.PathArray,
        node: UsdLayerSuperTool,
        editor: "UsdSuperLayerEditor",
    ) -> bool:
        """
        Must be implemented in subclasses. Called when a menu is clicked by user.

        :param menuPath: The menu path. See menuPaths() for details.
        :param controller: The StageSubtreeController stored in UsdSuperLayer.
        :param selectedPrimPaths: The prim paths selected by user.
        :param node: The node which this callback was called from.
        :param editor: The editor associated with the node this callback was called from.
        :return: True if StageSubtreeController is changed, otherwise False.
        """
        print("Hello from sample context menu plugin!")
        del node, editor
        result = False
        if menuPath.startswith("DefPrim"):
            controller.createPrim(usg.Path("defines"))
            result = True
        elif menuPath.startswith("OverPrim"):
            for selectedPath in selectedPrimPaths:
                controller.createPrim(
                    selectedPath.appendChild("overrides"),
                    specifier=usg.PrimSpecifier.Over,
                )
                result = True
        return result

    @classmethod
    def menuPaths(cls) -> list[str]:
        """
        Must be implemented in subclasses. Returns the list of menu paths, where each path is
        a string formed by the menu titles separated by slashes, such as "File/Open" and
        "Setting/UI/Color".

        @return: The menu paths.
        @rtype: C{list[str]}
        """
        return [
            "RootItem1",
            "RootItem2",
            ContextMenuPlugin.MENU_NAME_SEPARATOR,
            "DefPrim",
            "OverPrim",
            "DisabledItem",
        ]

    @classmethod
    def shortcutKeys(cls, menuPath: str) -> QtGui.QKeySequence:
        """
        Returns shortcut key for the menu specified by the provided menuPath.

        @param menuPath: The menu path.
        @type menuPath: C{str}
        @return: The shortcut key.
        @rtype: C{QtGui.QKeySequence}
        """
        if menuPath == "RootItem1":
            return QtGui.QKeySequence("Ctrl+A")
        if menuPath == "RootItem2":
            return QtGui.QKeySequence("Ctrl+B")
        return QtGui.QKeySequence()

    @classmethod
    def tooltip(cls, menuPath: str) -> str:
        """
        Returns tooltip message showed on the menu specified by the provided menuPath.

        @param menuPath: The menu path.
        @type menuPath: C{str}
        @return: The tooltip message.
        @rtype: C{str}
        """
        if menuPath == "DefPrim":
            return "Create def prim"
        if menuPath == "OverPrim":
            return "Create over prim"
        return ""

    @classmethod
    def isEnabled(
        cls,
        menuPath: str,
        controller: usg.StageSubtreeController,
        selectedPrimPaths: usg.PathArray,
    ) -> bool:
        """
        Return True if the menu specified by the given menuPath should be enabled, otherwise
        False.

        @param menuPath: The menu path that is enabled or disabled.
        @type menuPath: C{type}
        @param controller: The content layer.
        @type stageSubtreeController: C{usg.StageSubtreeController}
        @param selectedPrimPaths: The prim paths selected when building menus.
        @type primPaths: C{usg.PathArray}
        @return: True if the menu is enable, otherwise False.
        @rtype: C{bool}
        """
        del controller, selectedPrimPaths
        return menuPath != "DisabledItem"


PluginRegistry = [
    (
        "UsdSuperLayerContextMenu",
        1,
        "SampleContextMenuPlugin",
        (
            UsdSuperLayerSampleContextMenuPlugin,
            {
                "active": False,
                "priority": 0,
                "appliedNodes": ["all"],
            },
        ),
    )
]
