import os
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):

    async def test_data_access(self):
        from omni.graph.nodes.ogn.OgnReadPrimBundleDatabase import OgnReadPrimBundleDatabase
        test_file_name = "OgnReadPrimBundleTemplate.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_graph_nodes_ReadPrimBundle")
        database = OgnReadPrimBundleDatabase(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), 7)

        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:attrNamesToImport"))
        attribute = test_node.get_attribute("inputs:attrNamesToImport")
        db_value = database.inputs.attrNamesToImport
        expected_value = "*"
        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:computeBoundingBox"))
        attribute = test_node.get_attribute("inputs:computeBoundingBox")
        db_value = database.inputs.computeBoundingBox
        expected_value = False
        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:usdTimecode"))
        attribute = test_node.get_attribute("inputs:usdTimecode")
        db_value = database.inputs.usdTimecode
        expected_value = float("NaN")
        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:usePath"))
        attribute = test_node.get_attribute("inputs:usePath")
        db_value = database.inputs.usePath
        expected_value = False
        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_primBundle"))
        attribute = test_node.get_attribute("outputs_primBundle")
        db_value = database.outputs.primBundle

        self.assertTrue(test_node.get_attribute_exists("state:attrNamesToImport"))
        attribute = test_node.get_attribute("state:attrNamesToImport")
        db_value = database.state.attrNamesToImport

        self.assertTrue(test_node.get_attribute_exists("state:computeBoundingBox"))
        attribute = test_node.get_attribute("state:computeBoundingBox")
        db_value = database.state.computeBoundingBox

        self.assertTrue(test_node.get_attribute_exists("state:primPath"))
        attribute = test_node.get_attribute("state:primPath")
        db_value = database.state.primPath

        self.assertTrue(test_node.get_attribute_exists("state:usdTimecode"))
        attribute = test_node.get_attribute("state:usdTimecode")
        db_value = database.state.usdTimecode

        self.assertTrue(test_node.get_attribute_exists("state:usePath"))
        attribute = test_node.get_attribute("state:usePath")
        db_value = database.state.usePath
