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



/*----------------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*----------------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProMenu.h>
#include <ProMfg.h>
#include <ProMdl.h>
#include <ProCsys.h>
#include <ProMoldbase.h>
#include <ProArray.h>
#include <ProUtil.h>

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

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

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

/*----------------------------------------------------------------------------*\
Application prototypes
\*----------------------------------------------------------------------------*/
int     ProTestMoldBaseInfo(ProMdl );
int     ProTestMoldBaseCreate(ProMdl );
int     ProTestMoldBaseRedefine(ProMdl );

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

/*============================================================================*\
    FUNCTION : ProTestMoldBase	
    PURPOSE  : Setup menu for testing
\*============================================================================*/
int ProTestMoldBase(ProMdl mdl)
{
    ProError err;
    int menu_id, action;
    ProMoldbaseParams mb_par = NULL;
        
    err = ProMenuFileRegister((char *)"TkMoldBase",(char *)"tkmoldbase.mnu", &menu_id);
    err = ProMenubuttonActionSet((char *)"TkMoldBase", (char *)"TkMoldBase",
	(ProMenubuttonAction)ProMenuDelete, NULL, 0);
    err = ProMenubuttonActionSet((char *)"TkMoldBase", (char *)"Add",
	(ProMenubuttonAction)ProTestMoldBaseCreate, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkMoldBase", (char *)"Info",
	(ProMenubuttonAction)ProTestMoldBaseInfo, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkMoldBase", (char *)"Replace",
	(ProMenubuttonAction)ProTestMoldBaseRedefine, mdl, 0);
    err = ProMenubuttonActionSet((char *)"TkMoldBase", (char *)"Done",
	(ProMenubuttonAction)ProMenuDelete, NULL, 0);
    err = ProMenuCreate(PROMENUTYPE_MAIN, (char *)"TkMoldBase", NULL);
       
    err = ProMoldbaseFromModelGet (mdl, &mb_par);
    TEST_CALL_REPORT("ProMoldbaseFromModelGet()", "ProTestMoldBase()", 
	err, (err != PRO_TK_NO_ERROR && err != PRO_TK_E_NOT_FOUND));
    if (err == PRO_TK_E_NOT_FOUND)
    {
        err = ProMenubuttonDeactivate ((char *)"TkMoldBase", (char *)"Info");
        err = ProMenubuttonDeactivate ((char *)"TkMoldBase", (char *)"Replace");
    }    
    else
        err = ProMenubuttonDeactivate ((char *)"TkMoldBase", (char *)"Add");
       
    err = ProMenuProcess((char *)"", &action);
    
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestMoldBaseCreate 	
    PURPOSE  : Creat mold base.	
\*============================================================================*/
int ProTestMoldBaseCreate (ProMdl mdl)
{
    ProError err;
    ProMoldbaseParams mb_par = NULL;
    ProName vendor, series, length, width;
    
    ProStringToWstring (vendor, (char *)"Futaba");
    ProStringToWstring (series, (char *)"SA");
    ProStringToWstring (length, (char *)"150");
    ProStringToWstring (width,  (char *)"250");
    
    err =  ProMoldbaseParamsAlloc(&mb_par);
    TEST_CALL_REPORT("ProMoldbaseParamsAlloc()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
            
    err = ProMoldbaseParamsDefaultSet (vendor, series, length, width, mb_par);
    TEST_CALL_REPORT("ProMoldbaseParamsDefaultSet()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
            
    err = ProMoldbaseDialog (mdl, mb_par);
    TEST_CALL_REPORT("ProMoldbaseDialog()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
    if (err == PRO_TK_NO_ERROR)
    {            
        err = ProMoldbaseCreate (mb_par);
        TEST_CALL_REPORT("ProMoldbaseCreate()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
    }
    err = ProMoldbaseParamsFree (&mb_par);
    TEST_CALL_REPORT("ProMoldbaseParamsFree()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
            
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestMoldBaseRedefine	
    PURPOSE  : Replace existing mold base.	
\*============================================================================*/
int ProTestMoldBaseRedefine (ProMdl mdl)
{
    ProError err;
    ProMoldbaseParams mb_par = NULL;
    
    
    err = ProMoldbaseFromModelGet (mdl, &mb_par);
    TEST_CALL_REPORT("ProMoldbaseFromModelGet()",
            "ProTestMoldBaseRedefine()", err, err != PRO_TK_NO_ERROR);
    if (err != PRO_TK_NO_ERROR)
        return (-1);
        
    err = ProMoldbaseDialog (mdl, mb_par);
    TEST_CALL_REPORT("ProMoldbaseDialog()",
            "ProTestMoldBaseRedefine()", err, err != PRO_TK_NO_ERROR);
    if (err == PRO_TK_NO_ERROR)
    {            
        err = ProMoldbaseCreate (mb_par);
        TEST_CALL_REPORT("ProMoldbaseCreate()",
            "ProTestMoldBaseRedefine()", err, err != PRO_TK_NO_ERROR);
    }
    err = ProMoldbaseParamsFree (&mb_par);
    TEST_CALL_REPORT("ProMoldbaseParamsFree()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
    
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestMoldBaseRedefine	
    PURPOSE  : Get mold base info	
\*============================================================================*/
int ProTestMoldBaseInfo (ProMdl mdl)
{
    ProError err;
    ProMoldbaseParams mb_par = NULL;
    ProName vendor, series, length, width, *p_param_values;
    ProCharName *p_param_names;
    ProCharLine line, value;
    double rot_angle;
    ProMdl owner;
    char f_name[PRO_FILE_NAME_SIZE];
    int i, n_params;
    FILE *fp;
    
    ProTestQcrName(&mdl, (char *)".inf", f_name);
    fp = PTApplsUnicodeFopen (f_name, "w");
    if (fp == NULL)
        return -1;
    
    err = ProMoldbaseFromModelGet (mdl, &mb_par);
    TEST_CALL_REPORT("ProMoldbaseFromModelGet()",
            "ProTestMoldBaseInfo()", 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 = ProMoldbaseParamsGet (mb_par, &owner, vendor, series, length, width, 
        &p_param_names, &p_param_values, &rot_angle);
    TEST_CALL_REPORT("ProMoldbaseParamsGet()",
            "ProTestMoldBaseInfo()", err, err != PRO_TK_NO_ERROR);
            
    ProTKFprintf(fp, "%-20s %s\n", "Vendor", ProWstringToString (line, vendor)); 
    ProTKFprintf(fp, "%-20s %s\n", "Series", ProWstringToString (line, series)); 
    ProTKFprintf(fp, "%-20s %s\n", "Length", ProWstringToString (line, length)); 
    ProTKFprintf(fp, "%-20s %s\n", "Width", ProWstringToString (line, width)); 
    ProTKFprintf(fp, "%-20s %.3f\n", "Rotation angle", rot_angle); 
    
    err = ProArraySizeGet ((ProArray)p_param_values, &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 = ProMoldbaseParamsFree (&mb_par);
    TEST_CALL_REPORT("ProMoldbaseParamsFree()",
            "ProTestMoldBaseCreate()", err, err != PRO_TK_NO_ERROR);
    fclose (fp);
    
    ProStringToWstring(vendor, f_name);
    ProInfoWindowDisplay(vendor, NULL, NULL); 
    
    return (0);
}