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

// clang-format off
#include "UsdPCH.h"
// clang-format on

#include "PrimCommon.h"
#include "VariantCommon.h"

#include <carb/logging/Log.h>

#include <OgnClearVariantSelectionDatabase.h>

namespace omni::graph::nodes
{
class OgnClearVariantSelection
{
public:
    static bool compute(OgnClearVariantSelectionDatabase& db)
    {
        try
        {
            pxr::UsdPrim prim = tryGetTargetPrim(db, db.inputs.prim(), "prim");

            pxr::UsdVariantSets variantSets = prim.GetVariantSets();
            auto variantSetName = db.tokenToString(db.inputs.variantSetName());
            pxr::UsdVariantSet variantSet = variantSets.GetVariantSet(variantSetName);
            if (db.inputs.setVariant())
            {
                bool success = variantSet.SetVariantSelection("");
                if (!success)
                    throw warning(std::string("Failed to clear variant selection for variant set ") + variantSetName);
            }
            else
            {
                removeLocalOpinion(prim, variantSetName);
            }
            db.outputs.execOut() = kExecutionAttributeStateEnabled;
            return true;
        }
        catch (const warning& e)
        {
            db.logWarning(e.what());
        }
        catch (const std::exception& e)
        {
            db.logError(e.what());
        }

        return false;
    }
};

REGISTER_OGN_NODE()

} // namespace omni::graph::nodes
