
__all__ = ["InspectorPropertyWidget"]

import omni.ui as ui
import carb.events
import omni.kit.app

from omni.ui_query import OmniUIQuery

from .widgets.button import ButtonPropertyFrame
from .widgets.canvas_frame import CanvasPropertyFrame
from .widgets.circle import CirclePropertyFrame
from .widgets.collapsable_frame import CollapsableFramePropertyFrame
from .widgets.combox_box import ComboBoxPropertyFrame
from .widgets.frame import FramePropertyFrame
from .widgets.grid import GridPropertyFrame
from .widgets.image import ImagePropertyFrame
from .widgets.image_with_provider import ImageWithProviderPropertyFrame
from .widgets.label import LabelPropertyFrame
from .widgets.line import LinePropertyFrame
from .widgets.placer import PlacerPropertyFrame
from .widgets.scrolling_frame import ScrollingFramePropertyFrame
from .widgets.slider import SliderPropertyFrame
from .widgets.stack import StackPropertyFrame
from .widgets.string_field import StringFieldPropertyFrame
from .widgets.tree_view import TreeViewPropertyFrame
from .widgets.triangle import TrianglePropertyFrame
from .widgets.widget import WidgetPropertyFrame


class InspectorPropertyWidget:
    """The Stage widget"""

    def __init__(self, **kwargs):

        self._widget = None
        self._main_stack = ui.VStack(
            spacing=5,
            width=300,
            style={
                "VStack": {"margin_width": 10},
                "CollapsableFrame": {"background_color": 0xFF2A2A2A, "secondary_color": 0xFF2A2A2A},
            },
        )
        self._build_ui()

        app = omni.kit.app.get_app()
        self._update_sub = app.get_message_bus_event_stream().create_subscription_to_pop(
            self.on_selection_changed, name="Inspector Selection Changed"
        )

    def on_selection_changed(self, event: carb.events.IEvent):
        from ..inspector_widget import INSPECTOR_ITEM_SELECTED_ID

        if event.type == INSPECTOR_ITEM_SELECTED_ID:
            selection = OmniUIQuery.find_widget(event.payload["item_path"])
            if selection:
                self.set_widget(selection)

    def toggle_visibility(self):
        self._main_stack.visible = not self._main_stack.visible

    def set_widget(self, widget: ui.Widget):
        self._widget = widget

        self._build_ui()

    def _build_ui(self):
        self._main_stack.clear()

        with self._main_stack:
            ui.Spacer(height=0)
            ui.Label("Properties", height=0, style={"font_size": 16}, alignment=ui.Alignment.CENTER)
            ui.Spacer(height=0)

            if not self._widget:
                return

            if isinstance(self._widget, ui.Label):
                LabelPropertyFrame("Label", self._widget)
            elif isinstance(self._widget, ui.Stack):
                StackPropertyFrame("Stack", self._widget)
            elif isinstance(self._widget, ui.Button):
                ButtonPropertyFrame("Button", self._widget)
            elif isinstance(self._widget, ui.Image):
                ImagePropertyFrame("Image", self._widget)
            elif isinstance(self._widget, ui.CanvasFrame):
                CanvasPropertyFrame("CanvasFrame", self._widget)
            elif isinstance(self._widget, ui.AbstractSlider):
                SliderPropertyFrame("Slider", self._widget)
            elif isinstance(self._widget, ui.Circle):
                CirclePropertyFrame("Circle", self._widget)
            elif isinstance(self._widget, ui.ComboBox):
                ComboBoxPropertyFrame("ComboBox", self._widget)
            elif isinstance(self._widget, ui.ScrollingFrame):
                ScrollingFramePropertyFrame("ScrollingFrame", self._widget)
                FramePropertyFrame("Frame", self._widget)
            elif isinstance(self._widget, ui.CollapsableFrame):
                CollapsableFramePropertyFrame("CollapsableFrame", self._widget)
                FramePropertyFrame("Frame", self._widget)
            elif isinstance(self._widget, ui.Frame):
                FramePropertyFrame("Frame", self._widget)
            elif isinstance(self._widget, ui.Grid):
                GridPropertyFrame("Grid", self._widget)
            elif isinstance(self._widget, ui.ImageWithProvider):
                ImageWithProviderPropertyFrame("ImageWithProvider", self._widget)
            elif isinstance(self._widget, ui.Line):
                LinePropertyFrame("Line", self._widget)
            elif isinstance(self._widget, ui.Placer):
                PlacerPropertyFrame("Placer", self._widget)
            elif isinstance(self._widget, ui.ScrollingFrame):
                ScrollingFramePropertyFrame("ScrollingFrame", self._widget)
            elif isinstance(self._widget, ui.StringField):
                StringFieldPropertyFrame("StringField", self._widget)
            elif isinstance(self._widget, ui.TreeView):
                TreeViewPropertyFrame("TreeView", self._widget)
            elif isinstance(self._widget, ui.Triangle):
                TrianglePropertyFrame("Triangle", self._widget)

            WidgetPropertyFrame("Widget", self._widget)
