// 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.
//

#include <OgnOnViewportClickedDatabase.h>

#include "ViewportClickNodeCommon.h"

#include <omni/kit/IApp.h>
#include <omni/ui/Workspace.h>

namespace omni
{
namespace graph
{
namespace ui
{
class OgnOnViewportClicked
{
public:
    struct InternalState
    {
        carb::events::ISubscriptionPtr clickSub;
        ViewportClickEventPayloads eventPayloads;
    } m_internalState;

    static void initialize(GraphContextObj const&, NodeObj const& nodeObj)
    {
        OgnOnViewportClicked& state = OgnOnViewportClickedDatabase::sm_stateManagerOgnOnViewportClicked.getState<OgnOnViewportClicked>(nodeObj.nodeHandle);

        // Subscribe to click events
        if (omni::kit::IApp* app = carb::getCachedInterface<omni::kit::IApp>())
        {
            state.m_internalState.clickSub = carb::events::createSubscriptionToPushByType(
                app->getMessageBusEventStream(),
                kClickEventType,
                [nodeObj](carb::events::IEvent* e)
                {
                    if (e)
                    {
                        OgnOnViewportClicked& state = OgnOnViewportClickedDatabase::sm_stateManagerOgnOnViewportClicked.getState<OgnOnViewportClicked>(nodeObj.nodeHandle);
                        state.m_internalState.eventPayloads.setPayload(e->payload);

                        if (nodeObj.iNode->isValid(nodeObj))
                            nodeObj.iNode->requestCompute(nodeObj);
                    }
                }
            );
        }
    }

    static void release(const NodeObj& nodeObj)
    {
        OgnOnViewportClicked& state = OgnOnViewportClickedDatabase::sm_stateManagerOgnOnViewportClicked.getState<OgnOnViewportClicked>(nodeObj.nodeHandle);

        // Unsubscribe from click events
        if (state.m_internalState.clickSub.get())
            state.m_internalState.clickSub.detach()->unsubscribe();
    }

    static bool compute(OgnOnViewportClickedDatabase& db)
    {
        if (checkNodeDisabledForOnlyPlay(db))
            return true;

        OgnOnViewportClicked& state = db.internalState<OgnOnViewportClicked>();
        if (state.m_internalState.eventPayloads.empty())
            return true;

        // Get the targeted viewport and gesture
        char const* const viewportWindowName = db.tokenToString(db.inputs.viewport());
        char const* const gestureName = db.tokenToString(db.inputs.gesture());

        if (!omni::ui::Workspace::getWindow(viewportWindowName))
        {
            db.logWarning("Viewport window '%s' not found", viewportWindowName);
        }

        auto const* eventPayloadValuePtr = state.m_internalState.eventPayloads.getPayloadValue(viewportWindowName, gestureName);
        if (eventPayloadValuePtr)
        {
            db.outputs.position() = db.inputs.useNormalizedCoords() ? eventPayloadValuePtr->positionNorm : eventPayloadValuePtr->positionPixel;
            db.outputs.clicked() = kExecutionAttributeStateEnabled;
        }

        state.m_internalState.eventPayloads.clear();

        return true;
    }
};

REGISTER_OGN_NODE()
} // ui
} // graph
} // omni
