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

Saves specified CPU buffer to disk
"""


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



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

    Class Members:
        node: Node being evaluated

    Attribute Value Properties:
        Inputs:
            inputs.active
            inputs.aovCpu
            inputs.aovGpu
            inputs.autoFileNumber
            inputs.fileName
            inputs.fileNumber
            inputs.fileType
            inputs.frameCount
            inputs.gpu
            inputs.maxInflightWrites
            inputs.rp
            inputs.saveFlags
            inputs.saveLocation
            inputs.startFrame
        Outputs:
            outputs.gpu
            outputs.rp
    """

    # 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:active', 'bool', 0, 'activeReset', 'Alternative to frameCount/startFrame, does a single frame then autoResets to false', {ogn.MetadataKeys.DEFAULT: 'false'}, True, False, False, ''),
        ('inputs:aovCpu', 'string', 0, 'aovCpu', 'Name of AOV representing CPU buffer of GPU resource', {ogn.MetadataKeys.DEFAULT: '""'}, True, "", False, ''),
        ('inputs:aovGpu', 'string', 0, 'aovGpu', 'Name of AOV representing GPU resource, used for querying format + properties', {ogn.MetadataKeys.DEFAULT: '""'}, True, "", False, ''),
        ('inputs:autoFileNumber', 'int', 0, 'autoFileNumber', 'If non zero, this number will be the starting number for export. Each invocation of this node increases the number by 1.', {ogn.MetadataKeys.DEFAULT: '-1'}, True, -1, False, ''),
        ('inputs:fileName', 'string', 0, 'fileName', 'Optional, if specified the output filename will be fileName_{aovGpu}.{fileType}', {ogn.MetadataKeys.DEFAULT: '""'}, True, "", False, ''),
        ('inputs:fileNumber', 'int', 0, 'fileNumber', "Number that will be appended to the exported filename. If -1 then the render product's frame number will be used.", {ogn.MetadataKeys.DEFAULT: '-1'}, True, -1, False, ''),
        ('inputs:fileType', 'string', 0, 'fileType', 'bmp,png,exr', {ogn.MetadataKeys.DEFAULT: '"png"'}, True, "png", False, ''),
        ('inputs:frameCount', 'int64', 0, 'frameCount', 'Number of frames to capture (-1 means never stop)', {ogn.MetadataKeys.DEFAULT: '-1'}, True, -1, False, ''),
        ('inputs:gpu', 'uint64', 0, 'gpuFoundations', 'Pointer to shared context containing gpu foundations', {}, True, 0, False, ''),
        ('inputs:maxInflightWrites', 'int', 0, 'maxInflightWrites', 'Maximum number of in-flight file write operations before blocking on file i/o', {ogn.MetadataKeys.DEFAULT: '2'}, True, 2, False, ''),
        ('inputs:rp', 'uint64', 0, 'renderProduct', 'Pointer to render product for this view', {}, True, 0, False, ''),
        ('inputs:saveFlags', 'uint64', 0, 'saveFlags', 'Flags that will be passed to carb::imaging::IImaging for file saving.', {ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''),
        ('inputs:saveLocation', 'string', 0, 'saveLocation', 'Folder to save AOVs as AOV_FrameNumber.{exr,png}', {ogn.MetadataKeys.DEFAULT: '""'}, True, "", False, ''),
        ('inputs:startFrame', 'uint64', 0, 'startFrame', 'Frame to begin saving to disk', {ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''),
        ('outputs:gpu', 'uint64', 0, 'gpuFoundations', 'Pointer to shared context containing gpu foundations', {}, True, None, False, ''),
        ('outputs:rp', 'uint64', 0, 'renderProduct', 'Pointer to render product for this view', {}, 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.aovCpu = og.AttributeRole.TEXT
        role_data.inputs.aovGpu = og.AttributeRole.TEXT
        role_data.inputs.fileName = og.AttributeRole.TEXT
        role_data.inputs.fileType = og.AttributeRole.TEXT
        role_data.inputs.saveLocation = og.AttributeRole.TEXT
        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 active(self):
            data_view = og.AttributeValueHelper(self._attributes.active)
            return data_view.get()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        @startFrame.setter
        def startFrame(self, value):
            if self._setting_locked:
                raise og.ReadOnlyError(self._attributes.startFrame)
            data_view = og.AttributeValueHelper(self._attributes.startFrame)
            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 gpu(self):
            data_view = og.AttributeValueHelper(self._attributes.gpu)
            return data_view.get()

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

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

        @rp.setter
        def rp(self, value):
            data_view = og.AttributeValueHelper(self._attributes.rp)
            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 = OgnGpuInteropCpuToDiskDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT)
        self.outputs = OgnGpuInteropCpuToDiskDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes)
        dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE)
        self.state = OgnGpuInteropCpuToDiskDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
