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


/*--------------------------------------------------------------------*\
Pro/Toolkit includes
\*--------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProMenuBar.h>
#include <ProMdl.h>
#include <ProSelection.h>
#include <ProFeature.h>
#include <ProWstring.h>
/*--------------------------------------------------------------------*\
Application global/external data
\*--------------------------------------------------------------------*/
static wchar_t  MSGFIL[] = {'u','t','i','l','i','t','i','e','s','.','t','x','t','\0'};

/*=====================================================================*\
FUNCTION: PTUtilElemtreeXMLShow
PURPOSE:  Show an XML file in the embedded browser.
Note: native XML support in the browser is only available in Microsoft
Internet Explorer.	
\*=====================================================================*/
ProError PTUtilElemtreeXMLShow (ProFileName filename)
{
	ProPath win_url;
	ProPath current_dir;
	int win_id;

	ProStringToWstring (win_url, "file://");

	status = ProDirectoryCurrentGet (current_dir);
	
	status = ProWstringConcatenate (current_dir, win_url, PRO_VALUE_UNUSED);

	status = ProWstringConcatenate (filename, win_url, PRO_VALUE_UNUSED);

	status = ProWindowCurrentGet (&win_id);

	status = ProWindowURLShow (win_id, win_url);

	return PRO_TK_NO_ERROR;
}

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite
PURPOSE:  Write a feature's element tree to an XML file.	
\*=====================================================================*/
int UserFeatXMLWrite (ProFeature* feat)
{
  ProElement elemtree;
  wchar_t wFilename [PRO_FILE_NAME_SIZE];

/*--------------------------------------------------------------------*\
Prompt for the filename
\*--------------------------------------------------------------------*/
  ProMessageDisplay ( MSGFIL, "USER Enter the XML file name:");

  if (ProMessageStringRead (PRO_FILE_NAME_SIZE, wFilename) != PRO_TK_NO_ERROR)
    return (0);

/*--------------------------------------------------------------------*\
Extract the element tree and convert it to XML
\*--------------------------------------------------------------------*/
  status = ProFeatureElemtreeExtract (feat,NULL,PRO_FEAT_EXTRACT_NO_OPTS,&elemtree);

  if ( status == PRO_TK_NO_ERROR )
  {
    status = ProElemtreeWrite (elemtree, PRO_ELEMTREE_XML, wFilename);

   ProFeatureElemtreeFree(feat, elemtree);
  }

  if (status == PRO_TK_NO_ERROR )
  {
	  PTUtilElemtreeXMLShow(wFilename);
  }

  return status;
}

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite_AO
PURPOSE:  Action/Object callback for feature export to XML.	
\*=====================================================================*/
static int UserFeatXMLWrite_AO()
{
  ProError status;
  ProSelection* sel_array;
  int n_sels;
  ProFeature feat;

/*--------------------------------------------------------------------*\
Select the feature for XML info
\*--------------------------------------------------------------------*/
  ProMessageDisplay ( MSGFIL, "USER Select feature:");
  
  status = ProSelect ("feature,membfeat", 1, NULL, NULL, NULL, NULL, &sel_array, &n_sels);
  if (status != PRO_TK_NO_ERROR)
    return (0);

  status = ProSelectionModelitemGet (sel_array [0], &feat);

  UserFeatXMLWrite (&feat);

  return (1);
}

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite_OA
PURPOSE:  Object/Action callback for feature export to XML.	
\*=====================================================================*/
int UserFeatXMLWrite_OA()
{
  ProError status; 
  ProSelection* sel_array;
  ProFeature feat;

/*--------------------------------------------------------------------*\
Extract the current selected feature
\*--------------------------------------------------------------------*/
  status = ProSelbufferSelectionsGet (&sel_array);

  status = ProSelectionModelitemGet (sel_array [0], &feat);

  UserFeatXMLWrite (&feat);

  ProSelectionarrayFree (sel_array);

  return (1);
}

#define OA 1  /* Standard OA in the menu system, typically gray out 
		 the button when not usable*/
#define PM 2 /* OA in a popup menu, typically remove the button when 
	         not usable */

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite_TestLow
PURPOSE:  Access function for the button for feature export to XML.	
\*=====================================================================*/
static uiCmdAccessState UserFeatXMLWrite_TestLow (ProSelection* sels, int mode)
{
  uiCmdAccessState access_result;
  ProBoolean should_free = PRO_B_FALSE;
  ProError status;
  int size;

/*-----------------------------------------------------------------*\
  Set the default return if the button is unusable.
\*-----------------------------------------------------------------*/  
  if (mode == OA)
    access_result = ACCESS_UNAVAILABLE;
  else 
    access_result = ACCESS_REMOVE;

/*-----------------------------------------------------------------*\
  If called without selections, extract the current selections from
  the buffer.
\*-----------------------------------------------------------------*/ 
  if (sels == NULL)
    {   
      status = ProSelbufferSelectionsGet (&sels);
      
      if (status != PRO_TK_NO_ERROR)
	return access_result;

      if (sels == NULL)
	return access_result;

      should_free = PRO_B_TRUE;
    }

/*-----------------------------------------------------------------*\
  This command allows only one selection.
\*-----------------------------------------------------------------*/   
  status = ProArraySizeGet (sels, &size);
  
  if (status != PRO_TK_NO_ERROR)
    return access_result;

  if (size == 1)
    {
      ProFeature feature;

      status = ProSelectionModelitemGet (sels [0], &feature);

/*-----------------------------------------------------------------*\
  If the selected type is feature, its element tree must be extractable.
\*-----------------------------------------------------------------*/   
      if (feature.type == PRO_FEATURE)
	{
	  ProElement element;

          status = ProFeatureElemtreeExtract (&feature, NULL, PRO_FEAT_EXTRACT_NO_OPTS,
			&element);
      
	  if (status == PRO_TK_NO_ERROR)
	    {
	      ProFeatureElemtreeFree (&feature, element);
	      access_result = ACCESS_AVAILABLE;
	    }	  
	}
    }

  if (should_free)
    ProSelectionarrayFree (sels);
  
  return access_result;
}

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite_TestMdlTree
PURPOSE:  Access function for the model tree popup menu button.	
\*=====================================================================*/
uiCmdAccessState UserFeatXMLWrite_TestMdlTree (uiCmdAccessMode mode)
{ 
  return UserFeatXMLWrite_TestLow (NULL, PM);
}

/*=====================================================================*\
FUNCTION: UserFeatXMLWrite_TestPM
PURPOSE:  Access function for the graphics window popup menu button.	
\*=====================================================================*/
uiCmdAccessState UserFeatXMLWrite_TestPM (uiCmdCmdId id,
				      ProAppData data,
				      ProSelection* sels)
{ 
  return UserFeatXMLWrite_TestLow (sels, PM);
}

#undef OA
#undef PM