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

#include "SCW_UDataTable.h"
#include "Engine/DataTable.h"
#include "SCW_UScriptStruct.h"
#include "SourcifyCoreUEWrapperModule.h"

using namespace Sourcify;

SCW_UDataTable::SCW_UDataTable(UDataTable* InDataTable) : SCW_UObject(InDataTable), DataTable(InDataTable)
{

}

UDataTable* SCW_UDataTable::ToUDataTable() const
{
	return DataTable;
}

ISCW_UDataTablePtr SCW_UDataTable::ToUDataTablePtr()
{
	return GetSharedThisAs<SCW_UDataTable>();
}

ISCW_UDataTableConstPtr SCW_UDataTable::ToUDataTablePtr() const
{
	return GetSharedThisAs<SCW_UDataTable>();
}

vector<wstring> SCW_UDataTable::CreateTableFromCSVString(const wstring& csvString)
{
	TArray<FString> errors = DataTable->CreateTableFromCSVString(csvString.c_str());
	vector<wstring> errorsWString;
	for (const FString& error : errors)
	{
		errorsWString.push_back(*error);
	}
	return errorsWString;
}

void SCW_UDataTable::EmptyTable()
{
	DataTable->EmptyTable();
}

wstring SCW_UDataTable::GetTableAsCSV() const
{
	return *DataTable->GetTableAsCSV();
}

ISCW_UScriptStructPtr SCW_UDataTable::GetRowStruct() const
{
	return FSourcifyCoreUEWrapper::Get().Wrap(*DataTable->RowStruct);
}

void SCW_UDataTable::SetRowStruct(ISCW_UScriptStructPtr rowStruct)
{
	DataTable->RowStruct = dynamic_cast<SCW_UScriptStruct*>(rowStruct.get())->ToUScriptStruct();
}

void SCW_UDataTable::HandleDataTableChanged()
{
	DataTable->HandleDataTableChanged();
}