"""Support for simplified access to data on nodes of type omni.graph.test.TypeResolution

Test node, explicitly constructed to make the attribute type resolution mechanism testable. It has output attributes with
union and any types whose type will be resolved at runtime when they are connected to inputs with a fixed type. The extra
string output provides the resolution information to the test script for verification
"""

from typing import Any
import carb
import numpy

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



class OgnTestTypeResolutionDatabase(og.Database):
    """Helper class providing simplified access to data on nodes of type omni.graph.test.TypeResolution

    Class Members:
        node: Node being evaluated

    Attribute Value Properties:
        Inputs:
            inputs.anyValueIn
        Outputs:
            outputs.anyValue
            outputs.arrayValue
            outputs.mixedValue
            outputs.resolvedType
            outputs.tupleArrayValue
            outputs.tupleValue
            outputs.value
    """

    # 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:anyValueIn', 'any', 2, None, 'Input that can resolve to any type.  Internally the node couples \nthe types of inputs:anyValueIn -> outputs:anyValue.  Meaning if one is resolved it will\nautomatically resolve the other', {}, True, None, False, ''),
        ('outputs:anyValue', 'any', 2, None, 'Output that can resolve to any type at all', {}, True, None, False, ''),
        ('outputs:arrayValue', 'double[],float[],int64[],int[],uint64[],uint[]', 1, None, 'Output that only resolves to one of the numeric array types.', {}, True, None, False, ''),
        ('outputs:mixedValue', 'float,float[3],float[3][],float[]', 1, None, 'Output that can resolve to data of different shapes.', {}, True, None, False, ''),
        ('outputs:resolvedType', 'token[]', 0, None, "Array of strings representing the output attribute's type resolutions.\nThe array items consist of comma-separated pairs of strings representing\nthe output attribute name and the type to which it is currently resolved.\ne.g. if the attribute 'foo' is an integer there would be one entry in the array\nwith the string 'foo,int'", {}, True, None, False, ''),
        ('outputs:tupleArrayValue', 'double[3][],float[3][],int[3][]', 1, None, 'Output that only resolves to one of the numeric tuple array types.', {}, True, None, False, ''),
        ('outputs:tupleValue', 'double[3],float[3],int[3]', 1, None, 'Output that only resolves to one of the numeric tuple types.', {}, True, None, False, ''),
        ('outputs:value', 'double,float,int,int64,uint,uint64', 1, None, 'Output that only resolves to one of the numeric types.', {}, True, None, False, ''),
    ])

    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 anyValueIn(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute inputs.anyValueIn"""
            return og.RuntimeAttribute(self._attributes.anyValueIn.get_attribute_data(), self._context, True)

        @anyValueIn.setter
        def anyValueIn(self, value_to_set: Any):
            """Assign another attribute's value to outputs.anyValueIn"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.anyValueIn.value = value_to_set.value
            else:
                self.anyValueIn.value = value_to_set

        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.resolvedType_size = None
            self._batchedWriteValues = { }

        @property
        def anyValue(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.anyValue"""
            return og.RuntimeAttribute(self._attributes.anyValue.get_attribute_data(), self._context, False)

        @anyValue.setter
        def anyValue(self, value_to_set: Any):
            """Assign another attribute's value to outputs.anyValue"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.anyValue.value = value_to_set.value
            else:
                self.anyValue.value = value_to_set

        @property
        def arrayValue(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.arrayValue"""
            return og.RuntimeAttribute(self._attributes.arrayValue.get_attribute_data(), self._context, False)

        @arrayValue.setter
        def arrayValue(self, value_to_set: Any):
            """Assign another attribute's value to outputs.arrayValue"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.arrayValue.value = value_to_set.value
            else:
                self.arrayValue.value = value_to_set

        @property
        def mixedValue(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.mixedValue"""
            return og.RuntimeAttribute(self._attributes.mixedValue.get_attribute_data(), self._context, False)

        @mixedValue.setter
        def mixedValue(self, value_to_set: Any):
            """Assign another attribute's value to outputs.mixedValue"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.mixedValue.value = value_to_set.value
            else:
                self.mixedValue.value = value_to_set

        @property
        def resolvedType(self):
            data_view = og.AttributeValueHelper(self._attributes.resolvedType)
            return data_view.get(reserved_element_count=self.resolvedType_size)

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

        @property
        def tupleArrayValue(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.tupleArrayValue"""
            return og.RuntimeAttribute(self._attributes.tupleArrayValue.get_attribute_data(), self._context, False)

        @tupleArrayValue.setter
        def tupleArrayValue(self, value_to_set: Any):
            """Assign another attribute's value to outputs.tupleArrayValue"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.tupleArrayValue.value = value_to_set.value
            else:
                self.tupleArrayValue.value = value_to_set

        @property
        def tupleValue(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.tupleValue"""
            return og.RuntimeAttribute(self._attributes.tupleValue.get_attribute_data(), self._context, False)

        @tupleValue.setter
        def tupleValue(self, value_to_set: Any):
            """Assign another attribute's value to outputs.tupleValue"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.tupleValue.value = value_to_set.value
            else:
                self.tupleValue.value = value_to_set

        @property
        def value(self) -> og.RuntimeAttribute:
            """Get the runtime wrapper class for the attribute outputs.value"""
            return og.RuntimeAttribute(self._attributes.value.get_attribute_data(), self._context, False)

        @value.setter
        def value(self, value_to_set: Any):
            """Assign another attribute's value to outputs.value"""
            if isinstance(value_to_set, og.RuntimeAttribute):
                self.value.value = value_to_set.value
            else:
                self.value.value = value_to_set

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