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




#include <ProToolkit.h>
#include <ProFeature.h>
#include <ProAnnotationElem.h>
#include <ProAnnotationFeat.h>
#include <ProWindows.h>
#include <ProUtil.h>

#include <PTUDFExamples.h>
#include <PTApplsUnicodeUtils.h>

static ProBoolean append_to_file;

/*====================================================================*\
FUNCTION: PTUDFExAEParamsWrite
PURPOSE:  Write AE params to a file.
\*====================================================================*/
static ProError PTUDFExAEParamsWrite (ProAnnotationElem* elem,
			       PTUDFExParamInfoFormat format,
			       wchar_t* file_name)
{
  status = PTUDFExParamInfoWrite (elem, format, file_name, append_to_file,
				  NULL, NULL);

  append_to_file = PRO_B_TRUE;

  return (PRO_TK_NO_ERROR);
}

/*====================================================================*\
FUNCTION: PTUDFExAEParamsWriteTxt
PURPOSE:  Write AE params to a file using text based format.
\*====================================================================*/
ProError PTUDFExAEParamsWriteTxt (ProAnnotationElem* elem,
				  ProError filter_status,
				  ProAppData app_data)
{
  wchar_t* filename = (wchar_t*) app_data;

  status = PTUDFExAEParamsWrite (elem, PT_UDF_EX_PARAM_INFO_TEXT, filename);

 /* Write constraints on the AE parameters */
  PTUDFExAEConstraintsWrite (elem, filename, PRO_B_TRUE);

  return (status);
}

/*====================================================================*\
FUNCTION: PTUDFExAEParamsWriteCSV
PURPOSE:  Write AE params to a file using CSV based format.
\*====================================================================*/
ProError PTUDFExAEParamsWriteCSV (ProAnnotationElem* elem,
				  ProError filter_status,
				  ProAppData app_data)
{
  wchar_t* filename = (wchar_t*) app_data;

  return (PTUDFExAEParamsWrite (elem, PT_UDF_EX_PARAM_INFO_CSV, filename));
}

/*====================================================================*\
FUNCTION: PTUDFExPostLibraryEditAction
PURPOSE:  Notification function to write AE param info when a .gph file is
	      created or modified.
\*====================================================================*/
ProError PTUDFExPostLibraryEditAction(ProName name, ProFeature* features)
{
  ProFileName file_name;
  ProFileName csv_file_name;
  int num_feats;
  ProPath dir_path, path;
  int window_id;
  int i;

  ProWstringCopy (name, file_name, PRO_VALUE_UNUSED);
  ProWstringCopy (name, csv_file_name, PRO_VALUE_UNUSED);

  ProWstringConcatenate (L".txt", file_name, PRO_VALUE_UNUSED);
  ProWstringConcatenate (L".csv", csv_file_name, PRO_VALUE_UNUSED);

/*----------------------------------------------------------------------*\
	Features would be NULL if the .gph file is Standalone and being 
	modified without its reference model in session.
\*----------------------------------------------------------------------*/
  if (features != NULL)
    {
      status = ProArraySizeGet (features, &num_feats);
      PT_TEST_LOG_SUCC ("ProArraySizeGet()");

      append_to_file = PRO_B_FALSE;
      for (i = 0; i < num_feats; i++)
		{
		status = ProFeatureAnnotationelemsVisit (&features [i], 
						   PTUDFExAEParamsWriteTxt,
						   NULL,	
						   file_name);
		}	

      append_to_file = PRO_B_FALSE;
      for (i = 0; i < num_feats; i++)
		{
	  status = ProFeatureAnnotationelemsVisit (&features [i], 
						   PTUDFExAEParamsWriteCSV,
						   NULL,	
						   csv_file_name);
		}
    }

  if (show_ui)
    {
/*----------------------------------------------------------------------*\
	Show the text results in the browser window for review.
\*----------------------------------------------------------------------*/
      ProDirectoryCurrentGet (dir_path);
      
      ProStringToWstring (path, "file://");
      
      ProWstringConcatenate (dir_path, path, 
			     PRO_VALUE_UNUSED);
      
      ProWstringConcatenate (file_name, path, PRO_VALUE_UNUSED);
      
      ProWindowCurrentGet (&window_id);
      
      ProWindowURLShow(window_id, path);
      PT_TEST_LOG_SUCC ("ProWindowURLShow()");
    }
  
  return (PRO_TK_NO_ERROR);
}