# Copyright (c) 2018-2020, 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.

__all__ = ["test_widget_tool_button", "test_select_frame", "test_change_query", "test_menu_activation"]

from omni.ui_query import OmniUIQuery
import omni.kit.app
import asyncio
import carb
import omni.ui as ui

wait = None


def test_widget_tool_button():
    preview_tool_button = OmniUIQuery.find_widget("Inspector/Frame[0]/ZStack[1]/VStack[0]/HStack[2]/ToolButton")
    if preview_tool_button:
        preview_tool_button.call_clicked_fn()

    property_tool_button = OmniUIQuery.find_widget("Inspector/Frame[0]/ZStack[1]/VStack[0]/HStack[3]/ToolButton")
    if property_tool_button:
        property_tool_button.call_clicked_fn()

    style_tool_button = OmniUIQuery.find_widget("Inspector/Frame[0]/ZStack[1]/VStack[0]/HStack[4]/ToolButton")
    if style_tool_button:
        style_tool_button.call_clicked_fn()


def test_select_frame():
    global wait
    from .inspector_widget import INSPECTOR_ITEM_SELECTED_ID

    stream = omni.kit.app.get_app().get_message_bus_event_stream()
    stream.push(INSPECTOR_ITEM_SELECTED_ID, sender=444, payload={"item_path": "DemoWindow/Frame[0]/VStack"})

    async def wait_a_frame():
        await omni.kit.app.get_app().next_update_async()

        spacing_label_path = "Inspector/Frame[0]/ZStack[1]/VStack[2]/HStack[3]/VStack[3]/CollapsableFrame[0]/Frame[0]/VStack[1]/HStack[0]/Label"
        spacing_value_path = "Inspector/Frame[0]/ZStack[1]/VStack[2]/HStack[3]/VStack[3]/CollapsableFrame[0]/Frame[0]/VStack[1]/HStack[2]/FloatDrag"

        spacing_label = OmniUIQuery.find_widget(spacing_label_path)
        print("spacing_label", spacing_label)
        if spacing_label and isinstance(spacing_label, ui.Label):
            if not spacing_label.text == "spacing":
                carb.log_error(f"Failed the Frame Selection Test , Label is {spacing_label.text}")
                return

        spacing_value = OmniUIQuery.find_widget(spacing_value_path)
        print("spacing_value", spacing_value)
        if spacing_value and isinstance(spacing_value, ui.FloatDrag):
            if not spacing_value.model.as_float == 0.0:
                carb.log_error(f"Failed the Frame Selection Test , spacing value is {spacing_value.model.as_float}")
                return

        carb.log_warn("Frame Selection Test PASSED")

    wait = asyncio.ensure_future(wait_a_frame())


def test_change_query():

    query_field = OmniUIQuery.find_widget(
        "Inspector/Frame[0]/ZStack[1]/VStack[4]/ZStack[1]/VStack[1]/HStack[2]/StringField"
    )
    if query_field and isinstance(query_field, ui.StringField):
        response_field_path = "Inspector/Frame[0]/ZStack[1]/VStack[4]/ZStack[1]/VStack[1]/HStack[4]/StringField"
        query_field.model.set_value(response_field_path)
        query_field.model.end_edit()

        response_field = OmniUIQuery.find_widget(response_field_path)
        if not isinstance(response_field, ui.StringField):
            carb.log_error(f"Faild to find the right widget at {response_field_path}")
        else:
            print(response_field, response_field.model.as_string)


def test_menu_activation():
    menu_item = OmniUIQuery.find_menu_item("MainWindow/MenuBar[0]/Menu[14]/MenuItem")
    print(menu_item)
    if isinstance(menu_item, ui.MenuItem):
        print(menu_item.text)
        menu_item.call_triggered_fn()
