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

#include <ProArray.h>
#include <ProDrawing.h>
#include <ProSelection.h>
#include <ProSimprep.h>
#include <ProSimprepdata.h>
#include <ProMessage.h>
#include <ProMenu.h>
#include <ProMdl.h>
#include <ProUtil.h>
#include <ProDwgtable.h>
#include <ProSimprepdata.h>

#include <TestError.h>
#include <UtilString.h>
#include <UtilFiles.h>
#include <UtilMessage.h>
#include <ProTKRunTime.h>
/*--------------------------------------------------------------------*\
  Function prototypes
\*--------------------------------------------------------------------*/
int ProTestDrwSimpRepAdd ();
int ProTestDrwSimpRepDel ();

/*===========================================================================*\
  FUNCTION: ProTestDrwSimpRep
  PURPOSE:  to read and make dwg simprep menu files
\*===========================================================================*/
int ProTestDrwSimpRep()
{
    int menu_id;
    ProError error;


    error = ProMenuFileRegister((char*)"-Simp Rep",(char*)"tksimprep.mnu", &menu_id);
    TEST_CALL_REPORT ("ProMenuFileRegister()",
        "ProTestDrwSimpRep()", error, error!=PRO_TK_NO_ERROR);

    error = ProMenubuttonActionSet((char*)"-Simp Rep", (char*)"-add",
                (ProMenubuttonAction)ProTestDrwSimpRepAdd, NULL, 0);
    TEST_CALL_REPORT ("ProMenubuttonActionSet()",
        "ProTestDrwSimpRep()", error, error!=PRO_TK_NO_ERROR);

    error = ProMenubuttonActionSet((char*)"-Simp Rep", (char*)"-delete",
                (ProMenubuttonAction)ProTestDrwSimpRepDel, NULL, 0);
    TEST_CALL_REPORT ("ProMenubuttonActionSet()",
        "ProTestDrwSimpRep()", error, error!=PRO_TK_NO_ERROR);

    error = ProMenubuttonActionSet((char*)"-Simp Rep", (char*)"-Simp Rep",
                (ProMenubuttonAction)ProMenuDelete, NULL, 0);
    TEST_CALL_REPORT ("ProMenubuttonActionSet()",
        "ProTestDrwSimpRep()", error, error!=PRO_TK_NO_ERROR);


    error = ProMenuCreate(PROMENUTYPE_MAIN, (char*)"-Simp Rep", &menu_id);
    TEST_CALL_REPORT ("ProMenuCreate()",
        "ProTestDrwSimpRep()", error, error!=PRO_TK_NO_ERROR);

    error = ProMenuProcess((char*)"-Simp Rep", &menu_id);
    TEST_CALL_REPORT ("ProMenuProcess()", "ProTestDrwSimpRep()", error, 
        error != PRO_TK_NO_ERROR && error != PRO_TK_E_FOUND);

    return (0);
}

/*===========================================================================*\
  FUNCTION: ProTestDrwSimpRepAdd
  PURPOSE: add simpliefied representation to drawing.
\*===========================================================================*/
int ProTestDrwSimpRepAdd ()
{
    int             i, mdl_num;
    ProCharLine     c_name, line;
    ProDrawing      drawing;
    ProMdl         *mdl_arr = NULL;
    ProSimprep      simprep;
    ProMdlName      w_name;
    ProError        error;

    error = ProMdlCurrentGet((ProMdl*)&drawing);
    TEST_CALL_REPORT ("ProMdlCurrentGet()",
        "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);

/*----------------------------------------------------------------------------*\
    Get model list in session.
\*----------------------------------------------------------------------------*/
    error = ProSessionMdlList ( PRO_MDL_ASSEMBLY, &mdl_arr, &mdl_num);
    TEST_CALL_REPORT ("ProSessionMdlList()",
        "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);

/*----------------------------------------------------------------------------*\
    Add simp representation.
\*----------------------------------------------------------------------------*/
    if ( mdl_num >0)
    {
        for (i=0;i<mdl_num; i++)
        {
            error = ProMdlMdlnameGet (mdl_arr[i], w_name);
            TEST_CALL_REPORT ("ProMdlMdlnameGet()",
                "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);

            ProTKSprintf (line,"Add simpliefied representation from model %s [y/N}:",
                    ProWstringToString(c_name, w_name));

            ProUtilMsgPrint("gen", "TEST %0s", line);

            if (!ProUtilYesnoGet((char*)"Y"))
                continue;

            error = ProSimprepSelect ((ProSolid)mdl_arr[i], &simprep);
            TEST_CALL_REPORT ("ProSimprepSelect()",
                "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);
            if (error == PRO_TK_NO_ERROR)
            {
                error = ProDrawingAsmsimprepAdd (drawing, &simprep);
                TEST_CALL_REPORT ("ProDrawingAsmsimprepAdd()",
                    "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);
            }
        }

        error = ProArrayFree ((ProArray*)&mdl_arr);
        TEST_CALL_REPORT ("ProArrayFree()",
            "ProTestDrwSimpRepAdd()", error, error!=PRO_TK_NO_ERROR);

    }
    return (0);
}

/*===========================================================================*\
  FUNCTION: ProTestDrwSimpRepDel
  PURPOSE: Delete selected simpliefied representation from drawing.
\*===========================================================================*/
int ProTestDrwSimpRepDel ()
{
    int             j, i;
    int             rep_num, model_num;
    ProCharLine     line, c_name;
    ProDrawing      drawing;
    ProError        status;
    ProName         name;
    ProSimprep     *rep_arr      = NULL;
    ProSimprepdata *rep_data_ptr = NULL;
    ProSolid        model;
    ProView         view;
    ProSelection   *sel_arr;

    status = ProMdlCurrentGet ((ProMdl*)&drawing);
    TEST_CALL_REPORT ("ProMdlCurrentGet()", "ProTestDrwSimpRepDel()",
        status, status!=PRO_TK_NO_ERROR);

    ProUtilMsgPrint("gen", "TEST %0s", "Select view with simprep for delete.");
    status = ProSelect ((char*)"dwg_view", 1, NULL, NULL, NULL, NULL, 
                &sel_arr, &model_num);
    TEST_CALL_REPORT ("ProSelect()", "ProTestDrwSimpRepDel()",
        status, status != PRO_TK_NO_ERROR);

    status = ProSelectionViewGet (sel_arr[0], &view);
    TEST_CALL_REPORT ("ProSelectionViewGet()", "ProTestDrwSimpRepDel()",
        status, status != PRO_TK_NO_ERROR);
    
    status = ProDrawingViewSolidGet (drawing, view, &model);
    TEST_CALL_REPORT ("ProDrawingViewSolidGet()", "ProTestDrwSimpRepDel()",
        status, status != PRO_TK_NO_ERROR);

/*----------------------------------------------------------------------------*\
            Get  simp representations in model.
\*----------------------------------------------------------------------------*/
    status = ProDrawingSimprepsCollect(drawing, model, &rep_arr);
    TEST_CALL_REPORT ("ProDrawingSimprepsCollect()",
        "ProTestDrwSimpRepDel()", status, status!=PRO_TK_NO_ERROR);

    /* status != PRO_TK_GENERAL_ERROR until simpreps corrupt.*/
    
    status = ProArraySizeGet ((ProArray)rep_arr , &rep_num);
    TEST_CALL_REPORT("ProArraySizeGet()", "ProTestDrwSimpRepDel()", status,
        status != PRO_TK_NO_ERROR && status != PRO_TK_BAD_INPUTS);

#if 0
    if ( status == PRO_TK_NO_ERROR)
    {
        for (i=0; i<rep_num; i++)
        {
/*----------------------------------------------------------------------------*\
                Dont work with master representation.
\*----------------------------------------------------------------------------*/
            if (rep_arr[i].id == -1)
                continue;

/*----------------------------------------------------------------------------*\
                Delete some simp representation.
\*----------------------------------------------------------------------------*/
            /* status != PRO_TK_E_NOT_FOUND */
            status = ProSimprepdataGet (&rep_arr[i], &rep_data_ptr);
            TEST_CALL_REPORT ("ProSimprepdataGet()",
                "ProTestDrwSimpRepDel()", status, status != PRO_TK_NO_ERROR
                && status != PRO_TK_E_NOT_FOUND);

            if (status != PRO_TK_NO_ERROR)
                continue;

            status = ProSimprepdataNameGet (rep_data_ptr, name);
            TEST_CALL_REPORT ("ProSimprepdataNameGet()",
                "ProTestDrwSimpRepDel()", status, status!=PRO_TK_NO_ERROR);

            sprintf (line, "Delete simpliefied representation %s [y/n]",
                     ProWstringToString(c_name, name));

            ProUtilMsgPrint("gen", "TEST %0s", line);

            if (!ProUtilYesnoGet("Y"))
                continue;

            status = ProSimprepdataFree (&rep_data_ptr);
            TEST_CALL_REPORT ("ProSimprepdataFree()",
                "ProTestDrwSimpRepDel()", status, status!=PRO_TK_NO_ERROR);

            status = ProDrawingAsmsimprepDelete(drawing,  &rep_arr[i]);
            TEST_CALL_REPORT ("ProDrawingAsmsimprepDelete()",
                "ProTestDrwSimpRepDel()", status, status!=PRO_TK_NO_ERROR);
        }

        status = ProArrayFree ((ProArray*)&rep_arr);
        TEST_CALL_REPORT("ProArrayFree()", "ProTestDrwSimpRepDel()",
            status, status != PRO_TK_NO_ERROR);
    }
#endif

    return (0);
}