/*
	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 <ProMenuBar.h>
#include <ProMode.h>

#include <PTWCServer.h>

/*====================================================================*\
  FUNCTION: PTWCCreateOrUpdateDXF
  PURPOSE:  Create or update a DXF copy of the active drawing.  If the 
            copy is already in the project, it will be checked out and 
			updated.  If the file is new, it will be created in "Plans".
\*====================================================================*/
int PTWCCreateOrUpdateDXF ()
{
	ProMdl model;
	ProMdlType type;
	ProMdlFileName filename;
	ProMdlName modelname;
	ProPath filepath, ws_path;
	wchar_t* server;
	wchar_t* ws_name;
	wchar_t* context;
	wchar_t* model_url = NULL;
	ProServerCheckoutConflicts conflicts;
	ProPath folder;

/*--------------------------------------------------------------------*\
	Obtain current drawing handle and export to DXF
\*--------------------------------------------------------------------*/
	status = ProMdlCurrentGet (&model);
	PT_TEST_LOG_SUCC ("ProMdlCurrentGet()");

	status = ProMdlMdlnameGet (model, modelname);
	PT_TEST_LOG_SUCC ("ProMdlMdlnameGet()");

	ProWstringCopy (modelname, filename, PRO_VALUE_UNUSED);
	PTWCUtilWstringToLower (filename);
	ProWstringConcatenate (L"_drw.dxf", filename, PRO_VALUE_UNUSED);

	status = ProOutputFileMdlnameWrite (model, filename, PRO_DXF_FILE, NULL, NULL, NULL, NULL);
	PT_TEST_LOG_SUCC ("ProOutputFileMdlnameWrite()");

/*--------------------------------------------------------------------*\
	If the file already exists in the project, check it out by its 
	object name.
\*--------------------------------------------------------------------*/
	status = ProServerObjectsCheckout (NULL, filename, PRO_B_TRUE, 
									NULL, &model_url, &conflicts);
	PT_TEST_LOG_SUCC ("ProServerObjectsCheckout()");

/*--------------------------------------------------------------------*\
	A conflict most likely indicates that the file does not exist
	in the project already.  This error is ignored.
\*--------------------------------------------------------------------*/
	if (status == PRO_TK_CHECKOUT_CONFLICT)
	{
		ProServerconflictsFree (conflicts);
	}

/*--------------------------------------------------------------------*\
	Get the full disk path to the output DXF file as the source of the 
	File Copy operation.
\*--------------------------------------------------------------------*/
	status = ProDirectoryCurrentGet (filepath);
	PT_TEST_LOG_SUCC ("ProDirectoryCurrentGet()");

	ProWstringConcatenate (filename, filepath, PRO_VALUE_UNUSED);

/*--------------------------------------------------------------------*\
	Build the workspace path as the target of the File Copy operation.
\*--------------------------------------------------------------------*/
	status = ProServerActiveGet (&server);
	PT_TEST_LOG_SUCC ("ProServerActiveGet()");

	status = ProServerContextGet (server, &context);
	PT_TEST_LOG_SUCC ("ProServerContextGet()");

	status = ProServerWorkspaceGet (server, &ws_name);
	PT_TEST_LOG_SUCC ("ProServerWorkspaceGet()");

	PTWCUtilBuildWSPath (server, ws_name, ws_path);

/*--------------------------------------------------------------------*\
	Copy the file to the workspace as a top-level object.
\*--------------------------------------------------------------------*/
	status = ProFileCopyToWS (filepath, ws_path, NULL);
	PT_TEST_LOG_SUCC ("ProFileCopyToWS()");

/*--------------------------------------------------------------------*\
	Checkin the workspace.  If the object is new, it will go to
	the Plans folder.
\*--------------------------------------------------------------------*/
	PTWCUtilBuildPJLFolder (server, context, "Plans", folder);
	PTWCUtilCheckinWSToFolder (ws_name, folder);

	ProWstringFree (server);
	ProWstringFree (context);
	ProWstringFree (ws_name);
	ProWstringFree (model_url);

	return 0;
}

/*====================================================================*\
  FUNCTION: PTWCAccessCreateOrUpdateDXF
  PURPOSE:  The access function for the create or update DXF command.
\*====================================================================*/
uiCmdAccessState PTWCAccessCreateOrUpdateDXF (uiCmdAccessMode m)
{
	ProMode mode;
	wchar_t* server;
	wchar_t* server_class;
	int result;

/*--------------------------------------------------------------------*\
	This command accessible only in drawing mode.
\*--------------------------------------------------------------------*/
	status = ProModeCurrentGet (&mode);
	
	if (mode != PRO_MODE_DRAWING)
		return ACCESS_REMOVE;

/*--------------------------------------------------------------------*\
	Ensure that there is an active Windchill server
\*--------------------------------------------------------------------*/
	return (PTWCUtilAccessIfWCServer ());
}