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


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProSolid.h>
#include <ProCsys.h>
#include <ProDtlnote.h>
#include <ProDtlattach.h>

/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>



/*---------------------------Function Prototypes-----------------------------*/
ProError UserPickPosition ();
void UserFreeNoteSetup();

/*****************************************************************************\
Function : UserNoteCreate() 
Purpose :  Create a drawing note
\*****************************************************************************/
ProError UserNoteCreate()
{
    ProError    status;
    ProMdl		p_draw;
    ProDtlnote 	note;
    ProCharLine     	text;
    ProLine     	w_text;
    ProPoint3d		pos;
    ProSelection  *leaders;
    int			n_leaders;
    ProFileName WMSGFIL = {'m','s','g','_','u','g','d','w','g','.','t','x','t','\0'};

    /*------------------------------------------------------------*\
    Get the current model
    \*------------------------------------------------------------*/
    status = ProMdlCurrentGet(&p_draw);
    ERROR_CHECK("UserNoteCreate","ProMdlCurrentGet",status);
    if (status != PRO_TK_NO_ERROR) return(status);

    /*----------------------------------------------------------*\
    Ask the user for the note position.
    \*----------------------------------------------------------*/
    status = UserPickPosition(WMSGFIL,"Pick the position of the new note.", pos);
    ERROR_CHECK("UserNoteCreate","UserPickPosition",status);
    if (status != PRO_TK_NO_ERROR) return(status);
    
    /*----------------------------------------------------------*\
    Ask the user for a line of note text.
    \*----------------------------------------------------------*/
    status = ProMessageDisplay(WMSGFIL, "USER %0s", 
				"Enter note text [QUIT] : ");
    ERROR_CHECK("UserNoteCreate","ProMessageDisplay",status);

    status = ProMessageStringRead( PRO_LINE_SIZE, w_text);
    ERROR_CHECK("UserNoteCreate","ProMessageStringRead",status);
    if (status != PRO_TK_NO_ERROR) return(status);

    ProWstringToString (text, w_text);

    /*----------------------------------------------------------*\
    Ask the user to pick the leader points (edges of models in
    drawing views).
    \*----------------------------------------------------------*/
    status = ProMessageDisplay(WMSGFIL, "USER %0s", "Select leader points");
    ERROR_CHECK("UserNoteCreate","ProMessageDisplay",status);

    ProSelect ("edge", -1, NULL, NULL, NULL, NULL, &leaders, &n_leaders);

    /*----------------------------------------------------------*\
    Add the note to the drawing.
    \*----------------------------------------------------------*/
    UserFreeNoteSetup ( p_draw, pos, text, n_leaders, leaders, &note );

    status = ProDtlnoteShow(&note);
    ERROR_CHECK("UserNoteCreate","prodtl_display",status);
    if (status != PRO_TK_NO_ERROR) return(PRO_TK_GENERAL_ERROR);

    return(PRO_TK_NO_ERROR);
}

/*===============================================================*\
    Function that gives the default text height for the
    specified drawing
\*===============================================================*/
ProError UserTextDefaultHeight (ProMdl p_draw, double * height)
{
    int 	cur_sheet;
    ProName   	w_size;
    ProMatrix   matrix;
    ProError    err;

    /*----------------------------------------------------------*\
    Get the default text height, in drawing coordinates. 
    \*----------------------------------------------------------*/
    err = ProSolidDefaulttextheightGet ((ProSolid)p_draw, height);
    ERROR_CHECK("UserTextDefaultHeight","ProSolidDefaulttextheightGet",
		 err);
    if (PRO_TK_NO_ERROR != err) return (PRO_TK_GENERAL_ERROR);

    err = ProDrawingCurrentSheetGet(drawing, &cur_sheet);
    err = ProDrawingSheetTrfGet(drawing, cur_sheet, w_size, matrix);
    ERROR_CHECK("UserTextDefaultHeight","ProDrawingSheetTrfGet", err);
    if (PRO_TK_NO_ERROR != err) return (PRO_TK_GENERAL_ERROR);

   *height /= matrix[0][0]; /* for unit conversion */
    return (PRO_TK_NO_ERROR);

}

/*===============================================================*\
    Function that sets up the C structure for a free note with
    parametric leaders
\*===============================================================*/
void UserFreeNoteSetup(
	ProMdl		p_draw,
	ProVector       pos,
	char          	text[],
	int           	n_leaders,
	ProSelection   	*attach_geom,
	ProDtlnote      *p_note)
{
    ProDtlnotedata notedata;
    ProDtlnoteline dtl_line;
    ProDtlnotetext dtl_text;
    ProLine wtext;
    double height;
    ProColor color;
    ProDtlattach attachment, *leaders;
    ProError err = PRO_TK_NO_ERROR;
    int l;

    /*----------------------------------------------------------*\
    Claim and initialize the note record.
    \*----------------------------------------------------------*/
    ProDtlnotedataAlloc(p_draw, &notedata);

    /*----------------------------------------------------------*\
    Add the single text line record.
    \*----------------------------------------------------------*/
    ProDtlnotelineAlloc (&dtl_line);

    /*----------------------------------------------------------*\
    Add a single text record to the text line.
    \*----------------------------------------------------------*/
    ProDtlnotetextAlloc(&dtl_text);

    /*----------------------------------------------------------*\
    Set up the text with default properties.
    \*----------------------------------------------------------*/
    err = UserTextDefaultHeight(p_draw, &height);
    ProDtlnotetextHeightSet(dtl_text, height);
    ERROR_CHECK("UserFreeNoteSetup","UserTextDefaultHeight",err);
    err = ProDtlnotetextWidthSet (dtl_text, 0.8);
    err = ProDtlnotetextSlantSet (dtl_text, 0.0);
    err = ProDtlnotetextThicknessSet  (dtl_text, 0.0);
    ProStringToWstring (wtext, text);
    ProDtlnotetextStringSet (dtl_text, wtext);

    err = ProDtlnotelineTextAdd(dtl_line, dtl_text);
    err = ProDtlnotedataLineAdd(notedata, dtl_line);

    /*----------------------------------------------------------*\
    Set the note to be the default color for text.
    \*----------------------------------------------------------*/
    color.method = PRO_COLOR_METHOD_TYPE;
    color.value.type = PRO_COLOR_LETTER;
    err = ProDtlnotedataColorSet (notedata, &color);

    /*----------------------------------------------------------*\
    Set the attachment to be FREE at the specified screen
    coordinates.
    \*----------------------------------------------------------*/
    err = ProDtlattachAlloc(PRO_DTLATTACHTYPE_FREE, NULL, pos, NULL, &attachment);
    err = ProDtlnotedataAttachmentSet(notedata, attachment);

    /*----------------------------------------------------------*\
    Copy the selected leader points as PARAMETRIC leaders.
    \*----------------------------------------------------------*/
    err = ProArrayAlloc (n_leaders, sizeof (ProDtlattach), 1, (ProArray*)&leaders);

    for (l = 0; l < n_leaders; l++)
    {
         ProDtlattach leader;
         err = ProDtlattachAlloc(PRO_DTLATTACHTYPE_PARAMETRIC, NULL, NULL, attach_geom[l], &leader);
         err = ProArrayObjectAdd ((ProArray*)&leaders, -1, 1, &leader);
    }

    err = ProDtlnotedataLeadersSet(notedata, leaders);

    for (l = 0; l < n_leaders; l++)
      err = ProDtlattachFree (leaders[l]);
    err = ProArrayFree ((ProArray*)&leaders);

    err = ProDtlnoteCreate(p_draw, NULL, notedata, p_note);
    err = ProDtlnotedataFree(notedata);
}