import omni.kit.test
import omni.graph.core as og
import omni.graph.core.tests as ogts
import os


class TestOgn(ogts.OmniGraphTestCase):

    async def test_data_access(self):
        from omni.kit.procedural.mesh.ogn.OgnProceduralMeshDiskDatabase import OgnProceduralMeshDiskDatabase
        test_file_name = "OgnProceduralMeshDiskTemplate.usda"
        usd_path = os.path.join(os.path.dirname(__file__), "usd", test_file_name)
        if not os.path.exists(usd_path):
            self.assertTrue(False, f"{usd_path} not found for loading test")
        (result, error) = await ogts.load_test_file(usd_path)
        self.assertTrue(result, f'{error} on {usd_path}')
        test_node = og.Controller.node("/TestGraph/Template_omni_kit_procedural_mesh_CreateMeshDisk")
        database = OgnProceduralMeshDiskDatabase(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:capResolution"))
        attribute = test_node.get_attribute("inputs:capResolution")
        db_value = database.inputs.capResolution
        expected_value = 10
        actual_value = og.Controller.get(attribute)
        ogts.verify_values(expected_value, actual_value, _attr_error(attribute, True))
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

        self.assertTrue(test_node.get_attribute_exists("inputs:diameter"))
        attribute = test_node.get_attribute("inputs:diameter")
        db_value = database.inputs.diameter
        expected_value = 100
        actual_value = og.Controller.get(attribute)
        ogts.verify_values(expected_value, actual_value, _attr_error(attribute, True))
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

        self.assertTrue(test_node.get_attribute_exists("inputs:longitudeResolution"))
        attribute = test_node.get_attribute("inputs:longitudeResolution")
        db_value = database.inputs.longitudeResolution
        expected_value = 10
        actual_value = og.Controller.get(attribute)
        ogts.verify_values(expected_value, actual_value, _attr_error(attribute, True))
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

        self.assertTrue(test_node.get_attribute_exists("inputs:parameterCheck"))
        attribute = test_node.get_attribute("inputs:parameterCheck")
        db_value = database.inputs.parameterCheck
        expected_value = -1
        actual_value = og.Controller.get(attribute)
        ogts.verify_values(expected_value, actual_value, _attr_error(attribute, True))
        ogts.verify_values(expected_value, db_value, _attr_error(attribute, False))

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

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

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

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

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

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