# Copyright (c) 2021, 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

from typing import Callable, List
from .style import get_style

class OptionBox:
    def __init__(self, build_fn: Callable, **kwargs):
        """
        Option box.

        """
        self._frame = None
        self._build_fn = build_fn
        self._selection_changed_fn = kwargs.get("selection_changed_fn", None)
        self._mouse_pressed_fn = kwargs.get("mouse_pressed_fn", None)
        self._mouse_double_clicked_fn = kwargs.get("mouse_double_clicked_fn", None)
        self._build_ui()

    def _build_ui(self):
        self._frame = ui.Frame(visible=True, height=100, style=get_style())

    def rebuild(self, selected: List[str]):
        if self._frame:
            self._frame.clear()
            with self._frame:
                self._build_fn(selected)

    def destroy(self):
        self._build_fn = None
        self._selection_changed_fn = None
        self._mouse_pressed_fn = None
        self._mouse_double_clicked_fn = None
        self._frame = None

    @staticmethod
    def create_option_box(build_fn: Callable) -> 'OptionBox':
        widget = OptionBox(build_fn)
        return widget

    @staticmethod
    def on_selection_changed(widget: 'OptionBox', selected: List[str]):
        if widget:
            widget.rebuild(selected)

    @staticmethod
    def delete_option_box(widget: 'OptionBox'):
        if widget:
            widget.destroy()

