# Copyright (c) 2023, 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.
#
__all__ = ["start_render_loading_ui"]


def start_render_loading_ui(ext_manager, renderer: str):
    import carb
    import time

    time_begin = time.time()
    carb.settings.get_settings().set("/exts/omni.kit.viewport.ready/startup/enabled", False)
    ext_manager.set_extension_enabled_immediate("omni.kit.viewport.ready", True)

    from omni.kit.viewport.ready.viewport_ready import ViewportReady, ViewportReadyDelegate
    class TimerDelegate(ViewportReadyDelegate):
        def __init__(self, time_begin):
            super().__init__()
            self.__timer_start = time.time()
            self.__vp_ready_cost = self.__timer_start - time_begin

        @property
        def font_size(self) -> float:
            return 48

        @property
        def message(self) -> str:
            rtx_mode = {
                "RaytracedLighting": "Real-Time",
                "PathTracing": "Interactive (Path Tracing)",
                "LightspeedAperture": "Aperture (Game Path Tracer)" 
            }.get(carb.settings.get_settings().get("/rtx/rendermode"), "Real-Time")

            renderer_label = {
                "rtx": f"RTX - {rtx_mode}",
                "iray": "RTX - Accurate (Iray)",
                "pxr": "Pixar Storm",
                "index": "RTX - Scientific (IndeX)"
            }.get(renderer, renderer)

            return f"Waiting for {renderer_label} to start"

        def on_complete(self):
            rtx_load_time = time.time() - self.__timer_start
            super().on_complete()
            print(f"Time until pixel: {rtx_load_time}")
            print(f"ViewportReady cost: {self.__vp_ready_cost}")

    return ViewportReady(TimerDelegate(time_begin))
