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


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProUtil.h>
#include <ProMessage.h>
#include <UtilFiles.h>
/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>

/*--------------------------- Global Definitions ----------------------------*/
#define MSGFIL "msg.txt"

/*================================================================*\
FUNCTION  : UserUtilNameTypeGet()
PURPOSE   : Get the handle to a model by prompting the user for
	    a name and a type
\*================================================================*/
ProError UserUtilNameTypeGet( 
	ProFileName 	wmsgfil, 
	ProFamilyMdlName 	name, 
	ProMdlType 	*type )
{
  int 		status;
  ProCharLine	astr;
  ProMdlExtension	type_wname;
  char	type_name[PRO_MDLEXTENSION_SIZE];

  status = ProMessageDisplay(wmsgfil,"USER %0s", "Enter a model name : ");
  ERROR_CHECK("UserUtilNameTypeGet","ProMessageDisplay",status);
  if (status != PRO_TK_NO_ERROR) return(status);
  
  status = ProMessageStringRead(PRO_FAMILY_MDLNAME_SIZE, name);
  ERROR_CHECK("UserUtilNameTypeGet","ProMessageStringRead",status);
  if (status != PRO_TK_NO_ERROR) return(status);

  status = ProMessageDisplay(wmsgfil,"USER %0s", "Enter a model type : ");
  ERROR_CHECK("UserUtilNameTypeGet","ProMessageDisplay",status);
  if (status != PRO_TK_NO_ERROR) return(status);

  status = ProMessageStringRead(PRO_MDLEXTENSION_SIZE, type_wname);
  ERROR_CHECK("UserUtilNameTypeGet","ProMessageStringRead",status);
  if (status != PRO_TK_NO_ERROR) return(status);

  ProWstringToString(type_name,type_wname);

  (*type) = ProUtilGetProType(type_name);

  return (PRO_TK_NO_ERROR);
}

/*=====================================================================*\
   Function:    user_string_to_lower() 
   Purpose:     Convert a string to lower case
\*=====================================================================*/
void UserUtilStringToLower(char *input_string, char *output_string)
{
 int i = 0;
    
  while ( input_string[i] != '\0')
   { 
    output_string[i] = tolower(input_string[i]);
    i++;
   } 
     
  output_string[i] = '\0';
}

/*===========================================================================*\
   Function:    UserUtilModelNameType()
   Purpose:     Convert an object name to lower case
\*===========================================================================*/
char *UserUtilModelNameType(ProMdl p_obj, char *lname, char *ltype)
{
        ProError   status = PRO_TK_NO_ERROR;
        ProMdldata modeldata;
        char       astr1[PRO_MDLNAME_SIZE], astr2[PRO_MDLEXTENSION_SIZE];
		ProMdlName modelName;
		ProMdlExtension modelExtension;

        if(p_obj == NULL)
            status = ProMdlCurrentGet(&p_obj);


		status = ProMdlMdlnameGet(p_obj, modelName);   
		if (status != PRO_TK_NO_ERROR) return (NULL);
	
		status = ProMdlExtensionGet(p_obj, modelExtension);   
		if (status != PRO_TK_NO_ERROR) return (NULL);

        if( lname != NULL )
           UserUtilStringToLower(ProWstringToString(astr1, modelName),
lname);
        if( ltype != NULL )
           UserUtilStringToLower(ProWstringToString(astr2, modelExtension),
ltype);

        return(lname);
}

/*===========================================================================*\
   Function:    UserUtilGenFilename()
   Purpose:     Generate a name for an output file
\*===========================================================================*/
char *UserUtilGenFilename(ProMdl p_obj, char *filext, char *filename)
{
        ProError status = PRO_TK_NO_ERROR;
        char model_name[PRO_MDLNAME_SIZE], model_type[PRO_MDLEXTENSION_SIZE];

        if(p_obj == NULL)
            status = ProMdlCurrentGet(&p_obj);

        if(p_obj != NULL)
        {
            UserUtilModelNameType(p_obj, model_name,  model_type);
            strcpy(filename,model_name);
        }
        else
            strcpy(filename,"nomodel");
        strcat(filename,filext);

        return(filename);
}