// 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 <OgnToFloatDatabase.h>
#include <omni/graph/core/ogn/ComputeHelpers.h>
#include <omni/graph/core/ogn/UsdTypes.h>

namespace omni
{
namespace graph
{
namespace nodes
{
namespace
{
template<typename T>
bool tryComputeAssumingType(OgnToFloatDatabase& db, size_t count)
{
    auto functor = [](auto const& value, auto& converted) { converted = static_cast<float>(value); };
    return ogn::compute::tryComputeWithArrayBroadcasting<T, float>(db.inputs.value(), db.outputs.converted(), functor, count);
}

template<typename T, size_t tupleSize>
bool tryComputeAssumingType(OgnToFloatDatabase& db, size_t count)
{
    auto functor = [](auto const& value, auto& converted) { converted = static_cast<float>(value); };
    return ogn::compute::tryComputeWithTupleBroadcasting<tupleSize, T, float>(
        db.inputs.value(), db.outputs.converted(), functor, count);
}

} // namespace

class OgnToFloat
{
public:
    // Node to convert numeric inputs to floats
    static bool computeVectorized(OgnToFloatDatabase& db, size_t count)
    {
        auto& inputType = db.inputs.value().type();
        // Compute the components, if the types are all resolved.
        try
        {
            switch (inputType.baseType)
            {
            case BaseDataType::eBool:
                return tryComputeAssumingType<bool>(db, count);
            case BaseDataType::eDouble:
                switch (inputType.componentCount)
                {
                    case 1:  return tryComputeAssumingType<double>(db, count);
                    case 2:  return tryComputeAssumingType<double, 2>(db, count);
                    case 3:  return tryComputeAssumingType<double, 3>(db, count);
                    case 4:  return tryComputeAssumingType<double, 4>(db, count);
                    case 9:  return tryComputeAssumingType<double, 9>(db, count);
                    case 16: return tryComputeAssumingType<double, 16>(db, count);
                    default: break;
                }
            case BaseDataType::eFloat:
                switch (inputType.componentCount)
                {
                    case 1: return tryComputeAssumingType<float>(db, count);
                    case 2: return tryComputeAssumingType<float, 2>(db, count);
                    case 3: return tryComputeAssumingType<float, 3>(db, count);
                    case 4: return tryComputeAssumingType<float, 4>(db, count);
                    default: break;
                }
            case BaseDataType::eHalf:
                switch (inputType.componentCount)
                {
                    case 1: return tryComputeAssumingType<pxr::GfHalf>(db, count);
                    case 2: return tryComputeAssumingType<pxr::GfHalf, 2>(db, count);
                    case 3: return tryComputeAssumingType<pxr::GfHalf, 3>(db, count);
                    case 4: return tryComputeAssumingType<pxr::GfHalf, 4>(db, count);
                    default: break;
                }
            case BaseDataType::eInt:
                switch (inputType.componentCount)
                {
                    case 1: return tryComputeAssumingType<int32_t>(db, count);
                    case 2: return tryComputeAssumingType<int32_t, 2>(db, count);
                    case 3: return tryComputeAssumingType<int32_t, 3>(db, count);
                    case 4: return tryComputeAssumingType<int32_t, 4>(db, count);
                    default: break;
                }
                ;
            case BaseDataType::eInt64:
                return tryComputeAssumingType<int64_t>(db, count);
            case BaseDataType::eUChar:
                return tryComputeAssumingType<unsigned char>(db, count);
            case BaseDataType::eUInt:
                return tryComputeAssumingType<uint32_t>(db, count);
            case BaseDataType::eUInt64:
                return tryComputeAssumingType<uint64_t>(db, count);
            default: break;
            }

            db.logWarning("Failed to resolve input types");
        }
        catch (const std::exception& e)
        {
            db.logError("Input could not be converted to float: %s", e.what());
            return false;
        }

        return true;
    }

    static void onConnectionTypeResolve(const NodeObj& nodeObj)
    {
        auto valueAttr = nodeObj.iNode->getAttributeByToken(nodeObj, inputs::value.token());
        auto outAttr = nodeObj.iNode->getAttributeByToken(nodeObj, outputs::converted.token());

        auto valueType = valueAttr.iAttribute->getResolvedType(valueAttr);

        // The output shape must match the input shape and visa-versa, however we can't say anything
        // about the input base type until it's connected
        if (valueType.baseType != BaseDataType::eUnknown)
        {
            Type resultType(BaseDataType::eFloat, valueType.componentCount, valueType.arrayDepth);
            outAttr.iAttribute->setResolvedType(outAttr, resultType);
        }
    }

};

REGISTER_OGN_NODE();

}
}
}
