
__all__ = ["ResolvedStyleWidget"]

from typing import Union
import omni.ui as ui

from .widget_styles import ClassStyles

from .style_model import get_property_type, StylePropertyType, StyleColorModel, StyleComboItemModel, get_enum_class


class ResolvedStyleWidget:
    """The Stage widget"""

    def __init__(self, **kwargs):

        self._widget = None
        # self._frame_widget = ui.CollapsableFrame("Resolved Styles", height=0, build_fn=self._build_ui)
        # with self._frame_widget:
        self._stack = ui.VStack(height=0, spacing=5)

        self._class_name_mapping = {
            "CollapsableFrame": "Frame",
            "VStack": "Stack",
            "HStack": "Stack",
            "ZStack": "Stack",
            "IntSlider": "AbstractSlider",
            "FloatSlider": "AbstractSlider",
            "IntDrag": "AbstractSlider",
            "FloatDrag": "AbstractSlider",
        }

    def set_widget(self, widget: ui.Widget):
        self._widget = widget
        self._widget_type = self._widget.__class__.__name__
        self._widget_type = self._class_name_mapping.get(self._widget_type, self._widget_type)
        self._widget_styles = ClassStyles.get(self._widget_type, [])
        self._build_ui()

    def _build_ui(self):
        self._stack.clear()
        if not self._widget:
            return

        with self._stack:
            ui.Label("Resolved Styles", height=0)
            ui.Spacer(height=5)
            for name in self._widget_styles:
                with ui.HStack(height=0):
                    ui.Spacer(width=10)
                    ui.Label(name, width=150)
                    prop_type = get_property_type(name)
                    if prop_type == StylePropertyType.COLOR:
                        print(ui.Inspector.get_resolved_style(self._widget))
                        value = False
                        # value: Union[int, bool] = ui.Inspector.get_resolved_style(self._widget.get_resolved_style_value(name)
                        if value:
                            model = StyleColorModel(value)
                            color_widget = ui.ColorWidget(enabled=False)
                            color_widget.model = model
                        else:
                            ui.Label("Default")

                    elif prop_type == StylePropertyType.FLOAT:
                        print(ui.Inspector.get_resolved_style(self._widget))
                        value = False
                        # value: Union[float, bool] = self._widget.get_resolved_style_value(name)
                        if value:
                            field = ui.FloatField().model
                            field.set_value(value)
                        else:
                            ui.Label("Default")

                    elif prop_type == StylePropertyType.STRING:
                        print(ui.Inspector.get_resolved_style(self._widget))
                        value = False
                        # value: Union[str, bool] = self._widget.get_resolved_style_value(name)
                        if value:
                            ui.StringField().model.set_value(value)
                        else:
                            ui.Label("Default")
                    elif prop_type == StylePropertyType.ENUM:
                        print(ui.Inspector.get_resolved_style(self._widget))
                        value = False
                        # value: Union[int, bool] = self._widget.get_resolved_style_value(name)
                        if value:
                            enum_cls = get_enum_class(name)
                            self._model = StyleComboItemModel(enum_cls, value)
                            ui.ComboBox(self._model)
                        else:
                            ui.Label("Default")

                    else:
                        ui.Label("MISSING ... TYPE")
