## 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.app
import omni.kit.test
import omni.kit.ui_test
import omni.ui as ui
import omni.usd
import omni.timeline
import carb.tokens
import omni.usd.audio
from omni.ui.tests.test_base import OmniUiTest

import pathlib
import asyncio;


class TestAudioPlayerWindow(OmniUiTest):  # pragma: no cover

    async def _dock_window(self):
        await self.docked_test_window(
            window=self._win.window,
            width=600,
            height=200)

    def _dump_ui_tree(self, root):
        print("DUMP UI TREE START")
        #windows = omni.ui.Workspace.get_windows()
        #children = [windows[0].frame]
        children = [root.frame]

        print(str(dir(root.frame)))
        def recurse(children, path=""):
            for c in children:
                name = path + "/" + type(c).__name__
                print(name)
                if isinstance(c, omni.ui.ComboBox):
                    print(str(dir(c)))

                recurse(omni.ui.Inspector.get_children(c), name)

        recurse(children)
        print("DUMP UI TREE END")

    async def setUp(self):
        await super().setUp()

        extension_path = carb.tokens.get_tokens_interface().resolve("${omni.kit.window.audioplayer}")
        self._test_path = pathlib.Path(extension_path).joinpath("data").joinpath("tests").absolute()
        self._golden_img_dir = self._test_path.joinpath("golden")

        # open the dropdown
        window_menu = omni.kit.ui_test.get_menubar().find_menu("Window")
        self.assertIsNotNone(window_menu)
        await window_menu.click()

        # click the audioplayer option to open it
        player_menu = omni.kit.ui_test.get_menubar().find_menu("Audio Player")
        self.assertIsNotNone(player_menu)
        await player_menu.click()

        self._win = omni.kit.ui_test.find("Audio Player")
        self.assertIsNotNone(self._win)

        self._file_name_textbox = self._win.find("**/StringField[*]")
        self.assertIsNotNone(self._file_name_textbox)

    async def _test_just_opened(self):
        await self._dock_window()
        await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name="test_just_opened.png")

    async def _test_load_file(self):
        await self._file_name_textbox.click()
        await self._file_name_textbox.input(str(self._test_path / "1hz.oga"))

        await asyncio.sleep(1.0)

        # delete the text in the textbox so we'll have something constant
        # for the image comparison
        await self._file_name_textbox.double_click()
        await omni.kit.ui_test.emulate_keyboard_press(carb.input.KeyboardInput.DEL)

        await self._dock_window()
        await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name="test_open_file.png")

    async def test_all(self):
        await self._test_just_opened()
        await self._test_load_file()
        self._dump_ui_tree(self._win.window)
