
__all__ = ["InspectorWidget"]

import omni.ui as ui

from .tree.inspector_tree import InspectorTreeView
from .style.style_tree import StyleTreeView
from .property.inspector_property_widget import InspectorPropertyWidget

from .inspector_preview import InspectorPreview
from omni.ui_query import OmniUIQuery

# move the test into a menu on the top widget to experiment
from .inspector_tests import test_widget_tool_button, test_select_frame, test_change_query, test_menu_activation
from .inspector_style import INSPECTOR_STYLE

DARK_BACKGROUND = 0xFF333333

INSPECTOR_ITEM_SELECTED_ID = 427273737  # hash("INSPECTOR_ITEM_SELECTED")
INSPECTOR_PREVIEW_CHANGED_ID = 427273738  # hash("INSPECTOR_PREVIEW_CHANGED")
INSPECTOR_ITEM_EXPANDED_ID = 427273739
KIT_GREEN = 0xFF888888


class InspectorWidget:
    def destroy(self):
        self._tree_view.destroy()
        self._tree_view = None

        self._render_view.set_widget(None)
        self._render_view._window = None
        self._render_view = None

        self._property_view.set_widget(None)
        self._property_view = None

    def __init__(self, window: ui.Window, **kwargs):

        self._test_menu = None
        self._window = window
        self._render_view = None
        self._main_stack = ui.ZStack(style=INSPECTOR_STYLE)
        self._build_ui()

    def _build_ui(self):
        with self._main_stack:
            ui.Rectangle(name="background")
            with ui.VStack():
                with ui.HStack(height=30):
                    bt = ui.Button("TestMenu", width=0)

                    def show_test_menu():
                        self._test_menu = ui.Menu("Test")
                        with self._test_menu:
                            ui.MenuItem("ToolButton1", triggered_fn=test_widget_tool_button)
                            ui.MenuItem("Select Frame", triggered_fn=test_select_frame)
                            ui.MenuItem("Change Query", triggered_fn=test_change_query)
                            ui.MenuItem("Test Menu", triggered_fn=test_menu_activation)

                        self._test_menu.show_at(bt.screen_position_x, bt.screen_position_y + bt.computed_height)

                    bt.set_clicked_fn(show_test_menu)

                    ui.Spacer()
                    # self._tree_vis_model = ui.ToolButton(text="Tree", width=100).model
                    self._preview_vis_model = ui.ToolButton(text="Preview", name="toolbutton", width=100).model
                    self._property_vis_model = ui.ToolButton(text="Property", name="toolbutton", width=100).model
                    self._style_vis_model = ui.ToolButton(text="Style", name="toolbutton", width=100).model
                    ui.Spacer()

                ui.Line(height=3, name="dark_separator")

                with ui.HStack():

                    # Build adjustable Inspector Tree Widget with "Splitter"
                    with ui.ZStack(width=0):
                        handle_width = 3
                        # the placer will increate the stack width
                        with ui.Placer(offset_x=300, draggable=True, drag_axis=ui.Axis.X):
                            ui.Rectangle(width=handle_width, name="splitter")

                        # the tree view will fit the all ZStack Width minus the Handle Width
                        with ui.HStack(name="tree", style={"HStack::tree": {"margin_width": 5}}):
                            # Extensions List Widget (on the left)
                            self._tree_view = InspectorTreeView(self._window, self.update_window)
                            ui.Spacer(width=handle_width)

                    # Build Inspector Preview
                    self._render_view = InspectorPreview(self._window)

                    ui.Line(alignment=ui.Alignment.H_CENTER, width=2, style={"color": 0xFF222222, "border_width": 2})

                    # Build Inspector Property Window
                    self._property_view = InspectorPropertyWidget()

                    ui.Line(alignment=ui.Alignment.H_CENTER, width=2, style={"color": 0xFF222222, "border_width": 2})

                    self._style_view = StyleTreeView()

                ui.Line(height=3, style={"color": 0xFF222222, "border_width": 3})

                with ui.ZStack(height=30):
                    ui.Rectangle(name="background")
                    with ui.VStack():
                        ui.Spacer()
                        with ui.HStack(height=0):
                            ui.Spacer(width=10)
                            ui.Label("Query : ", width=0)

                            model = ui.StringField(width=400).model
                            model.set_value("DemoWindow//Frame/VStack[1]/HStack[1]/Button")

                            ui.Spacer(width=10)
                            result = ui.StringField(width=200, read_only=True).model

                            def execute_query(model: ui.AbstractValueModel):
                                query = model.as_string
                                children = OmniUIQuery.find_widget(query)
                                if children:
                                    result.set_value(children.__class__.__name__)
                                else:
                                    result.set_value("NO RESULT")

                                if type(children) == ui.Button:
                                    print("Has Clicked Fn", children.has_clicked_fn)
                                    children.call_clicked_fn()

                            model.add_end_edit_fn(execute_query)
                        ui.Spacer()

        def visiblity_changed_fn(w):
            w.toggle_visibility()

        self._preview_vis_model.set_value(True)
        self._property_vis_model.set_value(True)
        self._style_vis_model.set_value(True)

        # self._tree_vis_model.add_value_changed_fn(lambda m, w=self._tree_view: visiblity_changed_fn(w))
        self._preview_vis_model.add_value_changed_fn(lambda m, w=self._render_view: visiblity_changed_fn(w))
        self._property_vis_model.add_value_changed_fn(lambda m, w=self._property_view: visiblity_changed_fn(w))
        self._style_vis_model.add_value_changed_fn(lambda m, w=self._style_view: visiblity_changed_fn(w))

        self._preview_vis_model.set_value(False)
        self._property_vis_model.set_value(False)
        self._style_vis_model.set_value(False)

    def update_window(self, window: ui.Window):
        self._window = window
        if self._render_view:
            self._render_view.update_window(window)
