import omni.kit.test
import omni.graph.core as og
import omni.graph.core.tests as ogts
from omni.graph.core.tests.omnigraph_test_utils import _TestGraphAndNode
from omni.graph.core.tests.omnigraph_test_utils import _test_clear_scene
from omni.graph.core.tests.omnigraph_test_utils import _test_setup_scene
from omni.graph.core.tests.omnigraph_test_utils import _test_verify_scene


class TestOgn(ogts.OmniGraphTestCase):

    TEST_DATA = [
        {
            'inputs': [
                ['inputs:float3Array', [[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]], False],
                ['inputs:multiplier', 3.0, False],
            ],
            'outputs': [
                ['outputs:float3Array', [[3.0, 6.0, 9.0], [6.0, 9.0, 12.0]], False],
            ],
        },
    ]

    async def test_generated(self):
        test_info = _TestGraphAndNode()
        controller = og.Controller()
        for i, test_run in enumerate(self.TEST_DATA):
            await _test_clear_scene(self, test_run)
            test_info = await _test_setup_scene(self, controller, "/TestGraph", "TestNode_omni_graph_test_TupleArrays", "omni.graph.test.TupleArrays", test_run, test_info)
            await controller.evaluate(test_info.graph)
            _test_verify_scene(self, controller, test_run, test_info, f"omni.graph.test.TupleArrays User test case #{i+1}")

    async def test_vectorized_generated(self):
        test_info = _TestGraphAndNode()
        controller = og.Controller()
        for i, test_run in enumerate(self.TEST_DATA):
            await _test_clear_scene(self, test_run)
            test_info = await _test_setup_scene(self, controller, "/TestGraph", "TestNode_omni_graph_test_TupleArrays","omni.graph.test.TupleArrays", test_run, test_info, 16)
            await controller.evaluate(test_info.graph)
            _test_verify_scene(self, controller, test_run, test_info, f"omni.graph.test.TupleArrays User test case #{i+1}", 16)

    async def test_data_access(self):
        from omni.graph.test.ogn.OgnTestTupleArraysDatabase import OgnTestTupleArraysDatabase
        (_, (test_node,), _, _) = og.Controller.edit("/TestGraph", {
            og.Controller.Keys.CREATE_NODES: ("Template_omni_graph_test_TupleArrays", "omni.graph.test.TupleArrays")
        })
        database = OgnTestTupleArraysDatabase(test_node)
        self.assertTrue(test_node.is_valid())
        node_type_name = test_node.get_type_name()
        self.assertEqual(og.GraphRegistry().get_node_type_version(node_type_name), 1)

        def _attr_error(attribute: og.Attribute, usd_test: bool) -> str:
            test_type = "USD Load" if usd_test else "Database Access"
            return f"{node_type_name} {test_type} Test - {attribute.get_name()} value error"


        self.assertTrue(test_node.get_attribute_exists("inputs:float3Array"))
        attribute = test_node.get_attribute("inputs:float3Array")
        db_value = database.inputs.float3Array
        expected_value = []
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

        self.assertTrue(test_node.get_attribute_exists("inputs:multiplier"))
        attribute = test_node.get_attribute("inputs:multiplier")
        db_value = database.inputs.multiplier
        expected_value = 1.0
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

        self.assertTrue(test_node.get_attribute_exists("outputs:float3Array"))
        attribute = test_node.get_attribute("outputs:float3Array")
        db_value = database.outputs.float3Array
