# 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 carb.settings
import omni.graph.core as og
import omni.ui as ui
from omni.kit.widget.settings.deprecated import SettingType
from omni.kit.window.preferences import PERSISTENT_SETTINGS_PREFIX, PreferenceBuilder

SETTING_PAGE_NAME = "Visual Scripting"


class OmniGraphSettingsEditor(PreferenceBuilder):
    def __init__(self):
        super().__init__(SETTING_PAGE_NAME)
        self._settings = carb.settings.get_settings()

    def destroy(self):
        self._settings = None

    def build(self):
        """Updates"""
        with ui.VStack(height=0):
            with self.add_frame("Update Settings"):
                with ui.VStack():
                    self.create_setting_widget(
                        "Update to USD",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/updateToUsd",
                        SettingType.BOOL,
                        tooltip="When this setting is enabled, copies all OmniGraph node attribute values to USD"
                        " every tick",
                    )
                    self.create_setting_widget(
                        "Update mesh points to Hydra",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/updateMeshPointsToHydra",
                        SettingType.BOOL,
                        tooltip="When this setting is enabled, mesh points will be not be written back to USD",
                    )
                    self.create_setting_widget(
                        "Use Realm scheduler",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/realmEnabled",
                        SettingType.BOOL,
                        tooltip="When this setting is enabled, use the Realm scheduler instead of the static scheduler"
                        " with Push evaluator",
                    )
                    self.create_setting_widget(
                        "Default graph evaluator",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/defaultEvaluator",
                        SettingType.STRING,
                        tooltip="The evaluator to use for new Graphs when not specified",
                    )
                    self.create_setting_widget(
                        "Use legacy simulation pipeline",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/useLegacySimulationPipeline",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled , all graphs are run from the simulation"
                        " pipeline stage",
                    )
                    self.create_setting_widget(
                        "Disable automatic population of prim nodes",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/disablePrimNodes",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is disabled, prim nodes are populated into Fabric"
                        " for support of direct prim-to-node connections",
                    )
                    self.create_setting_widget(
                        "Create OmniGraph prims using the schema",
                        og.USE_SCHEMA_PRIMS_SETTING,
                        SettingType.BOOL,
                        tooltip="When this setting is enabled, creates OmniGraph and OmniGraph node prims using the"
                        " schema. Disable the three settings below and enable the one above before enabling"
                        " this.",
                    )
                    self.create_setting_widget(
                        "Allow global implicit graph",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/allowGlobalImplicitGraph",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled, allows the global implicit graph"
                        " to exist",
                    )
                    self.create_setting_widget(
                        "Enable direct connection to prim nodes",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/enableLegacyPrimConnections",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled, direct connections from OG nodes"
                        " to Prim nodes are allowed",
                    )
                    self.create_setting_widget(
                        "Enable deprecated OmniGraph prim drop menu",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/createPrimNodes",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled, adds a menu item on the"
                        " Drop-menu for Prim nodes and allows creation of the omni.graph.core.Prim node type",
                    )
                    self.create_setting_widget(
                        "Enable deprecated Node.pathChanged callback",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/enablePathChangedCallback",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled the callback is enabled"
                        " which will affect performance.",
                    )
                    self.create_setting_widget(
                        "Escalate all deprecation warnings to be errors",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/deprecationsAreErrors",
                        SettingType.BOOL,
                        tooltip="When this setting is enabled any deprecation warnings that a code path"
                        " encounters are escalated to log errors in C++ or raise exceptions in Python. This"
                        " provides a preview of what needs to be fixed when hard deprecation of a code path happens.",
                    )
                    self.create_setting_widget(
                        "Enable deprecated USD access in pre-render graphs",
                        f"{PERSISTENT_SETTINGS_PREFIX}/omnigraph/enableUSDInPreRender",
                        SettingType.BOOL,
                        tooltip="(deprecated) - When this setting is enabled, nodes that read USD data may"
                        " safely be used in a pre-render graph.",
                    )
