"""Support for simplified access to data on nodes of type omni.graph.action.OnGamepadInput

Event node which fires when a gamepad event occurs. This node only capture events on buttons,  excluding triggers and sticks
"""


import omni.graph.core as og
import omni.graph.core._omni_graph_core as _og
import omni.graph.tools.ogn as ogn



class OgnOnGamepadInputDatabase(og.Database):
    """Helper class providing simplified access to data on nodes of type omni.graph.action.OnGamepadInput

    Class Members:
        node: Node being evaluated

    Attribute Value Properties:
        Inputs:
            inputs.gamepadElementIn
            inputs.gamepadId
            inputs.onlyPlayback
        Outputs:
            outputs.isPressed
            outputs.pressed
            outputs.released

    Predefined Tokens:
        tokens.FaceButtonBottom
        tokens.FaceButtonRight
        tokens.FaceButtonLeft
        tokens.FaceButtonTop
        tokens.LeftShoulder
        tokens.RightShoulder
        tokens.SpecialLeft
        tokens.SpecialRight
        tokens.LeftStickButton
        tokens.RightStickButton
        tokens.DpadUp
        tokens.DpadRight
        tokens.DpadDown
        tokens.DpadLeft
    """

    # Imprint the generator and target ABI versions in the file for JIT generation
    GENERATOR_VERSION = (1, 41, 3)
    TARGET_VERSION = (2, 139, 12)

    # This is an internal object that provides per-class storage of a per-node data dictionary
    PER_NODE_DATA = {}

    # This is an internal object that describes unchanging attributes in a generic way
    # The values in this list are in no particular order, as a per-attribute tuple
    #     Name, Type, ExtendedTypeIndex, UiName, Description, Metadata,
    #     Is_Required, DefaultValue, Is_Deprecated, DeprecationMsg
    # You should not need to access any of this data directly, use the defined database interfaces
    INTERFACE = og.Database._get_interface([
        ('inputs:gamepadElementIn', 'token', 0, 'Gamepad Element In', 'The gamepad button to trigger the downstream execution.', {'displayGroup': 'parameters', ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.ALLOWED_TOKENS: 'Face Button Bottom,Face Button Right,Face Button Left,Face Button Top,Left Shoulder,Right Shoulder,Special Left,Special Right,Left Stick Button,Right Stick Button,D-Pad Up,D-Pad Right,D-Pad Down,D-Pad Left', ogn.MetadataKeys.ALLOWED_TOKENS_RAW: '{"FaceButtonBottom": "Face Button Bottom", "FaceButtonRight": "Face Button Right", "FaceButtonLeft": "Face Button Left", "FaceButtonTop": "Face Button Top", "LeftShoulder": "Left Shoulder", "RightShoulder": "Right Shoulder", "SpecialLeft": "Special Left", "SpecialRight": "Special Right", "LeftStickButton": "Left Stick Button", "RightStickButton": "Right Stick Button", "DpadUp": "D-Pad Up", "DpadRight": "D-Pad Right", "DpadDown": "D-Pad Down", "DpadLeft": "D-Pad Left"}', ogn.MetadataKeys.DEFAULT: '"Face Button Bottom"'}, True, "Face Button Bottom", False, ''),
        ('inputs:gamepadId', 'uint', 0, 'Gamepad ID', 'Gamepad id number starting from 0. Each gamepad will be registered automatically with an unique ID monotonically increasing in the order they are connected.\nIf gamepad is disconnected, the ID assigned to the remaining gamepad will be adjusted accordingly so the IDs are always continues and start from 0\nChange this value to a non-existing ID will result an error prompt in the console and the node will not listen to any gamepad input.', {ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''),
        ('inputs:onlyPlayback', 'bool', 0, 'Only Simulate On Play', 'When true, the node is only computed while Stage is being played.', {ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.DEFAULT: 'true'}, True, True, False, ''),
        ('outputs:isPressed', 'bool', 0, None, 'True if the gamepad button was pressed, False if it was released', {}, True, None, False, ''),
        ('outputs:pressed', 'execution', 0, 'Pressed', 'Executes when gamepad element was pressed', {}, True, None, False, ''),
        ('outputs:released', 'execution', 0, 'Released', 'Executes when gamepad element was released', {}, True, None, False, ''),
    ])

    class tokens:
        FaceButtonBottom = "Face Button Bottom"
        FaceButtonRight = "Face Button Right"
        FaceButtonLeft = "Face Button Left"
        FaceButtonTop = "Face Button Top"
        LeftShoulder = "Left Shoulder"
        RightShoulder = "Right Shoulder"
        SpecialLeft = "Special Left"
        SpecialRight = "Special Right"
        LeftStickButton = "Left Stick Button"
        RightStickButton = "Right Stick Button"
        DpadUp = "D-Pad Up"
        DpadRight = "D-Pad Right"
        DpadDown = "D-Pad Down"
        DpadLeft = "D-Pad Left"

    @classmethod
    def _populate_role_data(cls):
        """Populate a role structure with the non-default roles on this node type"""
        role_data = super()._populate_role_data()
        role_data.outputs.pressed = og.AttributeRole.EXECUTION
        role_data.outputs.released = og.AttributeRole.EXECUTION
        return role_data

    class ValuesForInputs(og.DynamicAttributeAccess):
        LOCAL_PROPERTY_NAMES = { }
        """Helper class that creates natural hierarchical access to input attributes"""
        def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface):
            """Initialize simplified access for the attribute data"""
            context = node.get_graph().get_default_graph_context()
            super().__init__(context, node, attributes, dynamic_attributes)
            self._batchedReadAttributes = []
            self._batchedReadValues = []

        @property
        def gamepadElementIn(self):
            data_view = og.AttributeValueHelper(self._attributes.gamepadElementIn)
            return data_view.get()

        @gamepadElementIn.setter
        def gamepadElementIn(self, value):
            if self._setting_locked:
                raise og.ReadOnlyError(self._attributes.gamepadElementIn)
            data_view = og.AttributeValueHelper(self._attributes.gamepadElementIn)
            data_view.set(value)

        @property
        def gamepadId(self):
            data_view = og.AttributeValueHelper(self._attributes.gamepadId)
            return data_view.get()

        @gamepadId.setter
        def gamepadId(self, value):
            if self._setting_locked:
                raise og.ReadOnlyError(self._attributes.gamepadId)
            data_view = og.AttributeValueHelper(self._attributes.gamepadId)
            data_view.set(value)

        @property
        def onlyPlayback(self):
            data_view = og.AttributeValueHelper(self._attributes.onlyPlayback)
            return data_view.get()

        @onlyPlayback.setter
        def onlyPlayback(self, value):
            if self._setting_locked:
                raise og.ReadOnlyError(self._attributes.onlyPlayback)
            data_view = og.AttributeValueHelper(self._attributes.onlyPlayback)
            data_view.set(value)

        def _prefetch(self):
            readAttributes = self._batchedReadAttributes
            newValues = _og._prefetch_input_attributes_data(readAttributes)
            if len(readAttributes) == len(newValues):
                self._batchedReadValues = newValues

    class ValuesForOutputs(og.DynamicAttributeAccess):
        LOCAL_PROPERTY_NAMES = { }
        """Helper class that creates natural hierarchical access to output attributes"""
        def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface):
            """Initialize simplified access for the attribute data"""
            context = node.get_graph().get_default_graph_context()
            super().__init__(context, node, attributes, dynamic_attributes)
            self._batchedWriteValues = { }

        @property
        def isPressed(self):
            data_view = og.AttributeValueHelper(self._attributes.isPressed)
            return data_view.get()

        @isPressed.setter
        def isPressed(self, value):
            data_view = og.AttributeValueHelper(self._attributes.isPressed)
            data_view.set(value)

        @property
        def pressed(self):
            data_view = og.AttributeValueHelper(self._attributes.pressed)
            return data_view.get()

        @pressed.setter
        def pressed(self, value):
            data_view = og.AttributeValueHelper(self._attributes.pressed)
            data_view.set(value)

        @property
        def released(self):
            data_view = og.AttributeValueHelper(self._attributes.released)
            return data_view.get()

        @released.setter
        def released(self, value):
            data_view = og.AttributeValueHelper(self._attributes.released)
            data_view.set(value)

        def _commit(self):
            _og._commit_output_attributes_data(self._batchedWriteValues)
            self._batchedWriteValues = { }

    class ValuesForState(og.DynamicAttributeAccess):
        """Helper class that creates natural hierarchical access to state attributes"""
        def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface):
            """Initialize simplified access for the attribute data"""
            context = node.get_graph().get_default_graph_context()
            super().__init__(context, node, attributes, dynamic_attributes)

    def __init__(self, node):
        super().__init__(node)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT)
        self.inputs = OgnOnGamepadInputDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT)
        self.outputs = OgnOnGamepadInputDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE)
        self.state = OgnOnGamepadInputDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
