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


/*----------------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*----------------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProMenu.h>
#include <ProMdl.h>
#include <ProImm.h>
#include <ProArray.h>
#include <ProUtil.h>
#include <ProTKRunTime.h>	

/*----------------------------------------------------------------------------*\
C System includes
\*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*\
Application includes
\*----------------------------------------------------------------------------*/
#include "UtilMessage.h"
#include "UtilMenu.h"
#include "UtilFiles.h"
#include "TestError.h"
#include "PTApplsUnicodeUtils.h"

/*----------------------------------------------------------------------------*\
Application macros
\*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*\
Application prototypes
\*----------------------------------------------------------------------------*/
int     ProTestImmInfo(ProMdl );
int     ProTestImmCreate(ProMdl );
int     ProTestImmRedefine(ProMdl );

/*----------------------------------------------------------------------------*\
Application global/external data
\*----------------------------------------------------------------------------*/

/*============================================================================*\
    FUNCTION : ProTestImm	
    PURPOSE  : Setup menu for testing
\*============================================================================*/
ProError ProTestImm(ProMdl mdl)
{
    ProError err;
    int menu_id, action;
    ProImmParams imm_par = NULL;
        
    err = ProMenuFileRegister((char *)"TkImm",(char *)"tkimm.mnu", &menu_id);
    err = ProMenubuttonActionSet((char *)"TkImm", (char *)"TkImm",
	(ProMenubuttonAction)ProMenuDelete, NULL, 0);
    err = ProMenubuttonActionSet((char *)"TkImm", (char *)"Add",
	(ProMenubuttonAction)ProTestImmCreate, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkImm", (char *)"Info",
	(ProMenubuttonAction)ProTestImmInfo, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkImm", (char *)"Replace",
	(ProMenubuttonAction)ProTestImmRedefine, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkImm", (char *)"Done",
	(ProMenubuttonAction)ProMenuDelete, NULL, 0);

    err = ProMenuCreate(PROMENUTYPE_MAIN, (char *)"TkImm", NULL);
    
    err = ProImmFromModelGet (mdl, &imm_par);
    TEST_CALL_REPORT("ProImmFromModelGet()", "ProTestImmCreate()", 
	err, (err != PRO_TK_NO_ERROR && err != PRO_TK_E_NOT_FOUND));
    
    if (err == PRO_TK_E_NOT_FOUND)
    {
        err = ProMenubuttonDeactivate ((char *)"TkImm", (char *)"Replace");
        err = ProMenubuttonDeactivate ((char *)"TkImm", (char *)"Info");
    }    
    else
        err = ProMenubuttonDeactivate ((char *)"TkImm", (char *)"Add");
        
    err = ProMenuProcess((char *)"", &action);
    
    return (PRO_TK_NO_ERROR);
}

/*============================================================================*\
    FUNCTION : ProTestImmCreate 	
    PURPOSE  : Create new IMM.	
\*============================================================================*/
int ProTestImmCreate (ProMdl mdl)
{
    ProError err;
    ProImmParams imm_par = NULL;
    ProName  machine_name;
    
    
    err =  ProImmParamsAlloc(&imm_par);
    TEST_CALL_REPORT("ProImmParamsAlloc()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
            
    ProStringToWstring (machine_name, (char *)"Access_220_725");
    err = ProImmParamsDefaultSet (machine_name, imm_par);
    TEST_CALL_REPORT("ProImmParamsDefaultSet()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
   
    err = ProImmDialog (mdl, imm_par);
    TEST_CALL_REPORT("ProImmDialog()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
    if (err == PRO_TK_NO_ERROR) 
    {       
        err = ProImmCreate (imm_par);
        TEST_CALL_REPORT("ProImmCreate()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
    }
    err = ProImmParamsFree (&imm_par);
    TEST_CALL_REPORT("ProImmParamsFree()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
            
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestImmRedefine	
    PURPOSE  : Redefine existing IMM	
\*============================================================================*/
int ProTestImmRedefine (ProMdl mdl)
{
    ProError err;
    ProImmParams imm_par = NULL;
    
    err = ProImmFromModelGet (mdl, &imm_par);
    TEST_CALL_REPORT("ProImmFromModelGet()",
            "ProTestImmRedefine()", err, err != PRO_TK_NO_ERROR);
    if (err != PRO_TK_NO_ERROR)
        return (-1);
        
    err = ProImmDialog (mdl, imm_par);
    TEST_CALL_REPORT("ProImmDialog()",
            "ProTestImmRedefine()", err, err != PRO_TK_NO_ERROR);
    if (err == PRO_TK_NO_ERROR) 
    {       
        err = ProImmCreate (imm_par);
        TEST_CALL_REPORT("ProImmCreate()",
            "ProTestImmRedefine()", err, err != PRO_TK_NO_ERROR);
    }
    err = ProImmParamsFree (&imm_par);
    TEST_CALL_REPORT("ProImmParamsFree()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);
    
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestImmInfo	
    PURPOSE  : Get IMM info	
\*============================================================================*/
int ProTestImmInfo (ProMdl mdl)
{
    ProError err;
    ProImmParams imm_par = NULL;
    ProName       machine_name, *p_param_values = NULL;
    ProCharName *p_param_names = NULL;
    ProCharLine line, value;
    char f_name[PRO_FILE_NAME_SIZE];
    FILE *fp;
    int n_params, i;
    
    ProTestQcrName(&mdl, (char *)".inf", f_name);
    fp = PTApplsUnicodeFopen (f_name, "w");
    if (fp == NULL)
        return -1;
    
    
    err = ProImmFromModelGet (mdl, &imm_par);
    TEST_CALL_REPORT("ProImmFromModelGet()",
            "ProTestImmInfo()", err, err != PRO_TK_NO_ERROR);
    if (err != PRO_TK_NO_ERROR)
        return (-1);
    
    err = ProArrayAlloc (0, sizeof(ProCharName), 1, (ProArray*)&p_param_names);
    err = ProArrayAlloc (0, sizeof(ProName), 1, (ProArray*)&p_param_values);
    
    err = ProImmParamsGet (imm_par, machine_name, &p_param_names, 
        &p_param_values );
    TEST_CALL_REPORT("ProImmParamsGet()",
            "ProTestImmInfo()", err, err != PRO_TK_NO_ERROR);
            
    ProTKFprintf (fp, "\nMachine_name: %s\n", 
        ProWstringToString(line, machine_name));
    err = ProArraySizeGet ((ProArray)p_param_names, &n_params);
    for (i = 0; i < n_params; i++)
    {
         strcpy(line, p_param_names[i]);
         ProWstringToString(value, p_param_values[i]);
         ProTKFprintf(fp, "    %-20s %s\n", line, value); 
    }
    err = ProArrayFree ((ProArray*)&p_param_values);    
    err = ProArrayFree ((ProArray*)&p_param_names);
    err = ProImmParamsFree (&imm_par);
    TEST_CALL_REPORT("ProImmParamsFree()",
            "ProTestImmCreate()", err, err != PRO_TK_NO_ERROR);

    fclose (fp);
    
    ProStringToWstring(machine_name, f_name);
    ProInfoWindowDisplay(machine_name, NULL, NULL); 
    
    return (0);
}