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


#include <PTWCServer.h>
#include <ProUtil.h>

/*====================================================================*\
FUNCTION: PTUDFExGetLine
PURPOSE:  Read one line from the file.
\*====================================================================*/
static ProError PTWCGetLine (FILE* f, char** line)
{
	char buf [1000];
	char* ret;
	int len;

	if (fgets (buf, 1000, f) == NULL)
	{
		return (PRO_TK_E_NOT_FOUND);
	}

/*-------------------------------------------------------------------*\
	Copy the line to the output string (and strip off the newline char)
\*-------------------------------------------------------------------*/
	len = strlen (buf);
	ret = (char*) calloc (len, sizeof (char));
	strncpy (ret, buf, len - 1);
	ret [len - 1] = '\0';

	*line = ret;

	return (PRO_TK_NO_ERROR);
}

/*====================================================================*\
  FUNCTION: PTWCAddPathToArray
  PURPOSE:  Add model path to the array of paths
\*====================================================================*/
static ProError PTWCAddPathToArray (wchar_t* new_path, 
									wchar_t*** path_array)
{
	wchar_t** actual_array = *path_array;
	if (actual_array == NULL)
	{
		status = ProArrayAlloc (1, sizeof (wchar_t*), 1, (ProArray*)&actual_array);
		PT_TEST_LOG_SUCC ("ProArrayAlloc()");
		actual_array [0] = new_path;
	}
	else
	{
		status = ProArrayObjectAdd ((ProArray*)&actual_array, PRO_VALUE_UNUSED, 1, 
							&new_path);
		PT_TEST_LOG_SUCC ("ProArrayObjectAdd()");
	}
	*path_array = actual_array;
	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCGetRegistryFileInfo
  PURPOSE:  Read data from the populate registry file
\*====================================================================*/
ProError PTWCGetInitFileInfo (char* file, PTWCInitFileInfo* info)
{
	FILE* fileHandle;
	char* line;

	if (file == NULL || info == NULL)
		return PRO_TK_BAD_INPUTS;

	info->username = info->password = info->server_url = info->project = NULL;
	info->design_models = NULL;
	info->plan_models = NULL;

	fileHandle = fopen (file, "r");
	if (fileHandle == NULL)
		return PRO_TK_E_NOT_FOUND;

	while (PTWCGetLine (fileHandle, &line) == PRO_TK_NO_ERROR)
	{
		char* label = strtok (line, " ");
		char* value;
		wchar_t* wValue;
		if (label == NULL)
			continue;
		value = strtok (NULL, " ");
		if (value == NULL)
			continue;
		wValue = (wchar_t*)calloc (strlen (value) + 1, sizeof (wchar_t));
		ProStringToWstring (wValue, value);

		if (strcmp (label, REG_FILE_USERNAME) == 0)
		{
			info->username = wValue;
		}
		else if (strcmp (label, REG_FILE_PASSWORD) == 0)
		{
			info->password = wValue;
		}
		else if (strcmp (label, REG_FILE_SERVER_URL) == 0)
		{
			info->server_url = wValue;
		}
		else if (strcmp (label, REG_FILE_PROJECT) == 0)
		{
			info->project = wValue;
		}
		else if (strcmp (label, REG_FILE_DESIGN_MODEL) == 0)
		{
			PTWCAddPathToArray (wValue, &info->design_models);
		}
		else if (strcmp (label, REG_FILE_PLAN_MODEL) == 0)
		{
			PTWCAddPathToArray (wValue, &info->plan_models);
		}
	}

	fclose (fileHandle);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCRegistryFileDataFree  
  PURPOSE:  Clear memory from the registry file data 
\*====================================================================*/
void PTWCInitFileDataFree (PTWCInitFileInfo* info)
{
	int num_paths, i;

	if (info->username != NULL)
		free (info->username);
	if (info->password != NULL)
		free (info->password);
	if (info->server_url != NULL)
		free (info->server_url);
	if (info->project != NULL)
		free (info->project);
	if (info->design_models != NULL)
	{
		ProArraySizeGet (info->design_models, &num_paths);

		for (i = 0; i < num_paths; i++)
			free (info->design_models [i]);

		ProArrayFree ((ProArray*)&info->design_models);
	}
	if (info->plan_models != NULL)
	{
		ProArraySizeGet (info->plan_models, &num_paths);

		for (i = 0; i < num_paths; i++)
			free (info->plan_models [i]);

		ProArrayFree ((ProArray*)&info->plan_models);
	}
}