// Copyright (c) 2024 Codemongous LLC. All Rights Reserved.

#include "SCW_FSubobjectData.h"
#include "SCW_UBlueprint.h"
#include "SourcifyCoreUEWrapperModule.h"
#include "SubobjectData.h"

using namespace Sourcify;

SCW_FSubobjectData::SCW_FSubobjectData(FSubobjectData* InSubobjectData) : SubobjectData(InSubobjectData)
{

}

vector<ISCW_FSubobjectDataHandleConstPtr> SCW_FSubobjectData::GetChildrenHandles() const
{
	vector<ISCW_FSubobjectDataHandleConstPtr> WrappedHandles;
	for (const FSubobjectDataHandle& Handle : SubobjectData->GetChildrenHandles())
	{
		// Vector of consts returned that it's not worth writing a new wrapper for
		WrappedHandles.push_back(FSourcifyCoreUEWrapper::Get().Wrap(const_cast<FSubobjectDataHandle&>(Handle)));
	}
	return WrappedHandles;
}

ISCW_UObjectConstPtr SCW_FSubobjectData::GetObject(bool bEvenIfPendingKill/* = false*/) const
{
	const UObject* object = SubobjectData->GetObject(bEvenIfPendingKill);
	// Const returned that it's not worth writing a new wrapper for
	return object ? FSourcifyCoreUEWrapper::Get().Wrap(*(const_cast<UObject*>(object))) : nullptr;
}

ISCW_UObjectConstPtr SCW_FSubobjectData::GetObjectForBlueprint(ISCW_UBlueprintPtr Blueprint) const
{
	const UObject* object = SubobjectData->GetObjectForBlueprint(dynamic_cast<SCW_UBlueprint*>(Blueprint.get())->ToUBlueprintRawPtr());
	// Const returned that it's not worth writing a new wrapper for
	return object ? FSourcifyCoreUEWrapper::Get().Wrap(*(const_cast<UObject*>(SubobjectData->GetObjectForBlueprint(dynamic_cast<SCW_UBlueprint*>(Blueprint.get())->ToUBlueprintRawPtr())))) : nullptr;
}

wstring SCW_FSubobjectData::GetVariableName() const
{
	return *SubobjectData->GetVariableName().ToString();
}

FSubobjectData* SCW_FSubobjectData::ToFSubobjectData() const
{
	return SubobjectData;
}

ISCW_FSubobjectDataPtr SCW_FSubobjectData::ToFSubobjectDataPtr()
{
	return GetSharedThisAs<SCW_FSubobjectData>();
}

ISCW_FSubobjectDataConstPtr SCW_FSubobjectData::ToFSubobjectDataPtr() const
{
	return GetSharedThisAs<SCW_FSubobjectData>();
}