# Copyright (c) 2018-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.
#
"""The documentation for CheckBox"""

from omni import ui
from .doc_page import DocPage

SPACING = 5


class CheckBoxDoc(DocPage):
    """ document for CheckBox"""

    def create_doc(self, navigate_to=None):
        self._section_title("CheckBox")

        self._text(
            "A CheckBox is an option button that can be switched on (checked) or off (unchecked). Checkboxes are "
            "typically used to represent features in an application that can be enabled or disabled without affecting "
            "others."
        )

        self._text(
            "The checkbox is implemented using the model-delegate-view pattern. The model is the central component of this "
            "system. It is the application's dynamic data structure independent of the widget. It directly manages the "
            "data, logic, and rules of the checkbox. If the model is not specified, the simple one is created "
            "automatically when the object is constructed."
        )

        self._text(
            "In the following example, the models of two checkboxes are connected, and if one checkbox is changed, it "
            "makes another checkbox be changed as well."
        )

        with ui.HStack(width=0, spacing=SPACING):
            # Create two checkboxes
            first = ui.CheckBox()
            second = ui.CheckBox()

            # Connect one to another
            first.model.add_value_changed_fn(lambda a, b=second: b.model.set_value(not a.get_value_as_bool()))
            second.model.add_value_changed_fn(lambda a, b=first: b.model.set_value(not a.get_value_as_bool()))

            # Set the first one to True
            first.model.set_value(True)

            self._text("One of two")

        self._code(
            """
            # Create two checkboxes
            first = ui.CheckBox()
            second = ui.CheckBox()

            # Connect one to another
            first.model.add_value_changed_fn(
                lambda a, b=second: b.model.set_value(not a.get_value_as_bool()))
            second.model.add_value_changed_fn(
                lambda a, b=first: b.model.set_value(not a.get_value_as_bool()))
            """
        )

        self._text("In the following example, that is a bit more complicated, only one checkbox can be enabled.")

        with ui.HStack(width=0, spacing=SPACING):
            # Create two checkboxes
            first = ui.CheckBox()
            second = ui.CheckBox()
            third = ui.CheckBox()

            def like_radio(model, first, second):
                """Turn on the model and turn off two checkboxes"""
                if model.get_value_as_bool():
                    model.set_value(True)
                    first.model.set_value(False)
                    second.model.set_value(False)

            # Connect one to another
            first.model.add_value_changed_fn(lambda a, b=second, c=third: like_radio(a, b, c))
            second.model.add_value_changed_fn(lambda a, b=first, c=third: like_radio(a, b, c))
            third.model.add_value_changed_fn(lambda a, b=first, c=second: like_radio(a, b, c))

            # Set the first one to True
            first.model.set_value(True)

            self._text("Almost like radio box")

        self._code(
            """
            # Create two checkboxes
            first = ui.CheckBox()
            second = ui.CheckBox()
            third = ui.CheckBox()

            def like_radio(model, first, second):
                '''Turn on the model and turn off two checkboxes'''
                if model.get_value_as_bool():
                    model.set_value(True)
                    first.model.set_value(False)
                    second.model.set_value(False)

            # Connect one to another
            first.model.add_value_changed_fn(
                lambda a, b=second, c=third: like_radio(a, b, c))
            second.model.add_value_changed_fn(
                lambda a, b=first, c=third: like_radio(a, b, c))
            third.model.add_value_changed_fn(
                lambda a, b=first, c=second: like_radio(a, b, c))
            """
        )

        with ui.HStack(width=0, spacing=SPACING):
            ui.CheckBox(enabled=False).model.set_value(True)
            ui.CheckBox(enabled=False)
            self._text("Disabled")

        self._code(
            """
            ui.CheckBox(enabled=False).model.set_value(True)
            ui.CheckBox(enabled=False)
            """
        )

        ui.Spacer(height=10)
