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

#include "ViewportHoverNodeCommon.h"

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

namespace omni
{
namespace graph
{
namespace ui
{
class OgnOnViewportHovered
{
public:
    struct InternalState
    {
        carb::events::ISubscriptionPtr hoverBeganSub;
        carb::events::ISubscriptionPtr hoverEndedSub;
        ViewportHoverEventPayloads eventPayloads;
        ViewportHoverEventStates eventStates;
    } m_internalState;

    static void initialize(GraphContextObj const&, NodeObj const& nodeObj)
    {
        OgnOnViewportHovered& state = OgnOnViewportHoveredDatabase::sm_stateManagerOgnOnViewportHovered
            .getState<OgnOnViewportHovered>(nodeObj.nodeHandle);

        // Subscribe to hover events
        if (omni::kit::IApp* app = carb::getCachedInterface<omni::kit::IApp>())
        {
            state.m_internalState.hoverBeganSub = carb::events::createSubscriptionToPushByType(
                app->getMessageBusEventStream(),
                kHoverBeganEventType,
                [nodeObj](carb::events::IEvent* e)
                {
                    if (e)
                    {
                        OgnOnViewportHovered& state = OgnOnViewportHoveredDatabase::sm_stateManagerOgnOnViewportHovered
                            .getState<OgnOnViewportHovered>(nodeObj.nodeHandle);
                        state.m_internalState.eventPayloads.setHoverBeganPayload(e->payload);

                        if (nodeObj.iNode->isValid(nodeObj))
                            nodeObj.iNode->requestCompute(nodeObj);
                    }
                }
            );
            state.m_internalState.hoverEndedSub = carb::events::createSubscriptionToPushByType(
                app->getMessageBusEventStream(),
                kHoverEndedEventType,
                [nodeObj](carb::events::IEvent* e)
                {
                    if (e)
                    {
                        OgnOnViewportHovered& state = OgnOnViewportHoveredDatabase::sm_stateManagerOgnOnViewportHovered
                            .getState<OgnOnViewportHovered>(nodeObj.nodeHandle);
                        state.m_internalState.eventPayloads.setHoverEndedPayload(e->payload);

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

    static void release(const NodeObj& nodeObj)
    {
        OgnOnViewportHovered& state = OgnOnViewportHoveredDatabase::sm_stateManagerOgnOnViewportHovered
            .getState<OgnOnViewportHovered>(nodeObj.nodeHandle);

        // Unsubscribe from hover events
        if (state.m_internalState.hoverBeganSub.get())
            state.m_internalState.hoverBeganSub.detach()->unsubscribe();

        if (state.m_internalState.hoverEndedSub.get())
            state.m_internalState.hoverEndedSub.detach()->unsubscribe();
    }

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

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

        // Get the targeted viewport
        char const* const viewportWindowName = db.tokenToString(db.inputs.viewport());
        if (!omni::ui::Workspace::getWindow(viewportWindowName))
        {
            db.logWarning("Viewport window '%s' not found", viewportWindowName);
        }

        // Process event payloads and update event state
        bool hoverBegan = false;
        for (auto const& hoverBeganPayload : state.m_internalState.eventPayloads.hoverBeganPayloads())
        {
            if (!hoverBeganPayload.second.isValid)
                continue;

            auto& eventStateValue = state.m_internalState.eventStates[hoverBeganPayload.first];
            eventStateValue.isHovered = true;

            if (std::strcmp(viewportWindowName, hoverBeganPayload.first.viewportWindowName) == 0)
            {
                hoverBegan = true;
            }
        }

        bool hoverEnded = false;
        for (auto const& hoverEndedPayload : state.m_internalState.eventPayloads.hoverEndedPayloads())
        {
            if (!hoverEndedPayload.second.isValid)
                continue;

            auto& eventStateValue = state.m_internalState.eventStates[hoverEndedPayload.first];
            if (eventStateValue.isHovered)
            {
                eventStateValue.isHovered = false;

                if (std::strcmp(viewportWindowName, hoverEndedPayload.first.viewportWindowName) == 0)
                {
                    hoverEnded = true;
                }
            }
        }

        state.m_internalState.eventPayloads.clear();

        // Get event state and set outputs
        auto it = state.m_internalState.eventStates.find({viewportWindowName});
        if (it != state.m_internalState.eventStates.end())
        {
            if (hoverEnded)
            {
                db.outputs.began() = kExecutionAttributeStateDisabled;
                db.outputs.ended() = kExecutionAttributeStateEnabled;
            }
            else if (hoverBegan)
            {
                db.outputs.began() = kExecutionAttributeStateEnabled;
                db.outputs.ended() = kExecutionAttributeStateDisabled;
            }
            else
            {
                db.outputs.began() = kExecutionAttributeStateDisabled;
                db.outputs.ended() = kExecutionAttributeStateDisabled;
            }
        }

        return true;
    }
};

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