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.graph.ui.ogn.OgnOnPickedDatabase import OgnOnPickedDatabase
        test_file_name = "OgnOnPickedTemplate.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_ui_OnPicked")
        database = OgnOnPickedDatabase(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:gesture"))
        attribute = test_node.get_attribute("inputs:gesture")
        db_value = database.inputs.gesture
        expected_value = "Left Mouse Click"
        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:onlyPlayback"))
        attribute = test_node.get_attribute("inputs:onlyPlayback")
        db_value = database.inputs.onlyPlayback
        expected_value = True
        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:viewport"))
        attribute = test_node.get_attribute("inputs:viewport")
        db_value = database.inputs.viewport
        expected_value = "Viewport"
        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:isTrackedPrimPicked"))
        attribute = test_node.get_attribute("outputs:isTrackedPrimPicked")
        db_value = database.outputs.isTrackedPrimPicked

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

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

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

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

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