## Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.
##
## NVIDIA CORPORATION and its licensors retain all intellectual property
## and proprietary rights in and to this software, related documentation
## and any modifications thereto.  Any use, reproduction, disclosure or
## distribution of this software and related documentation without an express
## license agreement from NVIDIA CORPORATION is strictly prohibited.
##
import omni.kit.test
import os
import carb
import omni.usd
import omni.kit.app
from omni.kit.test.async_unittest import AsyncTestCase
from omni.kit import ui_test
from pxr import Sdf, UsdShade
from omni.kit.test_suite.helpers import open_stage, get_test_data_path, wait_stage_loading, arrange_windows


class ExternalDragDropUsdStageItem(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        carb.settings.get_settings().set("/persistent/app/stage/dragDropImport", "reference")
        await arrange_windows("Stage", 512)
        await open_stage(get_test_data_path(__name__, "bound_shapes.usda"))


    # After running each test
    async def tearDown(self):
        await wait_stage_loading()
        carb.settings.get_settings().set("/persistent/app/stage/dragDropImport", "reference")


    async def test_l1_external_drag_drop_usd_stage_item_reference(self):
        await ui_test.find("Content").focus()
        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        await wait_stage_loading()

        # set dragDropImport
        carb.settings.get_settings().set("/persistent/app/stage/dragDropImport", "reference")

        # get file
        usd_path = get_test_data_path(__name__, f"shapes/basic_sphere.usda").replace("\\", "/")

        # position mouse
        await ui_test.find("Stage").click()
        await ui_test.human_delay()

        # simulate drag/drop
        omni.appwindow.get_default_app_window().get_window_drop_event_stream().push(0, 0, {'paths': [usd_path]})
        await ui_test.human_delay(50)

        # verify
        prim = stage.GetPrimAtPath("/World/basic_sphere")
        payloads = omni.usd.get_composed_payloads_from_prim(prim)
        references = omni.usd.get_composed_references_from_prim(prim)
        self.assertEqual(payloads, [])
        self.assertEqual(len(references), 1)
        for (ref, layer) in references:
            absolute_path = layer.ComputeAbsolutePath(ref.assetPath)
            self.assertEqual(os.path.normpath(absolute_path).lower(), os.path.normpath(usd_path).lower())

    async def test_l1_external_drag_drop_usd_stage_item_payload(self):
        await ui_test.find("Content").focus()
        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        await wait_stage_loading()

        # set dragDropImport
        carb.settings.get_settings().set("/persistent/app/stage/dragDropImport", "payload")

        # get file
        usd_path = get_test_data_path(__name__, f"shapes/basic_cone.usda").replace("\\", "/")

        # position mouse
        await ui_test.find("Stage").click()
        await ui_test.human_delay()

        # simulate drag/drop
        omni.appwindow.get_default_app_window().get_window_drop_event_stream().push(0, 0, {'paths': [usd_path]})
        await ui_test.human_delay(50)

        # verify
        prim = stage.GetPrimAtPath("/World/basic_cone")
        payloads = omni.usd.get_composed_payloads_from_prim(prim)
        references = omni.usd.get_composed_references_from_prim(prim)
        self.assertEqual(references, [])
        self.assertEqual(len(payloads), 1)
        for (ref, layer) in payloads:
            absolute_path = layer.ComputeAbsolutePath(ref.assetPath)
            self.assertEqual(os.path.normpath(absolute_path).lower(), os.path.normpath(usd_path).lower())
