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

#include <ProToolkit.h>
#include <ProWorkspace.h>
#include <ProWTUtils.h>
#include <ProBrowser.h>
#include <ProUtil.h>
#include <ProWstring.h>
#include <ProCore.h>

#include <PTWCServer.h>

#define POPULATE_ALIAS L"_PTWC_Populate"

/*====================================================================*\
  FUNCTION: PTWCPopulateServer
  PURPOSE:  Populate the ProjectLink project with models based on the 
            contents of the initialization file
\*====================================================================*/
int PTWCPopulateServer (char* init_file)
{
	PTWCInitFileInfo file_data;
	int i, num_workspaces;
	wchar_t* context;
	wchar_t* ws_name;
	wchar_t* aliased_url;
	ProServerWorkspaceData* workspaces;
	ProServerCheckinOptions opts;
	ProServerCheckinConflicts conflicts;
	ProPath designs_path;
	ProPath plans_path;

	status = PTWCGetInitFileInfo (init_file, &file_data);
	if (status == PRO_TK_NO_ERROR)
	{
/*--------------------------------------------------------------------*\
		Setup authentication
\*--------------------------------------------------------------------*/
		status = ProBrowserAuthenticate (file_data.username, file_data.password);
		PT_TEST_LOG_SUCC ("ProBrowserAuthenticate()");

/*--------------------------------------------------------------------*\
		Register and activate the server.  We find the workspace on the 
		given project (context) to use as the active workspace
\*--------------------------------------------------------------------*/
		status = ProServerWorkspacesCollect (file_data.server_url, &workspaces);
		PT_TEST_LOG_SUCC ("ProServerWorkspacesCollect()");

		status = ProArraySizeGet (workspaces, &num_workspaces);
		PT_TEST_LOG_SUCC ("ProArraySizeGet()");

		for (i = 0; i < num_workspaces; i++)
		{
			int result = -1;

			status = ProServerworkspacedataContextGet (workspaces [i], &context);
			PT_TEST_LOG_SUCC ("ProServerworkspacedataContextGet()");

			ProWstringCompare (context, file_data.project, PRO_VALUE_UNUSED, &result);
			ProWstringFree (context);

			if (result == 0)
			{
				status = ProServerworkspacedataNameGet (workspaces [i], &ws_name);
				PT_TEST_LOG_SUCC ("ProServerworkspacedataNameGet()");

				break;
			}
		}

		ProServerworkspacedataProarrayFree (workspaces);

		status = ProServerRegister (POPULATE_ALIAS, file_data.server_url, ws_name, 
									&aliased_url);
		PT_TEST_LOG_SUCC ("ProServerRegister()");

		status = ProServerActivate (POPULATE_ALIAS);
		PT_TEST_LOG_SUCC ("ProServerActivate()");

/*--------------------------------------------------------------------*\
		Import part and assembly models.  
\*--------------------------------------------------------------------*/

		if (file_data.design_models != NULL)
		{
			status = ProCurrentWorkspaceImport (PRO_WS_IMPEX_ALL, 
												file_data.design_models);
			PT_TEST_LOG_SUCC ("ProCurrentWorkspaceImport()");
		}

		PTWCUtilBuildPJLFolder (POPULATE_ALIAS, file_data.project, 
									"Designs", designs_path);

/*--------------------------------------------------------------------*\
		Prepare the checkin options; by default the imported parts
		and assemblies go in the "Designs" folder.
\*--------------------------------------------------------------------*/
		status = ProServercheckinoptsAlloc (&opts);
		PT_TEST_LOG_SUCC ("ProServercheckinoptsAlloc()");

		status = ProServercheckinoptsDeflocationSet (opts, designs_path);
		PT_TEST_LOG_SUCC ("ProServercheckinoptsDeflocationSet()");
	
/*--------------------------------------------------------------------*\
		Import drawing models.  
\*--------------------------------------------------------------------*/
		if (file_data.plan_models != NULL)
		{
			int num_drawings, i;
			ProPath filepath;
			ProMdlName name;
			ProMdlExtension extension;
			ProMdlFileName filename;

			status = ProCurrentWorkspaceImport (PRO_WS_IMPEX_NONE, 
												file_data.plan_models);
			PT_TEST_LOG_SUCC ("ProCurrentWorkspaceImport()");

/*--------------------------------------------------------------------*\
			The imported drawings go in the "Plans" folder.  
\*--------------------------------------------------------------------*/

			PTWCUtilBuildPJLFolder (POPULATE_ALIAS, file_data.project, 
									"Plans", plans_path);

			status = ProArraySizeGet (file_data.plan_models, &num_drawings);
			PT_TEST_LOG_SUCC ("ProArraySizeGet()");

			for (i = 0; i < num_drawings; i++)
			{
/*--------------------------------------------------------------------*\
				Each item is assigned to the checkin options using its
				model name.
\*--------------------------------------------------------------------*/
				ProWstringCopy (file_data.plan_models [i], filepath, PRO_VALUE_UNUSED);

				status = ProFileMdlnameParse (filepath, NULL, 
											name, extension, NULL); 
				PT_TEST_LOG_SUCC ("ProFileMdlnameParse()");

				ProWstringCopy (name, filename, PRO_VALUE_UNUSED);
				ProWstringConcatenate (L".", filename, PRO_VALUE_UNUSED);
				ProWstringConcatenate (extension, filename, PRO_VALUE_UNUSED);

				status = ProServercheckinoptsLocationAdd (opts, filename, plans_path);
				PT_TEST_LOG_SUCC ("ProServercheckinoptsLocationAdd()");
			}
		}

/*--------------------------------------------------------------------*\
		Check in the entire workspace using the options.
\*--------------------------------------------------------------------*/
		status = ProServerObjectsCheckin (NULL, opts, &conflicts);
		PT_TEST_LOG_SUCC ("ProServerObjectsCheckin()");

		if (status == PRO_TK_CHECKOUT_CONFLICT)
		{
			PTWCUtilConflictReport (conflicts);
			ProServerconflictsFree (conflicts);
		}

/*--------------------------------------------------------------------*\
		Clean the workspace, and free allocated memory
\*--------------------------------------------------------------------*/
		PTWCUtilRemoveAllFromWS (POPULATE_ALIAS, ws_name);

		PTWCInitFileDataFree (&file_data);

		ProWstringFree (ws_name);

	}
/*--------------------------------------------------------------------*\
	End the Pro/ENGINEER session
\*--------------------------------------------------------------------*/
	PT_TEST_LOG_CLOSE
	ProEngineerEnd ();

	return (PRO_TK_NO_ERROR);
}