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.syntheticdata.ogn.OgnSdFabricTimeRangeExecutionDatabase import OgnSdFabricTimeRangeExecutionDatabase
        test_file_name = "OgnSdFabricTimeRangeExecutionTemplate.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_syntheticdata_SdFabricTimeRangeExecution")
        database = OgnSdFabricTimeRangeExecutionDatabase(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:exec"))
        attribute = test_node.get_attribute("inputs:exec")
        db_value = database.inputs.exec

        self.assertTrue(test_node.get_attribute_exists("inputs:gpu"))
        attribute = test_node.get_attribute("inputs:gpu")
        db_value = database.inputs.gpu
        expected_value = 0
        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:renderResults"))
        attribute = test_node.get_attribute("inputs:renderResults")
        db_value = database.inputs.renderResults
        expected_value = 0
        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:timeRangeBeginDenominatorToken"))
        attribute = test_node.get_attribute("inputs:timeRangeBeginDenominatorToken")
        db_value = database.inputs.timeRangeBeginDenominatorToken
        expected_value = "timeRangeStartDenominator"
        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:timeRangeBeginNumeratorToken"))
        attribute = test_node.get_attribute("inputs:timeRangeBeginNumeratorToken")
        db_value = database.inputs.timeRangeBeginNumeratorToken
        expected_value = "timeRangeStartNumerator"
        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:timeRangeEndDenominatorToken"))
        attribute = test_node.get_attribute("inputs:timeRangeEndDenominatorToken")
        db_value = database.inputs.timeRangeEndDenominatorToken
        expected_value = "timeRangeEndDenominator"
        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:timeRangeEndNumeratorToken"))
        attribute = test_node.get_attribute("inputs:timeRangeEndNumeratorToken")
        db_value = database.inputs.timeRangeEndNumeratorToken
        expected_value = "timeRangeEndNumerator"
        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:timeRangeName"))
        attribute = test_node.get_attribute("inputs:timeRangeName")
        db_value = database.inputs.timeRangeName
        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("outputs:exec"))
        attribute = test_node.get_attribute("outputs:exec")
        db_value = database.outputs.exec

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

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

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

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