"""Support for simplified access to data on nodes of type omni.graph.examples.cpp.Smooth

Smooths data in a very simple way by averaging values with neighbors.
"""


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



class OgnExampleSmoothDatabase(og.Database):
    """Helper class providing simplified access to data on nodes of type omni.graph.examples.cpp.Smooth

    Class Members:
        node: Node being evaluated

    Attribute Value Properties:
        Inputs:
            inputs.iterations
            inputs.mesh
            inputs.nameOfAttributeToSmooth
            inputs.nameOfNeighborStartsInputAttribute
            inputs.nameOfNeighborsInputAttribute
            inputs.useGPU
        Outputs:
            outputs.mesh
    """

    # 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:iterations', 'int', 0, None, 'Number of times to average neighboring values', {ogn.MetadataKeys.DEFAULT: '5'}, True, 5, False, ''),
        ('inputs:mesh', 'bundle', 0, None, 'Bundle containing data to be smoothed and neighbor arrays.', {}, True, None, False, ''),
        ('inputs:nameOfAttributeToSmooth', 'token', 0, None, "Name of the attribute in 'mesh' containing the data to be smoothed", {ogn.MetadataKeys.DEFAULT: '"points"'}, True, "points", False, ''),
        ('inputs:nameOfNeighborStartsInputAttribute', 'token', 0, None, "Name of the 'int[]' attribute in 'mesh' containing the beginning index of neighbors of each point in the array attribute specified by 'nameOfNeighborsInputAttribute'", {ogn.MetadataKeys.DEFAULT: '"neighborStarts"'}, True, "neighborStarts", False, ''),
        ('inputs:nameOfNeighborsInputAttribute', 'token', 0, None, "Name of the 'int[]' attribute in 'mesh' containing the neighbors of all points.  The beginnings of each point's neighbors within this array are provided in the attribute specified by 'nameOfNeighborStartsInputAttribute'.", {ogn.MetadataKeys.DEFAULT: '"neighbors"'}, True, "neighbors", False, ''),
        ('inputs:useGPU', 'bool', 0, None, 'When this option is on, the node will use the GPU to perform the smoothing computation.', {ogn.MetadataKeys.DEFAULT: 'true'}, True, True, False, ''),
        ('outputs:mesh', 'bundle', 0, None, "A copy of 'mesh' with the specified attribute smoothed.", {}, True, None, False, ''),
    ])

    @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.inputs.mesh = og.AttributeRole.BUNDLE
        role_data.outputs.mesh = og.AttributeRole.BUNDLE
        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.__bundles = og.BundleContainer(context, node, attributes, [], read_only=True, gpu_ptr_kinds={})
            self._batchedReadAttributes = []
            self._batchedReadValues = []

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

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

        @property
        def mesh(self) -> og.BundleContents:
            """Get the bundle wrapper class for the attribute inputs.mesh"""
            return self.__bundles.mesh

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

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

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

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

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

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

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

        @useGPU.setter
        def useGPU(self, value):
            if self._setting_locked:
                raise og.ReadOnlyError(self._attributes.useGPU)
            data_view = og.AttributeValueHelper(self._attributes.useGPU)
            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.__bundles = og.BundleContainer(context, node, attributes, [], read_only=False, gpu_ptr_kinds={})
            self._batchedWriteValues = { }

        @property
        def mesh(self) -> og.BundleContents:
            """Get the bundle wrapper class for the attribute outputs.mesh"""
            return self.__bundles.mesh

        @mesh.setter
        def mesh(self, bundle: og.BundleContents):
            """Overwrite the bundle attribute outputs.mesh with a new bundle"""
            if not isinstance(bundle, og.BundleContents):
                carb.log_error("Only bundle attributes can be assigned to another bundle attribute")
            self.__bundles.mesh.bundle = bundle

        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 = OgnExampleSmoothDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT)
        self.outputs = OgnExampleSmoothDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE)
        self.state = OgnExampleSmoothDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
