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.OgnExportUSDPrimDatabase import OgnExportUSDPrimDatabase
        test_file_name = "OgnExportUSDPrimTemplate.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_ExportUSDPrim")
        database = OgnExportUSDPrimDatabase(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: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:attrNamesToExport"))
        attribute = test_node.get_attribute("inputs:attrNamesToExport")
        db_value = database.inputs.attrNamesToExport
        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:bundle"))
        attribute = test_node.get_attribute("inputs:bundle")
        db_value = database.inputs.bundle

        self.assertTrue(test_node.get_attribute_exists("inputs:excludedAttrNames"))
        attribute = test_node.get_attribute("inputs:excludedAttrNames")
        db_value = database.inputs.excludedAttrNames
        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:exportToRootLayer"))
        attribute = test_node.get_attribute("inputs:exportToRootLayer")
        db_value = database.inputs.exportToRootLayer
        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:layerName"))
        attribute = test_node.get_attribute("inputs:layerName")
        db_value = database.inputs.layerName
        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:onlyExportToExisting"))
        attribute = test_node.get_attribute("inputs:onlyExportToExisting")
        db_value = database.inputs.onlyExportToExisting
        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: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:primPathFromBundle"))
        attribute = test_node.get_attribute("inputs:primPathFromBundle")
        db_value = database.inputs.primPathFromBundle
        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:removeMissingAttrs"))
        attribute = test_node.get_attribute("inputs:removeMissingAttrs")
        db_value = database.inputs.removeMissingAttrs
        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: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 = 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 = 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("state:prevApplyTransform"))
        attribute = test_node.get_attribute("state:prevApplyTransform")
        db_value = database.state.prevApplyTransform

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

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

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

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

        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:prevLayerName"))
        attribute = test_node.get_attribute("state:prevLayerName")
        db_value = database.state.prevLayerName

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

        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:prevPrimDirtyIDs"))
        attribute = test_node.get_attribute("state:prevPrimDirtyIDs")
        db_value = database.state.prevPrimDirtyIDs

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

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

        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:prevUsdTimecode"))
        attribute = test_node.get_attribute("state:prevUsdTimecode")
        db_value = database.state.prevUsdTimecode
