# 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.usd
import carb
import omni.ui as ui
from omni.kit.test.async_unittest import AsyncTestCase
from omni.kit import ui_test
from omni.kit.test_suite.helpers import get_test_data_path, wait_stage_loading, arrange_windows


class TestContentBrowserSettings(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        super().setUp()
        await arrange_windows()

    async def test_content_browser_settings(self):
        from omni.kit.window.content_browser import get_content_window
        from omni.kit.window.content_browser.test_helper import ContentBrowserTestHelper

        async with ContentBrowserTestHelper() as content_browser_helper:
            path = get_test_data_path(__name__, "folder1/")
            async def get_files():
                return sorted([c.name for c in await content_browser_helper.select_items_async(path, "*")])

            # all off
            await content_browser_helper.set_config_menu_settings({'hide_unknown': False, 'hide_thumbnails': False, 'show_udim_sequence': False, 'show_details': False})
            menu_state = await content_browser_helper.get_config_menu_settings()
            self.assertEqual(menu_state, {'hide_unknown': False, 'hide_thumbnails': False, 'show_details': False, 'show_udim_sequence': False})
            self.assertEqual(await get_files(), ['.thumbs', 'badfile.cheese', 'cheese.1041.png', 'file.9999.xxx.png', 'tile.1001.png', 'tile.1200.png', 'udim.1001.jpg', 'udim.1200.jpg'])

            # unknown enabled
            await content_browser_helper.set_config_menu_settings({'hide_unknown': True, 'hide_thumbnails': False, 'show_udim_sequence': False, 'show_details': False})
            menu_state = await content_browser_helper.get_config_menu_settings()
            self.assertEqual(menu_state, {'hide_unknown': True, 'hide_thumbnails': False, 'show_details': False, 'show_udim_sequence': False})
            self.assertEqual(await get_files(), ['.thumbs', 'cheese.1041.png', 'file.9999.xxx.png', 'tile.1001.png', 'tile.1200.png', 'udim.1001.jpg', 'udim.1200.jpg'])

            # thumbs enabled
            await content_browser_helper.set_config_menu_settings({'hide_unknown': False, 'hide_thumbnails': True, 'show_udim_sequence': False, 'show_details': False})
            menu_state = await content_browser_helper.get_config_menu_settings()
            self.assertEqual(menu_state, {'hide_unknown': False, 'hide_thumbnails': True, 'show_details': False, 'show_udim_sequence': False})
            self.assertEqual(await get_files(), ['badfile.cheese', 'cheese.1041.png', 'file.9999.xxx.png', 'tile.1001.png', 'tile.1200.png', 'udim.1001.jpg', 'udim.1200.jpg'])

            # udim enabled
            await content_browser_helper.set_config_menu_settings({'hide_unknown': False, 'hide_thumbnails': False, 'show_udim_sequence': True, 'show_details': False})
            menu_state = await content_browser_helper.get_config_menu_settings()
            self.assertEqual(menu_state, {'hide_unknown': False, 'hide_thumbnails': False, 'show_details': False, 'show_udim_sequence': True})
            self.assertEqual(await get_files(), ['.thumbs', 'badfile.cheese', 'cheese.<UDIM>.png', 'file.9999.xxx.png', 'tile.<UDIM>.png', 'udim.<UDIM>.jpg'])

            # all enabled
            await content_browser_helper.set_config_menu_settings({'hide_unknown': True, 'hide_thumbnails': True, 'show_udim_sequence': True, 'show_details': False})
            menu_state = await content_browser_helper.get_config_menu_settings()
            self.assertEqual(menu_state, {'hide_unknown': True, 'hide_thumbnails': True, 'show_details': False, 'show_udim_sequence': True})
            self.assertEqual(await get_files(), ['cheese.<UDIM>.png', 'file.9999.xxx.png', 'tile.<UDIM>.png', 'udim.<UDIM>.jpg'])
