# 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.
#
from dataclasses import dataclass

import omni.graph.core as og

# Set True to enable verbose debugging prints to console
DEBUG_PRINT = False


# ==============================================================================================================
@dataclass
class OgnTickNState:
    count: int


# --------------------------------------------------------------------------------------------------------------
class OgnTickN:
    @staticmethod
    def internal_state():
        """Returns an object that will contain per-node state information"""
        return OgnTickNState(-1)

    @staticmethod
    def compute(db) -> bool:
        def print_state(state_string: str):
            print(f"{db.node.get_prim_path()} {state_string}")

        count = db.internal_state.count
        count += 1
        if count == 0:
            _ = DEBUG_PRINT and print_state("START")
            finished, tick = og.ExecutionAttributeState.LATENT_PUSH, og.ExecutionAttributeState.DISABLED
        else:
            duration = db.inputs.duration
            if duration < count:
                # Finished
                finished, tick = og.ExecutionAttributeState.LATENT_FINISH, og.ExecutionAttributeState.DISABLED
                count = -1
                _ = DEBUG_PRINT and print_state("FINISHED")
            else:
                # Still ticking
                finished = og.ExecutionAttributeState.DISABLED
                if (count % db.inputs.period) == 0:  # noqa: S001
                    tick = og.ExecutionAttributeState.ENABLED
                else:
                    tick = og.ExecutionAttributeState.DISABLED
                _ = DEBUG_PRINT and print_state("TICK")

        # Write outputs
        db.outputs.finished = finished
        db.outputs.tick = tick
        db.internal_state.count = count
        return True
