## Copyright (c) 2023, 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 os
import carb
import omni.kit.app
from omni.kit.test.async_unittest import AsyncTestCase
import omni.usd
import omni.ui as ui

from omni.kit import ui_test
from pxr import Vt, Gf
from omni.kit.test_suite.helpers import get_test_data_path, arrange_windows
from omni.kit.quicklayout import QuickLayout
from omni.kit.menu.utils import MenuItemDescription, MenuAlignment
from omni.kit.ui_test.query import MenuRef

PERSISTENT_SETTINGS_PREFIX = "/persistent"


class TestQuicklayoutMenu(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        await arrange_windows()
#        carb.settings.get_settings().set(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout", "$$last_used$$")
        self.assertEqual(QuickLayout.get_default_layouts(), [])

        data_path = get_test_data_path(__name__)
        self._layout_data = [ ["Test 1", f"{data_path}/layouts/test1.json", carb.input.KeyboardInput.KEY_1],
                              ["Test 2", f"{data_path}/layouts/test2.json", carb.input.KeyboardInput.KEY_2]]
        QuickLayout.add_default_layouts(self._layout_data)

    # After running each test
    async def tearDown(self):
        QuickLayout.remove_default_layouts(self._layout_data)

        carb.settings.get_settings().set(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout", "$$last_used$$")
        self.assertEqual(QuickLayout.get_default_layouts(), [])

    async def test_layout_menu(self):
        # add layout placeholder
        menu_placeholder = [MenuItemDescription(name="placeholder", show_fn=lambda: False)]
        omni.kit.menu.utils.add_menu_items(menu_placeholder, name="Layout")
        await ui_test.human_delay(10)

        layout_menu_items = []

        def add_layout_menu_entry(name, parameter, key):
            item = MenuItemDescription(name=name)
            layout_menu_items.append(item)

        for item in QuickLayout.get_default_layouts():
            add_layout_menu_entry(f"{item[0]}", item[1], item[2])

        # add menu
        layout_delegate = QuickLayout.LayoutMenuDelegate(read_fn=lambda: "Test 2",
                                                               layout_id="Test 2",
                                                               text_width=175,
                                                               hotkey_width=75)

        omni.kit.menu.utils.add_menu_items(layout_menu_items, "Layout", delegate=layout_delegate)
        await ui_test.human_delay(10)

        # click layout
        layout_menu = omni.kit.ui_test.get_menubar().find_menu("Layout")
        await layout_menu.click()
        await ui_test.human_delay(10)

        # verify menu icon status
        layout_menu_icons = layout_menu.widget.delegate._layout_menu_icons
        self.assertFalse(layout_menu.widget.delegate._layout_menu_icons['Test 1']['selected_image'].enabled)
        self.assertTrue(layout_menu.widget.delegate._layout_menu_icons['Test 2']['selected_image'].enabled)

        # remove menus
        omni.kit.menu.utils.remove_menu_items(layout_menu_items, name="Layout")
        omni.kit.menu.utils.remove_menu_items(menu_placeholder, name="Layout")
        await ui_test.human_delay(10)

