# Copyright (c) 2022, 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 omni.graph.core as og
import omni.ui as ui

from . import UINodeCommon


class OgnSpacer(UINodeCommon.OgWidgetNode):
    @staticmethod
    def compute(db) -> bool:
        if db.inputs.create != og.ExecutionAttributeState.DISABLED:
            parent_widget_path = db.inputs.parentWidgetPath
            if not parent_widget_path:
                db.log_error("No parentWidgetPath supplied.")
                return False

            parent_widget = UINodeCommon.find_widget_among_all_windows(parent_widget_path)
            if parent_widget is None:
                db.log_error("Could not find parent widget.")
                return False

            style = {}
            try:
                style = UINodeCommon.to_ui_style(db.inputs.style)
            except SyntaxError as err:
                db.log_error(f"'inputs:style': {err.msg}")
                return False

            widget_identifier = UINodeCommon.get_unique_widget_identifier(db)

            # Now create the spacer widget and register it.
            with parent_widget:
                spacer = ui.Spacer(identifier=widget_identifier, width=db.inputs.width, height=db.inputs.height)
                if style:
                    spacer.set_style(style)

            OgnSpacer.register_widget(db.abi_context, widget_identifier, spacer)
            db.outputs.created = og.ExecutionAttributeState.ENABLED
            db.outputs.widgetPath = UINodeCommon.find_widget_path(spacer)
            return True

        db.log_warning("Unexpected execution with no execution input enabled")
        return False


# The spacer has no properties or styles of its own: height and width are base Widget properties.
