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.io.ogn.OgnImportUSDPrimDatabase import OgnImportUSDPrimDatabase
        test_file_name = "OgnImportUSDPrimTemplate.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_ImportUSDPrim")
        database = OgnImportUSDPrimDatabase(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:applySkelBinding"))
        attribute = test_node.get_attribute("inputs:applySkelBinding")
        db_value = database.inputs.applySkelBinding
        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:applyTransform"))
        attribute = test_node.get_attribute("inputs:applyTransform")
        db_value = database.inputs.applyTransform
        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: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:importAttributes"))
        attribute = test_node.get_attribute("inputs:importAttributes")
        db_value = database.inputs.importAttributes
        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:importPath"))
        attribute = test_node.get_attribute("inputs:importPath")
        db_value = database.inputs.importPath
        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:importPrimvarMetadata"))
        attribute = test_node.get_attribute("inputs:importPrimvarMetadata")
        db_value = database.inputs.importPrimvarMetadata
        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:importTime"))
        attribute = test_node.get_attribute("inputs:importTime")
        db_value = database.inputs.importTime
        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:importTransform"))
        attribute = test_node.get_attribute("inputs:importTransform")
        db_value = database.inputs.importTransform
        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:importType"))
        attribute = test_node.get_attribute("inputs:importType")
        db_value = database.inputs.importType
        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:inputAttrNames"))
        attribute = test_node.get_attribute("inputs:inputAttrNames")
        db_value = database.inputs.inputAttrNames
        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:keepPrimsSeparate"))
        attribute = test_node.get_attribute("inputs:keepPrimsSeparate")
        db_value = database.inputs.keepPrimsSeparate
        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:outputAttrNames"))
        attribute = test_node.get_attribute("inputs:outputAttrNames")
        db_value = database.inputs.outputAttrNames
        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:renameAttributes"))
        attribute = test_node.get_attribute("inputs:renameAttributes")
        db_value = database.inputs.renameAttributes
        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:timeVaryingAttributes"))
        attribute = test_node.get_attribute("inputs:timeVaryingAttributes")
        db_value = database.inputs.timeVaryingAttributes
        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:usdTimecode"))
        attribute = test_node.get_attribute("inputs:usdTimecode")
        db_value = database.inputs.usdTimecode
        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("outputs_output"))
        attribute = test_node.get_attribute("outputs_output")
        db_value = database.outputs.output

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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