import carb
import omni.ui as ui
from omni.kit import ui_test


def verify_menu_items(cls, verify_list, use_menu_spacers: bool=False):
    menu_widget = ui_test.get_menubar()
    menu_widgets = []

    def show_debug():
        debug_info = ""
        for w in menu_widgets:
            if isinstance(w.widget, (ui.Menu, ui.MenuItem, ui.Separator)):
                debug_info += f"(ui.{w.widget.__class__.__name__}, \"{w.widget.text.strip()}\", {w.widget.visible}), "
            elif isinstance(w.widget, ui.Spacer) and use_menu_spacers:
                debug_info += f"(ui.{w.widget.__class__.__name__}, {w.widget.visible}), "

        carb.log_warn(f"verify_menu_items [{debug_info[:-2]}] vs {verify_list}")

    for w in menu_widget.find_all("**/"):
        if isinstance(w.widget, (ui.Menu, ui.MenuItem, ui.Separator)):
            menu_widgets.append(w)
        elif isinstance(w.widget, ui.Spacer) and use_menu_spacers and w.widget.identifier in ["right_aligned_menus", "right_padding"]:
            menu_widgets.append(w)

    try:
        cls.assertEqual(len(menu_widgets), len(verify_list))
        for index, item in enumerate(verify_list):
            widget = menu_widgets[index]
            cls.assertTrue(isinstance(widget.widget, item[0]), f"menu type error {widget.widget} vs {item[0]}")
            if isinstance(widget.widget, (ui.Menu, ui.MenuItem, ui.Separator)):
                cls.assertEqual(widget.widget.text.strip(), item[1])
                cls.assertEqual(widget.widget.visible, item[2])
            elif isinstance(widget.widget, ui.Spacer) and use_menu_spacers and widget.widget.identifier in ["right_aligned_menus", "right_padding"]:
                cls.assertEqual(widget.widget.visible, item[1])
    except Exception as exc:
        show_debug()
        raise Exception(exc)

def verify_menu_checked_items(cls, verify_list):
    menu_widget = ui_test.get_menubar()
    menu_widgets = []

    def show_debug():
        debug_info = ""
        for w in menu_widgets:
            if isinstance(w.widget, (ui.Menu, ui.MenuItem)) and w.widget.text.strip() != "placeholder":
                debug_info += f"(ui.{w.widget.__class__.__name__}, \"{w.widget.text.strip()}\", {w.widget.visible}, {w.widget.checkable}, {w.widget.checked}), "
        carb.log_warn(f"verify_menu_items [{debug_info[:-2]}] vs {verify_list}")

    for w in menu_widget.find_all("**/"):
        if isinstance(w.widget, (ui.Menu, ui.MenuItem)) and w.widget.text.strip() != "placeholder":
            menu_widgets.append(w)

    try:
        cls.assertEqual(len(menu_widgets), len(verify_list))
        for index, item in enumerate(verify_list):
            widget = menu_widgets[index]
            cls.assertTrue(isinstance(widget.widget, item[0]), f"menu type error {widget.widget} vs {item[0]}")
            cls.assertEqual(widget.widget.text.strip(), item[1])
            cls.assertEqual(widget.widget.visible, item[2])
            cls.assertEqual(widget.widget.checkable, item[3])
            cls.assertEqual(widget.widget.checked, item[4])
    except Exception as exc:
        show_debug()
        raise Exception(exc)

