/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


#include <ProToolkit.h>
#include <ProUdf.h>

#include <PTUDFExamples.h>
#include <PTUDFExDashboardStructs.h>

/*======================================================================================================*\
FUNCTION: PTUDFExUpdateReferences
PURPOSE:  Update data related to references needed by the current selected UDF.
\*======================================================================================================*/
void PTUDFExUpdateReferences (PTUDFExUdfdata* db_udf_data)
{
	ProUdfRequiredRef* req_ref_array;
	int size;
	int i;

/*-----------------------------------------------------------------------------------------------------*\
Check the number of references required by this UDF data
\*-----------------------------------------------------------------------------------------------------*/
	status = ProUdfdataRequiredreferencesGet (db_udf_data->udf_data, &req_ref_array);
	PT_TEST_LOG_SUCC ("ProUdfdataRequiredreferencesGet()");

	ProArraySizeGet (req_ref_array, &size);

	db_udf_data->num_references = size;

	ProArrayAlloc (size, sizeof (PTUDFExReference), 1, (ProArray*)&db_udf_data->reference_array);

	for (i = 0; i < size; i ++)
	{
		status = ProUdfrequiredrefPromptGet (req_ref_array [i], db_udf_data->reference_array[i].prompt);
		PT_TEST_LOG_SUCC ("ProUdfrequiredrefPromptGet()");

		status = ProUdfrequiredrefTypeGet (req_ref_array [i], &db_udf_data->reference_array[i].type);
		PT_TEST_LOG_SUCC ("ProUdfrequiredrefTypeGet()");

		db_udf_data->reference_array[i].sel_ref = NULL;
	}
}


/*======================================================================================================*\
FUNCTION: PTUDFExUpdateVarparams
PURPOSE:  Update data related to variable parameters available to the current selected UDF.
\*======================================================================================================*/
void PTUDFExUpdateVarparams (PTUDFExUdfdata* db_udf_data)
{
	ProUdfvarparam* var_param_array;
	int size;
	int i;

	status = ProUdfdataVarparamsGet (db_udf_data->udf_data,  &var_param_array);
	PT_TEST_LOG_SUCC ("ProUdfdataVarparamsGet()");

	if (status == PRO_TK_NO_ERROR)
	{
		status = ProArraySizeGet (var_param_array, &size);
		PT_TEST_LOG_SUCC ("ProArraySizeGet()");

		status = ProArrayAlloc (size, sizeof (PTUDFExVarParam), 1, (ProArray*)&db_udf_data->varparam_array);
		PT_TEST_LOG_SUCC ("ProArrayAlloc()");

		for (i = 0; i < size; i++)
		{
			ProModelitem owner_item;

			status = ProUdfvarparamNameGet (var_param_array [i], db_udf_data->varparam_array[i].param_name);
			PT_TEST_LOG_SUCC ("ProUdfvarparamNameGet()");

			status = ProUdfvarparamOwnerGet (var_param_array [i], &owner_item);
			PT_TEST_LOG_SUCC ("ProUdfvardimOwnerGet()");

			db_udf_data->varparam_array[i].owner_id = owner_item.id;
			db_udf_data->varparam_array[i].owner_type = owner_item.type;

			status = ProUdfvarparamDefaultvalueGet (var_param_array [i], &db_udf_data->varparam_array[i].value);
			PT_TEST_LOG_SUCC ("ProUdfvardimDefaultvalueGet()");
		}

		ProUdfvarparamProarrayFree (var_param_array);

		db_udf_data->num_varparams = size;
	}
}

/*======================================================================================================*\
FUNCTION: PTUDFExUpdateVardims
PURPOSE:  Update data related to variable dimensions available to the current selected UDF.
\*======================================================================================================*/
void PTUDFExUpdateVardims (PTUDFExUdfdata* db_udf_data)
{
	ProUdfvardim* var_dim_array;
	int size;
	int i;

	status = ProUdfdataVardimsGet (db_udf_data->udf_data,  &var_dim_array);
	PT_TEST_LOG_SUCC ("ProUdfdataVardimsGet()");

	if (status == PRO_TK_NO_ERROR)
	{
		status = ProArraySizeGet (var_dim_array, &size);
		PT_TEST_LOG_SUCC ("ProArraySizeGet()");

		status = ProArrayAlloc (size, sizeof (PTUDFExVarDim), 1, (ProArray*)&db_udf_data->vardim_array);
		PT_TEST_LOG_SUCC ("ProArrayAlloc()");

		for (i = 0; i < size; i++)
		{
			status = ProUdfvardimPromptGet (var_dim_array [i], db_udf_data->vardim_array[i].prompt);
			PT_TEST_LOG_SUCC ("ProUdfvardimPromptGet()");

			status = ProUdfvardimNameGet (var_dim_array [i], db_udf_data->vardim_array[i].dim_symbol);
			PT_TEST_LOG_SUCC ("ProUdfvardimNameGet()");
			
			status = ProUdfvardimDefaultvalueGet (var_dim_array [i], &db_udf_data->vardim_array[i].type,
															&db_udf_data->vardim_array[i].value);
			PT_TEST_LOG_SUCC ("ProUdfvardimDefaultvalueGet()");	
		}

		ProUdfvardimProarrayFree (var_dim_array);

		db_udf_data->num_vardims = size;
	}
}

/*======================================================================================================*\
FUNCTION: PTUDFExUpdateInstances
PURPOSE:  Update data related to instances available to the current selected UDF.
\*======================================================================================================*/
void PTUDFExUpdateInstances (PTUDFExUdfdata* db_udf_data)
{
	status = ProUdfdataInstancenamesGet (db_udf_data->udf_data, &db_udf_data->instance_names);
	PT_TEST_LOG_SUCC ("ProUdfdataInstancenamesGet()");
}