"""
* Copyright (c) 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.
"""
import omni.ui as ui
import os


ICONS_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "icons")
DICT_ICONS = {
    "forbidden": os.path.join(ICONS_DIR, "forbidden.svg"),
    "forbidden_black": os.path.join(ICONS_DIR, "forbidden_black.svg"),
    "star": os.path.join(ICONS_DIR, "star.svg"),
    "star_black": os.path.join(ICONS_DIR, "star_black.svg"),
    "plus": os.path.join(ICONS_DIR, "{style}", "Plus.svg"),
    "minus": os.path.join(ICONS_DIR, "{style}", "Minus.svg"),
}


class FastSearchDelegate(ui.AbstractItemDelegate):
    """Delegate of the Mapper Batcher"""

    def __init__(self):
        super(FastSearchDelegate, self).__init__()
        self._highlighting_enabled = None
        # Text that is highlighted in flat mode
        self._highlighting_text = None
        self.show_column_visibility = True
        self._style = None
        self.init_editor_style()

    def init_editor_style(self):
        import carb.settings

        self._style = carb.settings.get_settings().get_as_string("/persistent/app/window/uiStyle") or "NvidiaDark"

    def build_branch(self, model, item, column_id, level, expanded):
        """Create a branch widget that opens or closes subtree"""
        if column_id == 0:
            with ui.HStack(width=16 * (level + 2), height=0):
                ui.Spacer()
                if model.can_item_have_children(item):
                    # Draw the +/- icon
                    image_name = "minus" if expanded else "plus"
                    ui.Image(
                        DICT_ICONS[image_name].format(style=self._style),
                        width=10,
                        height=10,
                        style_type_name_override="TreeView.Item",
                    )
                    ui.Spacer(width=4)

    def build_widget(self, model, item, column_id, level, expanded):
        """Create a widget per item"""
        # Disable the item if it's an Instance Proxy because Kit can't interact with such object and crashes.
        enabled = True

        if column_id == 0:
            name = item.name.get_value_as_string()
            star_path = self.get_fav_icon(item)
            forbidden_path = self.get_block_icon(item)

            with ui.HStack(height=20):
                label = ui.Label(name)
                label.set_mouse_released_fn(lambda x, y, b, m, i=item: self._on_item_clicked(i, b))
                ui.Spacer()
                fav_icon = ui.Image(star_path, width=15, height=15)
                fav_icon.set_mouse_released_fn(lambda x, y, b, m, i=item, f=fav_icon: self._on_fav_clicked(i, f, b))
                ui.Spacer(width=10)
                blocklist_icon = ui.Image(forbidden_path, width=15, height=15)
                blocklist_icon.set_mouse_released_fn(lambda x, y, b, m, i=item, f=blocklist_icon: self._on_block_clicked(i, f, b))
                ui.Spacer(width=10)

    @staticmethod
    def get_fav_icon(item):
        """Get favorite icon path"""
        return DICT_ICONS["star_black"] if not item.favorite else DICT_ICONS["star"]

    @staticmethod
    def get_block_icon(item):
        """Get blocked icon path"""
        return DICT_ICONS["forbidden_black"] if not item.blocklist else DICT_ICONS["forbidden"]

    def _on_item_clicked(self, item, button):
        if button != 0:
            return
        item.item_mouse_released()

    def _on_fav_clicked(self, item, image_widget, button):
        """Called when favorite icon is clicked"""
        if button != 0:
            return
        item.favorite = not item.favorite
        image_widget.source_url = self.get_fav_icon(item)

    def _on_block_clicked(self, item, image_widget, button):
        """Called when blocked icon is clicked"""
        if button != 0:
            return
        item.blocklist = not item.blocklist
        image_widget.source_url = self.get_block_icon(item)

    def build_header(self, column_id):
        """Build the header"""
        style_type_name = "TreeView.Header"
        with ui.HStack():
            ui.Label("Node name", style_type_name_override=style_type_name)
