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:simpleTokens', "red", False],
                ['inputs:specialTokens', "<", False],
            ],
            'outputs': [
                ['outputs:combinedTokens', "red<", False],
            ],
        },
        {
            'inputs': [
                ['inputs:simpleTokens', "green", False],
                ['inputs:specialTokens', ">", False],
            ],
            'outputs': [
                ['outputs:combinedTokens', "green>", False],
            ],
        },
        {
            'inputs': [
                ['inputs:simpleTokens', "blue", False],
                ['inputs:specialTokens', "<", False],
            ],
            'outputs': [
                ['outputs:combinedTokens', "blue<", 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_TestAllowedTokens", "omni.graph.test.TestAllowedTokens", test_run, test_info)
            await controller.evaluate(test_info.graph)
            _test_verify_scene(self, controller, test_run, test_info, f"omni.graph.test.TestAllowedTokens 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_TestAllowedTokens","omni.graph.test.TestAllowedTokens", test_run, test_info, 16)
            await controller.evaluate(test_info.graph)
            _test_verify_scene(self, controller, test_run, test_info, f"omni.graph.test.TestAllowedTokens User test case #{i+1}", 16)

    async def test_data_access(self):
        from omni.graph.test.ogn.OgnTestAllowedTokensDatabase import OgnTestAllowedTokensDatabase
        (_, (test_node,), _, _) = og.Controller.edit("/TestGraph", {
            og.Controller.Keys.CREATE_NODES: ("Template_omni_graph_test_TestAllowedTokens", "omni.graph.test.TestAllowedTokens")
        })
        database = OgnTestAllowedTokensDatabase(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:simpleTokens"))
        attribute = test_node.get_attribute("inputs:simpleTokens")
        db_value = database.inputs.simpleTokens
        expected_value = "red"
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

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

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