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

#include "SCW_FMemberReference.h"
#include "Engine/MemberReference.h"
#include "SCW_UClass.h"
#include "SourcifyCoreUEWrapperModule.h"

using namespace Sourcify;

SCW_FMemberReference::SCW_FMemberReference(FMemberReference* InMemberReference) : MemberReference(InMemberReference)
{

}

wstring SCW_FMemberReference::GetMemberName() const
{
	return *MemberReference->GetMemberName().ToString();
}

ISCW_UClassPtr SCW_FMemberReference::GetMemberParentClass() const
{
	return MemberReference->GetMemberParentClass() ? FSourcifyCoreUEWrapper::Get().Wrap(*MemberReference->GetMemberParentClass()) : nullptr;
}

wstring SCW_FMemberReference::GetMemberScopeName() const
{
	return *MemberReference->GetMemberScopeName();
}

bool SCW_FMemberReference::IsSelfContext() const
{
	return MemberReference->IsSelfContext();
}

bool SCW_FMemberReference::IsLocalScope() const
{
	return MemberReference->IsLocalScope();
}

ISCW_FPropertyPtr SCW_FMemberReference::ResolveMember_FProperty(ISCW_UClassPtr InClass) const
{
	FProperty* ResolvedMember = MemberReference->ResolveMember<FProperty>(dynamic_cast<SCW_UClass*>(InClass.get())->ToUClass());
	return ResolvedMember ? FSourcifyCoreUEWrapper::Get().Wrap(*ResolvedMember) : nullptr;
}

void SCW_FMemberReference::SetDirect(const wstring InMemberName, /*const FGuid InMemberGuid, */ISCW_UClassPtr InMemberParentClass, bool bIsConsideredSelfContext)
{
	MemberReference->SetDirect(InMemberName.c_str(), FGuid(), InMemberParentClass ? dynamic_cast<SCW_UClass*>(InMemberParentClass.get())->ToUClass() : nullptr, bIsConsideredSelfContext);
}

void SCW_FMemberReference::SetExternalMember(wstring InMemberName, ISCW_UClassPtr InMemberParentClass)
{
	MemberReference->SetExternalMember(InMemberName.c_str(), dynamic_cast<SCW_UClass*>(InMemberParentClass.get())->ToUClass());
}

void SCW_FMemberReference::SetLocalMember(wstring InMemberName, ISCW_UStructPtr InScope, const SCW_FGuid& InMemberGuid)
{
	MemberReference->SetLocalMember(InMemberName.c_str(), dynamic_cast<SCW_UStruct*>(InScope.get())->ToUStructPtr(), FGuid{ InMemberGuid.A, InMemberGuid.B, InMemberGuid.C, InMemberGuid.D });
}

void SCW_FMemberReference::SetLocalMember(wstring InMemberName, const wstring& InScope, const SCW_FGuid& InMemberGuid)
{
	MemberReference->SetLocalMember(InMemberName.c_str(), InScope.c_str(), FGuid{ InMemberGuid.A, InMemberGuid.B, InMemberGuid.C, InMemberGuid.D });
}

void SCW_FMemberReference::SetMemberName(wstring InMemberName)
{
	MemberReference->SetMemberName(InMemberName.c_str());
}

FMemberReference* SCW_FMemberReference::ToFMemberReference() const
{
	return MemberReference;
}

ISCW_FMemberReferencePtr SCW_FMemberReference::ToFMemberReferencePtr()
{
	return GetSharedThisAs<SCW_FMemberReference>();
}

ISCW_FMemberReferenceConstPtr SCW_FMemberReference::ToFMemberReferencePtr() const
{
	return GetSharedThisAs<SCW_FMemberReference>();
}