# 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

from . import UINodeCommon


class OgnWriteWidgetStyle:
    @staticmethod
    def compute(db) -> bool:

        if db.inputs.write != og.ExecutionAttributeState.DISABLED:
            widget_path = db.inputs.widgetPath
            if not widget_path:
                widget_identifier = db.inputs.widgetIdentifier
                if not widget_identifier:
                    db.log_warning("No widgetIdentifier or widgetPath provided.")
                    return False

                widget = UINodeCommon.get_registered_widget(db.abi_context, widget_identifier)
                if not widget:
                    db.log_warning(f"No widget with identifier '{widget_identifier}' found in this graph.")
                    return False
            else:
                widget = UINodeCommon.find_widget_among_all_windows(widget_path)
                if not widget:
                    db.log_warning(f"No widget found at path '{widget_path}'.")
                    return False
                # For error messages only.
                widget_identifier = widget_path

            style_string = db.inputs.style
            if not style_string:
                db.log_error("No style provided.")
                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.set_style(style)

            db.outputs.written = og.ExecutionAttributeState.ENABLED
            return True

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