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


#include <ProUIMessage.h>
#include <ProWstring.h>
#include <stdio.h>
#include <wchar.h>


static ProBoolean s_interactive = PRO_B_TRUE;

/*====================================================================*\
  FUNCTION: PTWCUtilSetInteractive
  PURPOSE:	Set the mode of the application to be interactive or 
			non-interactive
\*====================================================================*/
ProError PTWCUtilSetInteractive (ProBoolean interactive)
{
	s_interactive = interactive;

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilGetInteractive
  PURPOSE:  Identifies if the application is in interactive mode or not
\*====================================================================*/
ProBoolean PTWCUtilGetInteractive ()
{
	return s_interactive;
}

/*====================================================================*\
  FUNCTION: PTWCUtilConflictReport
  PURPOSE:  Inform the user of the description of the latest checkout 
			conflict, and log it as well.
\*====================================================================*/
ProError PTWCUtilConflictReport (ProServerConflicts conflicts)
{
	wchar_t* description;
	char* c_description;
	int length;
				
	ProServerconflictsDescriptionGet (conflicts, &description);

	ProWstringLengthGet (description, &length);

	/* Allow for the potential of multibyte characters in the conflict
	   description. */
	length = length * 3;
	length++;

	c_description = (char*) calloc (length, sizeof (char));

	ProWstringToString (c_description, description);

	fprintf (errlog_fp, c_description);
	fprintf (errlog_fp, "\n");

	if (PTWCUtilGetInteractive ())
	{
		ProUIMessageButton* buttons;
		ProUIMessageButton result;
		ProArrayAlloc (1, sizeof (ProUIMessageButton), 1, (ProArray*)&buttons);
		buttons[0] = PRO_UI_MESSAGE_OK;

		ProUIMessageDialogDisplay (PROUIMESSAGE_ERROR, L"Conflict occurred",
									description, buttons, buttons[0], &result);
	}

	ProWstringFree (description);
	free (&c_description);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilRemoveAllFromWS
  PURPOSE:  Remove all objects from the workspace 
\*====================================================================*/
ProError PTWCUtilRemoveAllFromWS (wchar_t* alias, wchar_t* ws_name)
{
	ProServerRemoveConflicts conflicts = NULL;
	wchar_t* current_ws_name;
	int result;

	status = ProServerWorkspaceGet (alias, &current_ws_name);
	PT_TEST_LOG_SUCC ("ProServerWorkspaceGet()");

	ProWstringCompare (ws_name, current_ws_name, PRO_VALUE_UNUSED, &result);
	if (result != 0)
	{
		status = ProServerWorkspaceSet (alias, ws_name);
		PT_TEST_LOG_SUCC ("ProServerWorkspaceSet()");
	}

	status = ProServerObjectsRemove (NULL, &conflicts);
	PT_TEST_LOG_SUCC ("ProServerObjectsRemove()");

	if (status != PRO_TK_NO_ERROR && conflicts != NULL)
	{
		PTWCUtilConflictReport (conflicts);
		ProServerconflictsFree (conflicts);
	}

	if (result != 0)
	{
		status = ProServerWorkspaceSet (alias, current_ws_name);
		PT_TEST_LOG_SUCC ("ProServerWorkspaceSet()");
	}

	ProWstringFree (current_ws_name);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilBuildWSPath
  PURPOSE:  Construct the path to the workspace, given the server and 
			workspace name.
\*====================================================================*/
ProError PTWCUtilBuildWSPath (wchar_t* server, wchar_t* ws_name,
							  ProPath folder)
{
	ProStringToWstring (folder, "wtws://");
	ProWstringConcatenate (server, folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (L"/", folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (ws_name, folder, PRO_VALUE_UNUSED);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilBuildPJLFolder
  PURPOSE:  Construct the path to the ProjectLink folder, given the 
			server name, project name, and folder name in the project.
\*====================================================================*/
ProError PTWCUtilBuildPJLFolder (wchar_t* server, wchar_t* project, 
										char* foldername, ProPath folder)
{
	ProPath w_foldername;

	ProStringToWstring (folder, "wtpub://");
	ProWstringConcatenate (server, folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (L"/Projects/", folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (project, folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (L"/", folder, PRO_VALUE_UNUSED);
	ProStringToWstring (w_foldername, foldername);
	ProWstringConcatenate (w_foldername, folder, PRO_VALUE_UNUSED);

	return  PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilBuildServerFolder
  PURPOSE:  Construct a commonspace folder using a location.
\*====================================================================*/
ProError PTWCUtilBuildServerFolder (wchar_t* server, 
									wchar_t* folderpath, ProPath folder)
{
	ProPath w_foldername;

	ProStringToWstring (folder, "wtpub://");
	ProWstringConcatenate (server, folder, PRO_VALUE_UNUSED);
	ProWstringConcatenate (folderpath, folder, PRO_VALUE_UNUSED);

	return  PRO_TK_NO_ERROR;
}

/*====================================================================*\
  FUNCTION: PTWCUtilCheckinWSToFolder
  PURPOSE:  Checkin the entire workspace.  If folder is supplied it
			will be used as the new object "default location".
\*====================================================================*/
extern ProError PTWCUtilCheckinWSToFolder (wchar_t* ws_name, ProPath folder)
{
	ProServerCheckinOptions options;
	ProServerCheckinConflicts conflicts = NULL;

	status = ProServercheckinoptsAlloc (&options);
	PT_TEST_LOG_SUCC ("ProServercheckinoptsAlloc()");

	if (folder != NULL)
	{
		status = ProServercheckinoptsDeflocationSet (options, folder);
		PT_TEST_LOG_SUCC ("ProServercheckinoptsDeflocationSet()");
	}

	status = ProServerObjectsCheckin (NULL, options, &conflicts);
	PT_TEST_LOG_SUCC ("ProServerObjectsCheckin()");

	if (status != PRO_TK_NO_ERROR && conflicts != NULL)
	{
		PTWCUtilConflictReport (conflicts);
		ProServerconflictsFree (conflicts);
	}

	return PRO_TK_NO_ERROR;
}

uiCmdAccessState PTWCUtilAccessIfWCServer ()
{
	wchar_t* server;
	wchar_t* server_class;
	int result;

	status = ProServerActiveGet (&server);

	if (status != PRO_TK_NO_ERROR)
		return ACCESS_REMOVE;

	status = ProServerClassGet (server, &server_class);
	
	ProWstringCompare (L"Windchill", server_class, PRO_VALUE_UNUSED, &result);

	ProWstringFree (server);
	ProWstringFree (server_class);

	if (result != 0)
		return ACCESS_REMOVE;

	return ACCESS_AVAILABLE;
}

void PTWCUtilWstringToLower (wchar_t* str)
{
	char buff [1000];
	char* result;
	int i = 0;

	ProWstringToString (buff, str);


    while ( buff[i] != '\0')
    {
        buff[i] = (char)tolower(buff[i]);
        i++;
    }

	ProStringToWstring  (str, buff);
}