## Copyright (c) 2022, 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 os import listdir
from os.path import isfile, join
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, get_prims, arrange_windows


class ExternalDragDropUsdViewportAudio(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_audio_viewport_item(self):
        await ui_test.find("Content").focus()
        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        await wait_stage_loading()

        audio_list = []
        audio_path = get_test_data_path(__name__, f"media/")
        prims = get_prims(stage)
        for item_path in [join(audio_path, f) for f in listdir(audio_path) if isfile(join(audio_path, f))]:
            # position mouse
            await ui_test.find("Viewport").click()
            await ui_test.human_delay()

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

            prim_list = get_prims(stage, prims)
            self.assertEqual(len(prim_list), 1)
            audio_list.append([prim_list[0], os.path.relpath(item_path, audio_path)])
            prims.append(prim_list[0])

        # verify
        self.assertEqual(len(audio_list), 5)
        for prim, audio_file in audio_list:
            asset_path = prim.GetAttribute('filePath').Get()
            self.assertTrue(asset_path.resolvedPath.endswith(audio_file))
