# 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 carb
import omni.ext
import omni.kit.app
import omni.kit.window.toolbar
import omni.ui
import os
from carb.input import KeyboardInput as Key
from omni.kit.widget.toolbar import SimpleToolButton, WidgetGroup


class ExampleSimpleToolButton(SimpleToolButton):
    """
    Example of how to use SimpleToolButton
    """

    def __init__(self, icon_path):
        super().__init__(
            name="example_simple",
            tooltip="Example Simple ToolButton",
            icon_path=f"{icon_path}/plus.svg",
            icon_checked_path=f"{icon_path}/plus.svg",
            hotkey=Key.L,
            toggled_fn=lambda c: carb.log_warn(f"Example button toggled {c}"),
        )


class ExampleToolButtonGroup(WidgetGroup):
    """
    Example of how to create two ToolButton in one WidgetGroup
    """

    def __init__(self, icon_path):
        super().__init__()
        self._icon_path = icon_path

    def clean(self):
        super().clean()

    def get_style(self):
        style = {
            "Button.Image::example1": {"image_url": f"{self._icon_path}/plus.svg"},
            "Button.Image::example1:checked": {"image_url": f"{self._icon_path}/minus.svg"},
            "Button.Image::example2": {"image_url": f"{self._icon_path}/minus.svg"},
            "Button.Image::example2:checked": {"image_url": f"{self._icon_path}/plus.svg"},
        }
        return style

    def create(self, default_size):
        def on_clicked():
            # example of getting a button by name from toolbar
            toolbar = omni.kit.window.toolbar.get_instance()
            button = toolbar.get_widget("scale_op")
            if button is not None:
                button.enabled = not button.enabled

        button1 = omni.ui.ToolButton(
            name="example1",
            tooltip="Example Button 1",
            width=default_size,
            height=default_size,
            mouse_pressed_fn=lambda x, y, b, _: on_clicked(),
        )

        button2 = omni.ui.ToolButton(
            name="example2", tooltip="Example Button 2", width=default_size, height=default_size
        )

        # return a dictionary of name -> widget if you want to expose it to other widget_group
        return {"example1": button1, "example2": button2}


class ToolbarButtonExample(omni.ext.IExt):
    def on_startup(self, ext_id):
        ext_path = omni.kit.app.get_app().get_extension_manager().get_extension_path(ext_id)
        icon_path = os.path.join(ext_path, "icons")

        self._toolbar = omni.kit.window.toolbar.get_instance()
        self._widget_simple = ExampleSimpleToolButton(icon_path)
        self._widget = ExampleToolButtonGroup(icon_path)

        self._toolbar.add_widget(self._widget, -100)
        self._toolbar.add_widget(self._widget_simple, -200)

    def on_shutdown(self):
        self._toolbar.remove_widget(self._widget)
        self._toolbar.remove_widget(self._widget_simple)
        self._widget.clean()
        self._widget = None
        self._widget_simple.clean()
        self._widget_simple = None
        self._toolbar = None
