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


/*--------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*--------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProObjects.h>
#include <ProAssembly.h>
#include <ProAsmcomp.h>
#include <ProElemId.h>
#include <ProElement.h>
#include <ProExpldstate.h>
#include <ProFeatType.h>
#include <ProMdl.h>
#include <ProMessage.h>
#include <ProModelitem.h>
#include <ProMenu.h>
#include <ProSelection.h>
#include <ProSolid.h>
#include <ProUtil.h>
#include <ProValue.h>
#include <ProSkeleton.h>

/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include "TestError.h"
#include "TestFiletypes.h"
#include "UtilFiles.h"
#include "UtilMessage.h"
#include "UtilString.h"
#include "UtilNames.h"
#include "UtilCollect.h"
#include "UtilTree.h"
#include "UtilMenu.h"
#include <PTApplsUnicodeUtils.h>
#include <ProTKRunTime.h>
#include <TestError.h>
#include <UtilFiles.h>

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


/*--------------------------------------------------------------------*\
Application data types
\*--------------------------------------------------------------------*/

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


ProError ProUtilAsmcompSelect(ProAsmcomp *p_asmcomp);
/*====================================================================*\
  Function : ProTestAsmcompSetPlaced()
  Purpose  : Menu callback
\*====================================================================*/
int  ProTestAsmcompSetPlaced (ProAppData data, int dummy)
{
    ProError err;
    ProAsmcomp asmcomp;
    ProBoolean  p_is_placed;
    
    static ProUtilMenuButtons placed_type[] = {
	{"TkAsmcompSetPlaced", 0, TEST_CALL_PRO_MENU_DELETE},
	{"Placed", PRO_B_TRUE, 0 },
	{"Unplaced", PRO_B_FALSE, 0 },
	{"",0,0}
    };
    err = ProUtilAsmcompSelect(&asmcomp); 

    if (err != PRO_TK_NO_ERROR)
	 return (0);

    err = ProUtilMenuIntValueSelect(placed_type, (int *)&p_is_placed);
    if (err != PRO_TK_NO_ERROR)
	 return (0);


    err = ProAsmcompSetPlaced (&asmcomp, p_is_placed);
    TEST_CALL_REPORT("ProAsmcompSetPlaced()",
			         "ProTestAsmcompSetPlaced()",
		     err, err != PRO_TK_NO_ERROR );


     return (0);
}

/*====================================================================*\
  Function : ProTestAsmcompSelectPath()
  Purpose  : Select comp and get path
\*====================================================================*/
ProError ProTestAsmcompSelectPath (
    ProAsmcomppath* asmcomppath)
{
   ProSelection *p_sel;
   ProError status;
   int n_sel;
   

   status = ProSelect((char *)"prt_or_asm", 1, NULL, NULL, NULL, NULL,
	&p_sel, &n_sel);
   TEST_CALL_REPORT("ProSelect()", 
	"ProTestAsmcompSelectPath()", status, status != PRO_TK_NO_ERROR);
   if (status != PRO_TK_NO_ERROR)
	return (status);

   status = ProSelectionAsmcomppathGet(p_sel[0], asmcomppath);
   TEST_CALL_REPORT("ProSelectionAsmcomppathGet()", 
	"ProTestAsmcompSelectPath()", status, status != PRO_TK_NO_ERROR);


   return (PRO_TK_NO_ERROR);  
}

/*====================================================================*\
  Function : ProTestAsmcompMakeUniqueSubas()
  Purpose  : Menu callback
\*====================================================================*/
int  ProTestAsmcompMakeUniqueSubas (ProAppData data, int dummy)
{
    ProError err;
    ProAsmcomppath asmcomppath;
    

    err = ProTestAsmcompSelectPath (&asmcomppath);
    if (err != PRO_TK_NO_ERROR)
	 return (0);

    err = ProAsmcompMakeUniqueSubasm (&asmcomppath);
     TEST_CALL_REPORT("ProAsmcompMakeUniqueSubasm()",
			         "ProTestAsmcompMakeUniqueSubas()",
		     err, err != PRO_TK_NO_ERROR );

     return (0);
}     

/*====================================================================*\
  Function : ProTestAsmcompRmvUniqueSubas()
  Purpose  : Menu callback
\*====================================================================*/
int  ProTestAsmcompRmvUniqueSubas (ProAppData data, int dummy)
{
    ProError err;
    ProAsmcomppath asmcomppath;
    

    err = ProTestAsmcompSelectPath (&asmcomppath);
    if (err != PRO_TK_NO_ERROR)
	 return (0);
         
    /* bug in the function ProAsmcompRmvUniqueSubasm() */
    err = ProAsmcompRmvUniqueSubasm (&asmcomppath);
     TEST_CALL_REPORT("ProAsmcompRmvUniqueSubasm()",
			         "ProTestAsmcompRmvUniqueSubas()", err, 0 );

     return (0);
}     

/*====================================================================*\
  Function : ProTestAsmcompPlacedInfoCollect()
  Purpose  : Menu callback
\*====================================================================*/
int  ProTestAsmcompPlacedInfoCollect (ProAppData data, int dummy)
{
    ProError err;
    ProAssembly c_asm;
    ProAsmcomp *asm_comps;
    int n_asm_comps = 0, i;
    
    FILE * qcr_place_info;
    ProBool is_plcd = PRO_B_TRUE;
    ProMdlfileType mdl_type;
    ProFamilyMdlName mdl_name;
    char mdl_char_name [PRO_FAMILY_MDLNAME_SIZE];

    err = ProMdlCurrentGet ((ProMdl*)&c_asm);
    TEST_CALL_REPORT("ProMdelCurrentGet()",
			         "ProTestAsmcompPlacedInfoCollect()",
		     err, err != PRO_TK_NO_ERROR );
    if (err !=PRO_TK_NO_ERROR)
	return (0);

    err = ProUtilCollectAsmcomp (c_asm, &asm_comps);
    if (err !=PRO_TK_NO_ERROR)
	return (0);

    err = ProArraySizeGet ((ProArray)asm_comps, &n_asm_comps);
    TEST_CALL_REPORT("ProArraySizeGet()",
			         "ProTestAsmcompPlacedInfoCollect()",
		     err, err != PRO_TK_NO_ERROR );
    if (err !=PRO_TK_NO_ERROR)
	return (0);
    qcr_place_info = (FILE *)PTApplsUnicodeFopen ("comp_plcd.info", "w+t");
    
    if (qcr_place_info == NULL)
       return 0;
    
    for (i=0; i<n_asm_comps; i++)
    {
	    
	err = ProAsmcompIsPlaced (& (asm_comps[i]), &is_plcd);
	 TEST_CALL_REPORT("ProAsmcompIsPlaced()",
			         "ProTestAsmcompPlacedInfoCollect()",
		     err, err != PRO_TK_NO_ERROR );
	    
	if (err !=PRO_TK_NO_ERROR)
	    break;
	
        err = ProAsmcompMdlMdlnameGet (& (asm_comps[i]), &mdl_type,
	    mdl_name);
	TEST_CALL_REPORT("ProAsmcompMdlMdlnameGet()",
			         "ProTestAsmcompPlacedInfoCollect()",
		     err, err != PRO_TK_NO_ERROR );
	ProWstringToString (mdl_char_name, mdl_name);
	ProTKFprintf (qcr_place_info,
	    "Component ID: %d\tModel name : %s\tComp. placement: %s\n",
	    (asm_comps[i]).id, mdl_char_name,
	    (is_plcd == PRO_B_TRUE ? "placed":"unplaced"));
    }

    fclose (qcr_place_info);

    err = ProArrayFree ((ProArray*)&asm_comps);
    TEST_CALL_REPORT("ProArrayFree()",
			         "ProTestAsmcompPlacedInfoCollect()",
		     err, err != PRO_TK_NO_ERROR );
    return (0);
}

/*====================================================================*\
  Function : ProTestAsmcompMechanismMenu()
  Purpose  : Menu callback
\*====================================================================*/
int  ProTestAsmcompMechanismMenu (ProAppData data, int dummy)
{
    ProError err;
       char * cnames [] =
    {
	"Set Placed",
	"Make Unique",
	"Remove Unique",
	"Collect Plcd Info",
	""
    };
    wchar_t **wnames;
    int i, dummy1;

    ProUtilMenuStringsAlloc(&wnames);
    for (i=0; i<sizeof (cnames)/sizeof (char*); i++)
    {
        ProUtilMenuStringsStrAdd(&wnames, cnames[i]);
    }
    err = ProMenuFromStringsRegister((char *)"TkAsmcompMachanism", NULL, wnames, NULL, 
	    NULL, &dummy1);
    TEST_CALL_REPORT("ProMenuFromStringsRegister()", 
	"ProTestAsmcompMechanismMenu()", err, err != PRO_TK_NO_ERROR);
    ProUtilMenuStringsFree(&wnames);
    
    ProMenubuttonActionSet( (char *)"TkAsmcompMachanism", (char *)"Set Placed", 
	    (ProMenubuttonAction)ProTestAsmcompSetPlaced, NULL, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompMachanism", (char *)"Make Unique", 
	    (ProMenubuttonAction)ProTestAsmcompMakeUniqueSubas, NULL, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompMachanism", (char *)"Remove Unique", 
	    (ProMenubuttonAction)ProTestAsmcompRmvUniqueSubas, NULL, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompMachanism", (char *)"Collect Plcd Info", 
	    (ProMenubuttonAction)ProTestAsmcompPlacedInfoCollect, NULL, 0); 
    ProMenubuttonActionSet( (char *)"TkAsmcompMachanism", (char *)"TkAsmcompMachanism", 
       	    (ProMenubuttonAction)ProMenuDelete, NULL, 0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char *)"TkAsmcompMachanism", NULL);
    ProMenuProcess((char *)"TkAsmcompMachanism", &i); 
    

    return (0);
}