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

from omni import ui
from omni.ui import color as cl
from .doc_page import DocPage
import re

SPACING = 5


class SliderDoc(DocPage):
    """ document for Slider"""

    def create_doc(self, navigate_to=None):
        self._section_title("Drag & Slider")
        self._text(
            "There are 2 main kind of Sliders, <Type>Slider and <Type>Drag. The Sliders are more like traditionalal "
            "slider that can be drag and snap where you click, the value can be shown on the slider or not but can not "
            "be edited directly by double clicking"
        )
        self._text(
            "The Drags are more like Field in the way that they behave like a Field, you can double clic to edit the "
            "value but they also have a mean to be 'Dragged' to increase / decrease the value"
        )
        self._text(
            "They currently support minimal styling, simple background color (background_color) and text color (color)."
        )

        self._caption("FloatSlider", navigate_to)

        def build_slider_table(label, slider_command):
            def mystrip(line: str, number: int) -> str:
                """Remove `number` spaces from the begin of the line"""
                n = 0
                l = len(line)
                while n < number and n < l and line[n] == " ":
                    n += 1

                return line[n:]

            # Remove whitespaces from the command but keep the newline symbols
            code = "\n".join(mystrip(line, 16) for line in slider_command.split("\n") if line)
            with ui.HStack():
                with ui.ZStack(width=130):
                    ui.Rectangle(name="table")
                    ui.Label(label, name="text", alignment=ui.Alignment.RIGHT, style={"margin": SPACING})
                with ui.ZStack():
                    ui.Rectangle(name="table")
                    with ui.HStack(name="margin", style={"HStack::margin": {"margin": SPACING / 2}}, spacing=SPACING):
                        exec(code)
                with ui.ZStack(width=ui.Fraction(2)):
                    ui.Rectangle(name="table")
                    with ui.ZStack(name="margin", style={"ZStack::margin": {"margin": SPACING / 2}}):
                        ui.Rectangle(name="code")
                        ui.Label(code, name="code")

        with ui.VStack():
            build_slider_table(
                "Default",
                """
                ui.FloatSlider()
                """,
            )
            build_slider_table(
                "Min/Max",
                """
                ui.FloatSlider(min=0, max=10)
                """,
            )
            build_slider_table(
                "Hard Min/Max",
                """
                model = ui.SimpleFloatModel(1.0, min=0, max=100)
                ui.FloatSlider(model, min=0, max=10)
                """,
            )
            build_slider_table(
                "With Style",
                """
                ui.FloatSlider(
                    min=-180,
                    max=180,
                    style={
                        "draw_mode": ui.SliderDrawMode.HANDLE,
                        "background_color": cl("#BBBBBB"),
                        "color": cl.black
                    }
                )
                """,
            )
            build_slider_table(
                "Transparent bg",
                """
                ui.FloatSlider(
                    min=-180,
                    max=180,
                    style={
                        "draw_mode": ui.SliderDrawMode.HANDLE,
                        "background_color": cl.transparent,
                        "color": cl.black,
                        "font_size":20.0
                    }
                )
                """,
            )

            build_slider_table(
                "different slider color",
                """
                ui.FloatSlider(
                    min=0,
                    max=1,
                    style={
                        "draw_mode": ui.SliderDrawMode.HANDLE,
                        "background_color": cl.transparent,
                        "color": cl.black,
                        "font_size":20.0,
                        "secondary_color": cl.red,
                        "secondary_selected_color": cl("#FF00FF")
                    }
                )
                """,
            )

            build_slider_table(
                "Field & Slider",
                """
                field = ui.FloatField(height=15, width=50)
                ui.FloatSlider(
                    min=0,
                    max=20,
                    step=.1,
                    model=field.model,
                    style={"color":cl.transparent}
                )
                # default value
                field.model.set_value(12.0)
                """,
            )

            build_slider_table(
                "Filled Mode Slider",
                """
                ui.FloatSlider(
                    height=20,
                    style={"background_color": cl("#DDDDDD"),
                           "secondary_color": cl("#AAAAAA"),
                           "color": cl("#111111")}
                    ).model.set_value(0.5)
                """,
            )

            build_slider_table(
                "Rounded Slider",
                """
                ui.FloatSlider(
                    height=20,
                    style={"border_radius": 20,
                           "background_color": cl.black,
                           "secondary_color": cl("#DDDDDD"),
                           "color": cl("#AAAAAA")}
                    ).model.set_value(0.5)
                """,
            )

            build_slider_table(
                "Slider Step",
                """
                slider = ui.FloatSlider(
                    step=0.25,
                    height=0,
                    style={"border_radius": 20,
                           "background_color": cl.black,
                           "secondary_color": cl("#DDDDDD"),
                           "color": cl("#AAAAAA")})
                slider.model.set_value(0.5)
                """,
            )

            build_slider_table(
                "Handle Step",
                """
                slider = ui.FloatSlider(
                    step=0.2,
                    height=0,
                    style={"draw_mode": ui.SliderDrawMode.HANDLE})
                slider.model.set_value(0.4)
                """,
            )

        self._caption("IntSlider", navigate_to)

        with ui.VStack():
            build_slider_table(
                "Default",
                """
                ui.IntSlider()
                """,
            )
            build_slider_table(
                "Min/Max",
                """
                ui.IntSlider(min=0, max=20)
                """,
            )
            build_slider_table(
                "With Style",
                """
                ui.IntSlider(
                    min=0,
                    max=20,
                    style={
                        "draw_mode": ui.SliderDrawMode.HANDLE,
                        "background_color": cl("#BBFFBB"),
                        "color": cl("#FF00FF"),
                        "font_size": 14.0
                    }
                )
                """,
            )

        self._caption("FloatDrag", navigate_to)

        with ui.VStack():
            build_slider_table("Default", "ui.FloatDrag()")
            build_slider_table("Min/Max", "ui.FloatDrag(min=-10, max=10, step=0.01)")

        self._caption("IntDrag", navigate_to)

        with ui.VStack():
            build_slider_table("Default", "ui.IntDrag()")
            build_slider_table("Min/Max", "ui.IntDrag(min=0, max=50)")

        ui.Spacer(height=10)
