// Copyright (c) 2022-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.
//

#include <OgnOnceDatabase.h>
#include <omni/graph/action/IActionGraph.h>

namespace omni
{
namespace graph
{
namespace action
{

class OgnOnce
{
    bool m_open {false};
public:
    static bool compute(OgnOnceDatabase& db)
    {
        auto iActionGraph = getInterface();
        auto& state = db.internalState<OgnOnce>();

        bool isReset = iActionGraph->getExecutionEnabled(inputs::reset.token(), db.getInstanceIndex());
        if (isReset)
        {
            state.m_open = false;
            return true;
        }

        if (!state.m_open)
        {
            state.m_open = true;
            iActionGraph->setExecutionEnabled(outputs::once.token(), db.getInstanceIndex());
        }
        else
        {
            iActionGraph->setExecutionEnabled(outputs::after.token(), db.getInstanceIndex());
        }

        return true;
    }
};

REGISTER_OGN_NODE()
} // action
} // graph
} // omni
