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


/*-------------------------------------------------------------------------*\
			  Pro/TOOLKIT Include Files
\*-------------------------------------------------------------------------*/
#include "ProToolkit.h"
#include "ProUtil.h"
#include "ProWstring.h"
#include "ProArray.h"
#include "ProMenu.h"
#include "TestError.h"
#include "ProMenuBar.h"
#include <PTApplsUnicodeUtils.h>
/*-------------------------------------------------------------------------*\
			 Global and static variables
\*-------------------------------------------------------------------------*/
static ProName MSGFILE = {'m', 's', 'g', '.', 't', 'x', 't', 0};

/*=========================================================================*\
FUNCTION    : 	UserEditFile()			
\*=========================================================================*/
int UserEditFile( void * arg1,  int arg2)
{
    int status;

    /* Variables used to setup file selection */
    ProName		wlabel; 
    ProLine		wfilter;
    ProPath	*	p_short_path;
    ProName 	*	p_short_label;
    ProPath		tmppath1, tmppath2;
    ProName		tmplabel;

    /* The selected file */ 
    ProPath		w_file_sel;

    /* Variables representing the selected file after parsing */
    ProPath		file_path;
    ProMdlName	file_name;
    ProMdlExtension	file_ext;
    int			file_ver;

    /* New File after release update */
    ProPath		w_file_new;
    ProCharLine		file_sel, file_new;

    /* CharLine representing the command passed to the OS */
    ProCharLine		sys_cmd;


    /*-----------------------------------------------------------------------*\
      Prepare the file selection 
    \*-----------------------------------------------------------------------*/ 
    ProStringToWstring(wlabel, "Open Text File");
    ProStringToWstring(wfilter , "*txt*"); 

    status = ProArrayAlloc(0, sizeof(ProPath), 1, (ProArray*) &p_short_path);
    ERROR_CHECK("UserEditFile","ProArrayAlloc",status);
    if (status != PRO_TK_NO_ERROR) return status;

    status = ProArrayAlloc(0, sizeof(ProName), 1, (ProArray*) &p_short_label);
    ERROR_CHECK("UserEditFile","ProArrayAlloc",status);
    if (status != PRO_TK_NO_ERROR) return status;

    /*-----------------------------------------------------------------------*\
      First Shortcut path points to directory containing appl executable
    \*-----------------------------------------------------------------------*/ 
    status = ProToolkitApplExecPathGet(tmppath1);
    ERROR_CHECK("UserEditFile","ProToolkitApplTextPathGet",status);
    if (status != PRO_TK_NO_ERROR) return status;

    status = ProFileMdlnameParse(tmppath1, tmppath2, NULL,NULL,NULL);
    ERROR_CHECK("UserEditFile","ProFileMdlnameParse",status);
    if (status != PRO_TK_NO_ERROR) return status;

    status = ProArrayObjectAdd((ProArray*) &p_short_path, PRO_VALUE_UNUSED,
				1, &tmppath2);
    ERROR_CHECK("UserEditFile","ProWstringArrayObjectAdd",status);
    if (status != PRO_TK_NO_ERROR) return status;

    ProStringToWstring(tmplabel, "App Exec Path");
    status = ProArrayObjectAdd((ProArray*) &p_short_label ,PRO_VALUE_UNUSED, 
				1, &tmplabel);
    ERROR_CHECK("UserEditFile","ProWstringArrayObjectAdd",status);
    if (status != PRO_TK_NO_ERROR) return status;

    /*-----------------------------------------------------------------------*\
      Second Shortcut path points to application text directory
    \*-----------------------------------------------------------------------*/ 
    status = ProToolkitApplTextPathGet(tmppath1);
    ERROR_CHECK("UserEditFile","ProToolkitApplTextPathGet",status);
    if (status != PRO_TK_NO_ERROR) return status;

    status = ProArrayObjectAdd((ProArray*) &p_short_path, PRO_VALUE_UNUSED,
				1, &tmppath1);
    ERROR_CHECK("UserEditFile","ProWstringArrayObjectAdd",status);
    if (status != PRO_TK_NO_ERROR) return status;

    ProStringToWstring(tmplabel, "App Text Path");
    status = ProArrayObjectAdd((ProArray*) &p_short_label ,PRO_VALUE_UNUSED, 
				1, &tmplabel);
    ERROR_CHECK("UserEditFile","ProWstringArrayObjectAdd",status);
    if (status != PRO_TK_NO_ERROR) return status;

    /*-----------------------------------------------------------------------*\
      Select a file 
    \*-----------------------------------------------------------------------*/ 
    status = ProFileMdlnameOpen(wlabel, wfilter, p_short_path, p_short_label, 
			NULL, NULL, w_file_sel);
    ERROR_CHECK("UserEditFile","ProFileMdlnameOpen",status);
    if (status != PRO_TK_NO_ERROR) return status;
    
    /*-----------------------------------------------------------------------*\
    Free Memory  that is not necessary any longer
    \*-----------------------------------------------------------------------*/ 
    status = ProArrayFree((ProArray*)&p_short_path);
    ERROR_CHECK("UserEditFile","ProArrayFree",status);
    if (status != PRO_TK_NO_ERROR) return status;

    status = ProArrayFree((ProArray*)&p_short_label);
    ERROR_CHECK("UserEditFile","ProArrayFree",status);
    if (status != PRO_TK_NO_ERROR) return status;
    
    /*-----------------------------------------------------------------------*\
    Create the new file name, with incremented release number 
    \*-----------------------------------------------------------------------*/ 
    status = ProFileMdlnameParse(w_file_sel, 
			file_path, file_name, file_ext, &file_ver);
    ERROR_CHECK("UserEditFile","ProFileMdlnameParse",status);
    if (status != PRO_TK_NO_ERROR) return status;

    if (file_ver < 0) 
	file_ver = 1;
    else ++file_ver;

    status = ProPathMdlnameCreate (file_path, 
		file_name, file_ext, file_ver, w_file_new);
    ERROR_CHECK("UserEditFile","ProPathMdlnameCreate",status);
    if (status != PRO_TK_NO_ERROR) return status;

    /*-----------------------------------------------------------------------*\
    Make a system copy of the file to edit
    \*-----------------------------------------------------------------------*/ 
    ProWstringToString (file_sel, w_file_sel);
    ProWstringToString (file_new, w_file_new);
    ProTKSprintf(sys_cmd, "cp %s %s", file_sel, file_new); 
    system(sys_cmd);

    /*-----------------------------------------------------------------------*\
    Edit the new file 
    \*-----------------------------------------------------------------------*/ 
    status = ProFileEdit(w_file_new);
    ERROR_CHECK("UserEditFile","ProFileEdit",status);
    if (status != PRO_TK_NO_ERROR) return status;

    return (PRO_TK_NO_ERROR);
}