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


#include <ProToolkit.h>
#include <ProObjects.h>
#include <ProMfg.h>
#include <ProMenu.h>
#include <ProNotify.h>
#include <ProRmdt.h>
#include <UtilFiles.h>
#include <ProTKRunTime.h>
#include <PTApplsUnicodeUtils.h>
#include <ProUtil.h>
/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include "TestError.h"

/*--------------------------------------------------------------------*\
Application macros
\*--------------------------------------------------------------------*/
#define USER_ON	    1
#define USER_OFF    0
/*--------------------------------------------------------------------*\
Application global/external data
\*--------------------------------------------------------------------*/
static FILE *log_file;
static int preaction_status = USER_OFF;

/*=============================================================*\
  FUNCTION: ProTestMoldLayoutInfo
  PURPOSE:  Test Mold Layout info functions 
\*=============================================================*/
int ProTestMoldLayoutInfo(ProMdl *model)
{
    ProError status;
    ProMoldLayout mold_layout;   
    FILE *fp;
    int i, cor, num_p;
    char **p_par_nam, line[256];
    wchar_t **pw_par_val, *w_name;
    double xyz_p[3], xyz_m[3], xyz_t[3];
    ProPath w_path;
    char fname[PRO_FILE_NAME_SIZE];

    ProTestQcrName(model, (char*)".inf", fname);
    fp = PTApplsUnicodeFopen(fname, "w");
    ProStringToWstring(w_path, fname);
   
    status = ProMfgMoldLayoutGet((ProMfg)*model, &mold_layout);
    TEST_CALL_REPORT("ProMfgMoldLayoutGet()", "ProTestMoldLayoutInfo()",
					status, status != PRO_TK_NO_ERROR);
    if (status != PRO_TK_NO_ERROR)
      	return (0);
   
    ProTKFprintf(fp, (char*)"IMM\n");
    status = ProRmdtImmInfoGet(mold_layout, &w_name, &p_par_nam, &pw_par_val, 
	&num_p);
    TEST_CALL_REPORT("ProRmdtImmInfoGet()", "ProTestMoldLayoutInfo()",
					status, status != PRO_TK_NO_ERROR);
    if (status == PRO_TK_NO_ERROR) {
	if (w_name != NULL) 
	{
      	    ProWstringToString(line, w_name);
      	    ProTKFprintf(fp, (char*)"    %-20s %s\n", (char*)"Name", line);
	}
      	for (i=0; i<num_p; i++)
      	{
	    if (pw_par_val[i] == NULL || p_par_nam[i] == NULL)
		continue; 
	    ProWstringToString(line, pw_par_val[i]);
	    ProTKFprintf(fp, (char*)"    %-20s %s\n", p_par_nam[i], line);
      	}
    }
    else
	ProTKFprintf(fp, (char*)"    Unspecified\n");

    ProTKFprintf(fp, (char*)"\nMaterial\n");
    status = ProRmdtMaterialInfoGet(mold_layout, &p_par_nam, &pw_par_val,
	 &num_p);
    TEST_CALL_REPORT("ProRmdtMaterialInfoGet()", "ProTestMoldLayoutInfo()",
					status, status != PRO_TK_NO_ERROR);
    if (status == PRO_TK_NO_ERROR) {
      	for (i=0; i<num_p; i++)
        {
	    if (pw_par_val[i] == NULL || p_par_nam[i] == NULL)
		continue; 
	    ProWstringToString(line, pw_par_val[i]);
	    ProTKFprintf(fp, (char*)"    %-20s %s\n", p_par_nam[i], line);
      	}
    }
    else
	ProTKFprintf(fp, (char*)"    Unspecified\n");


    ProTKFprintf(fp, (char*)"\nMold Base\n");
    status = ProRmdtMoldBaseInfoGet(mold_layout, &p_par_nam, &pw_par_val,
				   &num_p, &cor);
    TEST_CALL_REPORT("ProRmdtMoldBaseInfoGet()", "ProTestMoldLayoutInfo()",
					status, status != PRO_TK_NO_ERROR);
    if (status == PRO_TK_NO_ERROR) {
      	for (i=0; i<num_p; i++)
      	{
	    if (pw_par_val[i] == NULL || p_par_nam[i] == NULL)
		continue; 
	    ProWstringToString(line, pw_par_val[i]);
	    ProTKFprintf(fp, (char*)"    %-20s %s\n", p_par_nam[i], line);
      	}
      	ProTKFprintf(fp, (char*)"    Corner %d\n", cor);
    }
    else
	ProTKFprintf(fp, (char*)"    Unspecified\n");

    ProTKFprintf(fp, (char*)"\nRef Model\n"); 
    for (i=0;;i++)
    {
      	status = ProRmdtRefModelInfoGet(mold_layout, i, xyz_p, xyz_m, xyz_t);
      	if (status!=PRO_TK_NO_ERROR)
	    break;
        TEST_CALL_REPORT("ProRmdtRefModelInfoGet()", "ProTestMoldLayoutInfo()",
					status, status != PRO_TK_NO_ERROR);
      	ProTKFprintf(fp, (char*)"    %3d   +   %7.2f %7.2f %7.2f\n"
	            "          -   %7.2f %7.2f %7.2f\n"
	            "       Total  %7.2f %7.2f %7.2f\n",
	      i,  xyz_p[0], xyz_p[1], xyz_p[2],
	          xyz_m[0], xyz_m[1], xyz_m[2],
	          xyz_t[0], xyz_t[1], xyz_t[2]);
    }
    if (i<=0)
	ProTKFprintf(fp, (char*)"    Unspecified\n");
   
    ProTKFprintf(fp, (char*)"\nWorkpiece\n"); 
    for (i=0;;i++)
    {
      	status = ProRmdtWorkpieceInfoGet(mold_layout, i, xyz_p, xyz_m, xyz_t);
      	if (status!=PRO_TK_NO_ERROR)
	    break;
        TEST_CALL_REPORT("ProRmdtWorkpieceInfoGet()",
	    "ProTestMoldLayoutInfo()", status, status != PRO_TK_NO_ERROR);
      	ProTKFprintf(fp, (char*)"    %3d   +   %7.2f %7.2f %7.2f\n"
	            "          -   %7.2f %7.2f %7.2f\n"
	            "       Total  %7.2f %7.2f %7.2f\n",
	      i,  xyz_p[0], xyz_p[1], xyz_p[2],
	          xyz_m[0], xyz_m[1], xyz_m[2],
	          xyz_t[0], xyz_t[1], xyz_t[2]);
    }
    if (i<=0)
	ProTKFprintf(fp, (char*)"    Unspecified\n");
   
    fclose(fp);
   
    ProInfoWindowDisplay(w_path, NULL, NULL);
    return 0;
}

/*=============================================================*\
  FUNCTION: ProUtilLogFileWrite
  PURPOSE:  write one line into log file
\*=============================================================*/
static void ProUtilLogFileWrite(char *line)
{
    if (log_file != NULL)
    {
	ProTKFprintf(log_file, (char*)"%s function called\n", line);
    }
}

/*=============================================================*\
  FUNCTION: ProTestRmdtCreateWpPreAction
  PURPOSE:  Preaction function.  Called before workpice created 
\*=============================================================*/
ProError ProTestRmdtCreateWpPreAction(
    ProMoldLayout mold_layout)
{
    ProDlgWorkpieceData  wp_def_sets;
    int  i, j;
    ProError  err;

    TEST_CALL_REPORT("ProRmdtCreateWpPreAction()", 
				    "ProTestRmdtCreateWpPreAction()", (ProError)0, 0);

    ProUtilLogFileWrite((char*)"PRO_RMDT_CREATE_WP_PRE");

    memset(&wp_def_sets, '\0', sizeof(wp_def_sets));
    wp_def_sets.IsRect = PRO_B_TRUE;
    wp_def_sets.IsSym  = PRO_B_TRUE;
    wp_def_sets.rect_dim[0] = 100.0;
    wp_def_sets.rect_dim[1] = 200.0;
    wp_def_sets.rect_dim[2] = 300.0;
    wp_def_sets.radial_z = 150.0;
    wp_def_sets.radial_diam = 120.0;
    for (i = 0; i < 2; i++)
	for (j = 0; j < 3; j++)
	    wp_def_sets.allowance[i][j] = 10.0;
    err = ProRmdtDlgWorkpieceSet(mold_layout, &wp_def_sets);
    TEST_CALL_REPORT("ProRmdtDlgWorkpieceSet()", 
		"ProTestRmdtCreateWpPreAction()", err, err != PRO_TK_NO_ERROR);

    return err;
}

/*=============================================================*\
  FUNCTION: ProTestRmdtCreateImmPreAction
  PURPOSE:  Preaction function.  Called before workpice created 
\*=============================================================*/
ProError ProTestRmdtCreateImmPreAction(
    ProMoldLayout mold_layout)
{
    ProError  err;
    ProName w_name;

    TEST_CALL_REPORT("ProRmdtCreateImmPreAction()", 
				    "ProTestRmdtCreateImmPreAction()", (ProError)0, 0);

    ProUtilLogFileWrite((char*)"PRO_RMDT_CREATE_IMM_PRE");

    ProStringToWstring(w_name, (char*)"IMM_14x14");

    err = ProRmdtDlgImmSet(mold_layout, w_name);
    TEST_CALL_REPORT("ProRmdtDlgImmSet()", 
		"ProTestRmdtCreateImmPreAction()", err, err != PRO_TK_NO_ERROR);
    
    return (err);
}

/*=============================================================*\
  FUNCTION: ProTestRmdtBoundBoxPreAction
  PURPOSE:  Preaction function. 
\*=============================================================*/
ProError ProTestRmdtBoundBoxPreAction(
    ProMoldLayout mold_layout)
{
    ProError err;
    ProMatrix matrix = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};

    TEST_CALL_REPORT("ProRmdtBoundBoxPreAction()", 
				    "ProTestRmdtBoundBoxPreAction()", (ProError)0, 0);

    ProUtilLogFileWrite((char*)"PRO_RMDT_BOUND_BOX_PRE");

    err = ProRmdtDlgRefModelOrientSet(mold_layout, matrix);
    TEST_CALL_REPORT("ProRmdtDlgRefModelOrientSet()", 
		"ProTestRmdtBoundBoxPreAction()", err, err != PRO_TK_NO_ERROR);
    return (err);
}

/*=============================================================*\
  FUNCTION: ProTestRmdtCavLayoutPreAction
  PURPOSE:  Preaction function. 
\*=============================================================*/
ProError ProTestRmdtCavLayoutPreAction(
    ProMoldLayout mold_layout)
{
    ProError err;
    ProDlgCavityLayoutData cavdata;

    TEST_CALL_REPORT("ProRmdtCavLayoutPreAction()", 
				    "ProTestRmdtCavLayoutPreAction()", (ProError)0, 0);
    ProUtilLogFileWrite((char*)"PRO_RMDT_CAV_LAYOUT_PRE");

    memset(&cavdata, '\0', sizeof(cavdata));
    cavdata.cavity_layout_type = 0;
    cavdata.is_symmetric = PRO_B_TRUE;
    cavdata.mirror_type = 0;
    cavdata.num = 9;
    cavdata.num_x = 3;
    cavdata.num_y = 3;
    cavdata.offset_x = 2.0;
    cavdata.offset_y = 2.0;
    cavdata.inc_x = 2.0;
    cavdata.inc_y = 2.0;

    err = ProRmdtDlgCavityLayoutSet(mold_layout, &cavdata);
    TEST_CALL_REPORT("ProRmdtDlgCavityLayoutSet()", 
		"ProTestRmdtCavLayoutPreAction()", err, err != PRO_TK_NO_ERROR);

    return (err);
}

/*=============================================================*\
  FUNCTION: ProTestRmdtMaterialPreAction
  PURPOSE:  Preaction function. 
\*=============================================================*/
ProError ProTestRmdtMaterialPreAction(
    ProMoldLayout mold_layout)
{
    ProError err;
    ProName w_gentype, w_mfg, w_trade, w_grade;

    TEST_CALL_REPORT("ProRmdtMaterialPreAction()", 
				    "ProTestRmdtMaterialPreAction()", (ProError)0, 0);
    ProUtilLogFileWrite((char*)"PRO_RMDT_MATERIAL_PRE");

    ProStringToWstring(w_gentype, (char*)"Policarbonate");
    ProStringToWstring(w_mfg, (char*)"GE_plastics");
    ProStringToWstring(w_trade, (char*)"Lexan");
    ProStringToWstring(w_grade, (char*)"101R");

    err = ProRmdtDlgMaterialSet(mold_layout, w_gentype, w_mfg, w_trade,
	w_grade);
    TEST_CALL_REPORT("ProRmdtDlgMaterialSet()", 
		"ProTestRmdtMaterialPreAction()", err, err != PRO_TK_NO_ERROR);

    return (err);
}

/*=============================================================*\
  FUNCTION: ProTestRmdtBaseSelectPreAction
  PURPOSE:  Preaction function. 
\*=============================================================*/
ProError ProTestRmdtBaseSelectPreAction(
    ProMoldLayout mold_layout)
{
    ProError err;
    ProName w_vendor, w_series, w_length, w_width;

    TEST_CALL_REPORT("ProRmdtMBaseSelectPreAction()", 
				    "ProTestRmdtBaseSelectPreAction()", (ProError)0, 0);
    ProUtilLogFileWrite((char*)"PRO_RMDT_MBASE_SELECT_PRE");

    ProStringToWstring(w_vendor, (char*)"Futaba");
    ProStringToWstring(w_series, (char*)"SA");
    ProStringToWstring(w_length, (char*)"200");
    ProStringToWstring(w_width, (char*)"200");

    err = ProRmdtDlgMoldBaseSet(mold_layout, w_vendor, w_series, w_length, 
	w_width);
    TEST_CALL_REPORT("ProRmdtDlgMoldBaseSet()", 
	    "ProTestRmdtBaseSelectPreAction()", err, err != PRO_TK_NO_ERROR);

    return (err);
}

/*====================================================================*\
    FUNCTION :  ProTestRmdtPreactionSet()
    PURPOSE  :  Set/unset preaction functions for ProRmdt actions
\*====================================================================*/
int ProTestRmdtPreactionSet(
    ProMdl *mdl,
    int action)
{
    ProError err;

    preaction_status = action;
    if (action == USER_OFF)
    {
	err = ProMenubuttonActivate((char*)"TkMoldLayout",(char*)"Preaction on");
	TEST_CALL_REPORT("ProMenubuttonActivate()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProMenubuttonDeactivate((char*)"TkMoldLayout",(char*)"Preaction off");
	TEST_CALL_REPORT("ProMenubuttonDeactivate()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);

	if (log_file != NULL)
	{
	    fclose(log_file);
	    log_file = 0;
	}

	err = ProNotificationUnset(PRO_RMDT_CREATE_IMM_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationUnset(PRO_RMDT_BOUND_BOX_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationUnset(PRO_RMDT_CAV_LAYOUT_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationUnset(PRO_RMDT_CREATE_WP_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationUnset(PRO_RMDT_MATERIAL_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationUnset(PRO_RMDT_MBASE_SELECT_PRE);
	TEST_CALL_REPORT("ProNotificationUnset()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
    }
    else
    {
	err = ProMenubuttonActivate((char*)"TkMoldLayout",(char*)"Preaction off");
	TEST_CALL_REPORT("ProMenubuttonActivate()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProMenubuttonDeactivate((char*)"TkMoldLayout",(char*)"Preaction on");
	TEST_CALL_REPORT("ProMenubuttonDeactivate()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);

	if (log_file == NULL)
	{
	    log_file = PTApplsUnicodeFopen("rmdt_notify.log", "w");
	}

	err = ProNotificationSet(PRO_RMDT_CREATE_IMM_PRE,
	    (ProFunction)ProTestRmdtCreateImmPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationSet(PRO_RMDT_BOUND_BOX_PRE,
	    (ProFunction)ProTestRmdtBoundBoxPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationSet(PRO_RMDT_CAV_LAYOUT_PRE,
	    (ProFunction)ProTestRmdtCavLayoutPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationSet(PRO_RMDT_CREATE_WP_PRE,
	    (ProFunction)ProTestRmdtCreateWpPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationSet(PRO_RMDT_MATERIAL_PRE,
	    (ProFunction)ProTestRmdtMaterialPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
	err = ProNotificationSet(PRO_RMDT_MBASE_SELECT_PRE,
	    (ProFunction)ProTestRmdtBaseSelectPreAction);
	TEST_CALL_REPORT("ProNotificationSet()", 
		    "ProTestRmdtPreactionSet()", err, err != PRO_TK_NO_ERROR);
    }

    return (0);
}

/*====================================================================*\
    FUNCTION :  ProTestMoldLayoutMenu()
    PURPOSE  :  Define and activate menu of Toolkit Test commands Mold  layout
\*====================================================================*/
int ProTestMoldLayoutMenu()
{
    ProError status;
    ProMdl   model;
    int action;

    status = ProMdlCurrentGet(&model);
    TEST_CALL_REPORT("ProMdlCurrentGet()", "ProTestMoldLayoutMenu()",
                                status, status != PRO_TK_NO_ERROR);

    ProMenuFileRegister((char*)"TkMoldLayout",(char*)"tkrmdt.mnu", NULL);
    ProMenubuttonActionSet((char*)"TkMoldLayout",(char*)"TkMoldLayout",
                           (ProMenubuttonAction)ProMenuDelete, NULL,  0);
    ProMenubuttonActionSet((char*)"TkMoldLayout",(char*)"Preaction on",
	    (ProMenubuttonAction)ProTestRmdtPreactionSet, &model, USER_ON);
    ProMenubuttonActionSet((char*)"TkMoldLayout",(char*)"Preaction off",
            (ProMenubuttonAction)ProTestRmdtPreactionSet, &model, USER_OFF);
    ProMenubuttonActionSet((char*)"TkMoldLayout",(char*)"-Info",
                        (ProMenubuttonAction)ProTestMoldLayoutInfo, &model, 0);
    ProMenubuttonActionSet((char*)"TkMoldLayout",(char*)"Done TkML",
                        (ProMenubuttonAction)ProMenuDelete, NULL,  0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char*)"TkMoldLayout", NULL);
    ProTestRmdtPreactionSet(&model, preaction_status);
    ProMenuProcess((char*)"", &action);

    return (0);
}