# 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 Images"""

from omni import ui
from .doc_page import DocPage
import inspect

SPACING = 5


def image_hot_switch():
    styles = [
        {
            "": {"image_url": "resources/icons/Nav_Walkmode.png"},
            ":hovered": {"image_url": "resources/icons/Nav_Flymode.png"},
        },
        {
            "": {"image_url": "resources/icons/Move_local_64.png"},
            ":hovered": {"image_url": "resources/icons/Move_64.png"},
        },
        {
            "": {"image_url": "resources/icons/Rotate_local_64.png"},
            ":hovered": {"image_url": "resources/icons/Rotate_global.png"},
        },
    ]

    def set_image(model, image):
        value = model.get_item_value_model().get_value_as_int()
        image.set_style(styles[value])

    image = ui.Image(width=64, height=64, style=styles[0])

    with ui.HStack(width=ui.Percent(50)):
        ui.Label("Select a texture to display", name="text")
        model = ui.ComboBox(0, "Navigation", "Move", "Rotate").model
        model.add_item_changed_fn(lambda m, i, im=image: set_image(m, im))


class ImageDoc(DocPage):
    """ document for Image"""

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

        self._text("The Image type displays an image.")
        self._text("The source of the image is specified as a URL using the source property.")
        self._text(
            "By default, specifying the width and height of the item causes the image to be scaled to that size. This "
            "behavior can be changed by setting the fill_mode property, allowing the image to be stretched or scaled "
            "instead. The property alignment controls where to align the scaled image."
        )
        code_width = 320
        image_source = "resources/desktop-icons/omniverse_512.png"
        with ui.VStack():
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.STRETCH)""",
                "(Default) The image is scaled to fit, the alignment is ignored",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_FIT,
                    alignment=ui.Alignment.LEFT_CENTER)""",
                "The image is scaled uniformly to fit without cropping and aligned to the left",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_FIT,
                    alignment=ui.Alignment.CENTER)""",
                "The image is scaled uniformly to fit without cropping and aligned to the center",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_FIT,
                    alignment=ui.Alignment.RIGHT_CENTER)""",
                "The image is scaled uniformly to fit without croppingt and aligned to right",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_CROP,
                    alignment=ui.Alignment.CENTER_TOP)""",
                "The image is scaled uniformly to fill, cropping if necessary and aligned to the top",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_CROP,
                    alignment=ui.Alignment.CENTER)""",
                "The image is scaled uniformly to fill, cropping if necessary and centered",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_CROP,
                    alignment=ui.Alignment.CENTER_BOTTOM)""",
                "The image is scaled uniformly to fill, cropping if necessary and aligned to the bottom",
                code_width=code_width,
            )
            self._image_table(
                f"""ui.Image(
                    '{image_source}',
                    fill_policy=ui.FillPolicy.PRESERVE_ASPECT_FIT,
                    alignment=ui.Alignment.CENTER,
                    style={{'border_radius': 10}})""",
                "The image has rounded corners",
                code_width=code_width,
            )

            ui.Spacer(height=20)
            ui.Line()
            ui.Spacer(height=10)
            self._text("The Image can use style image_url")
            ui.Spacer(height=10)
            ui.Line()
            ui.Spacer(height=20)

            code_width = 470
            self._image_table(
                f"""ui.Image(style={{'image_url': 'resources/desktop-icons/omniverse_128.png'}})""",
                "The image has a styled URL",
                code_width=code_width,
            )

            self._image_table(
                f"""with ui.HStack(spacing =5,
                        style={{"Image":{{'image_url': 'resources/desktop-icons/omniverse_128.png'}}}}):
                        ui.Image()
                        ui.Image()
                        ui.Image()
                    """,
                "The image has a styled URL",
                code_width=code_width,
            )

            self._image_table(
                f"""ui.Image(style={{
                    'image_url': 'resources/desktop-icons/omniverse_128.png',
                    'alignment': ui.Alignment.RIGHT_CENTER}})""",
                "The image has styled Alignment",
                code_width=code_width,
            )

            self._image_table(
                f"""ui.Image(style={{
                    'image_url': 'resources/desktop-icons/omniverse_256.png',
                    'fill_policy': ui.FillPolicy.PRESERVE_ASPECT_CROP,
                    'alignment': ui.Alignment.RIGHT_CENTER}})""",
                "The image has styled fill_policy",
                code_width=code_width,
            )

        self._text(
            "It's possible to set a different image per style state. And switch them depending on the mouse hovering, "
            "selection state, etc."
        )

        image_hot_switch()
        self._code(inspect.getsource(image_hot_switch).split("\n", 1)[-1])

        ui.Spacer(height=10)
