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



/*----------------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*----------------------------------------------------------------------------*/
#include "ProToolkit.h"
#include "ProMenu.h"
#include "ProUtil.h"

/*----------------------------------------------------------------------------*\
Pro/DEVELOP includes
\*----------------------------------------------------------------------------*/

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

/*----------------------------------------------------------------------------*\
Application includes
\*----------------------------------------------------------------------------*/
#include <TestError.h>
#include <TestMenu.h>
#include <UtilMessage.h>
#include <PTApplsUnicodeUtils.h>

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

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

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

static int menuTestMode = TRUE;

static wchar_t **strings;
static wchar_t **help;


/*============================================================================*\
  Function : ProTestMenus
  Purpose  : Create a menu to test Menu functions
\*============================================================================*/
int ProTestMenus(void *a, int b)
{
    int menu_id;
    ProError status;
    int result;
    int action;
    int i;
    char tmp_str[80];
    ProBoolean value;
    FILE *fp = NULL;
    int ProTestMenuGenaction(FILE*, void*, void*, void*, void*, int*);
    
    if((fp = PTApplsUnicodeFopen ("test_menus.txt", "a")) == NULL)
        return -1;
        
    ProUtilMsgPrint("gen", "TEST %0s", "ProTestMenus: enter");

/*----------------------------------------------------------------------------*\
Test initialization
\*----------------------------------------------------------------------------*/

    strings = (wchar_t**)calloc(PRO_TEST_NUM_STRINGS+1, sizeof(wchar_t *));
    help = (wchar_t**)calloc(PRO_TEST_NUM_STRINGS+1, sizeof(wchar_t *));

    for( i = 0; i < PRO_TEST_NUM_STRINGS; i++ )
    {
	strings[i] = (wchar_t*)calloc(80, sizeof(wchar_t));
	help[i] = (wchar_t*)calloc(80, sizeof(wchar_t));

	ProTKSprintf(tmp_str, "Button%d", i);
        ProStringToWstring(strings[i], tmp_str);
	ProTKSprintf(tmp_str, "Select Button%d", i);
        ProStringToWstring(help[i], tmp_str);
    }
    strings[i] = (wchar_t*)calloc(80, sizeof(wchar_t));
    help[i] = (wchar_t*)calloc(80, sizeof(wchar_t));

/*----------------------------------------------------------------------------*\
Setup the main menu for testing
\*----------------------------------------------------------------------------*/
    status = ProMenuFileRegister((char*)"TkMenu", (char*)"tkmenu.mnu", &menu_id);
    TEST_CALL_REPORT("ProMenuFileRegister()", "ProTestMenu()", status, 
						status != PRO_TK_NO_ERROR);
    status = ProMenubuttonLocationSet((char*)"TkMenu", (char*)"Done", -1);
    TEST_CALL_REPORT("ProMenubuttonLocationSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMenu", (char*)"StringsSelect", 
        (ProMenubuttonAction)ProTestMenuAction, fp, PRO_TEST_MENU_STRINGS);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMenu", (char*)"Activate", 
        (ProMenubuttonAction)ProTestMenuAction, fp, PRO_TEST_MENU_ACTIVATE);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMenu", (char*)"Deactivate", 
        (ProMenubuttonAction)ProTestMenuAction, fp, PRO_TEST_MENU_DEACTIVATE);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    action = PRO_TEST_MENU_DONE;
    status = ProMenubuttonGenactionSet((char*)"TkMenu", (char*)"Done", 
	(ProMenubuttonGenaction)ProTestMenuGenaction, fp, NULL, NULL,
	NULL, NULL, &action);
    TEST_CALL_REPORT("ProMenubuttonGenactionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMenu", (char*)"TkMenu", 
			(ProMenubuttonAction) ProMenuHold, NULL, 0);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);

/*----------------------------------------------------------------------------*\
Setup pre- and post- actions for the done button
\*----------------------------------------------------------------------------*/

    status = ProMenubuttonPreactionSet((char*)"TkMenu", (char*)"Done", 
        (ProMenubuttonAction)ProTestMenuPremode, fp, 0);
    TEST_CALL_REPORT("ProMenubuttonPreactionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonPostactionSet((char*)"TkMenu", (char*)"Done", 
        (ProMenubuttonAction)ProTestMenuPostmode, fp, 0);
    TEST_CALL_REPORT("ProMenubuttonPostactionSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);

/*----------------------------------------------------------------------------*\
Display and Process the main testing menu
\*----------------------------------------------------------------------------*/

    if( !menuTestMode )
    {
        status = ProMenuDatamodeSet((char*)"TkMenu", PRO_B_TRUE);
        TEST_CALL_REPORT("ProMenuDatamodeSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenubuttonDelete((char*)"TkMenu", (char*)"Deactivate", &result);
        TEST_CALL_REPORT("ProMenubuttonDelete()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
	status = ProMenubuttonVisibilitySet((char*)"TkMenu", (char*)"Activate", PRO_B_FALSE);
        TEST_CALL_REPORT("ProMenubuttonVisibilitySet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenuCreate(PROMENUTYPE_MAIN, (char*)"TkMenu", &menu_id);
        TEST_CALL_REPORT("ProMenuCreate()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    }
    else
    {
        status = ProMenuModeSet((char*)"TkMenu", PROMENUMODE_OPERATIONAL);
        TEST_CALL_REPORT("ProMenuModeSet()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenuPush();
        TEST_CALL_REPORT("ProMenuPush()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenuCreate(PROMENUTYPE_MAIN, (char*)"TkMenu", &menu_id);
        TEST_CALL_REPORT("ProMenuCreate()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenubuttonHighlight((char*)"TkMenu", (char*)"StringsSelect");
        TEST_CALL_REPORT("ProMenubuttonHighlight()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        status = ProMenubuttonDeactivate((char*)"TkMenu", (char*)"Deactivate");
        TEST_CALL_REPORT("ProMenubuttonDeactivate()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    }

    status = ProMenuProcess((char*)"TkMenu", &action);
    TEST_CALL_REPORT("ProMenuProcess()", "ProTestMenu()", 
		status, status != PRO_TK_NO_ERROR && status != PRO_TK_E_FOUND);

    status = ProMenuVisibilityGet((char*)"TkMain", &value);
    TEST_CALL_REPORT("ProMenuVisibilityGet()", "ProTestMenu()", 
		status, status != PRO_TK_NO_ERROR && status != PRO_TK_E_FOUND);

    if (value == PRO_B_FALSE)
    {
        status = ProMenuPop();
        TEST_CALL_REPORT("ProMenuPop()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
    }
    fclose(fp);
    return(0);
}

/*============================================================================*\
  Function : ProTestMenuPreMode
  Purpose  : Function used for testing ProMenubuttonPreactionSet()
\*============================================================================*/
int ProTestMenuPremode(FILE *fp, int b)
{
    ProError status;
    ProTKFprintf(fp, "PreAction called\n");
    TEST_CALL_REPORT("ProMenubuttonAction()", "ProTestMenuPremode()", PRO_TK_NO_ERROR, 0);

    if( menuTestMode )
    {
        ProUtilMsgPrint("gen", "TEST %0s", 
			   "Cannot currently leave menu test mode!");
	status = ProMenuCommandPush((char*)"StringsSelect");
        TEST_CALL_REPORT("ProMenuCommandPush()", "ProTestMenu()", 
					status, status != PRO_TK_NO_ERROR);
        return(1);
    }

    return(0);
}

/*============================================================================*\
  Function : ProTestMenuPostMode
  Purpose  : Function used for testing ProMenubuttonPostactionSet()
\*============================================================================*/
int ProTestMenuPostmode(FILE *fp, int b)
{
    TEST_CALL_REPORT("ProMenubuttonAction()", "ProTestMenuPostmode()", PRO_TK_NO_ERROR, 0);

    ProTKFprintf(fp, "PostAction called\n");
    ProUtilMsgPrint("gen", "TEST %0s", "Menu testing complete!");

    return(0);
}


/*============================================================================*\
  Function : ProTestMenuAction
  Purpose  : Available actions for menu picks
\*============================================================================*/
int ProTestMenuAction(FILE *fp, int choice)
{
    ProError status;
    int n_selected;
    wchar_t **selected;
    wchar_t title[20];
    char mess[100], button[20];
    int i;
    static int exit_number=-1;

    TEST_CALL_REPORT("ProMenubuttonAction()", "ProTestMenuAction()", PRO_TK_NO_ERROR, 0);


    switch(choice)
    {
        case PRO_TEST_MENU_DONE:
	    exit_number++;
            switch (exit_number%3)
	    {
	    case 0:
    		ProUtilMsgPrint("gen", "TEST %0s", "Can not leave menu now."
		    " Press once more.");
		status = ProMenuHold();
                TEST_CALL_REPORT("ProMenuHold()", "ProTestMenuAction()", 
					status, status != PRO_TK_NO_ERROR);
		break;
	    case 1: 
		status = ProMenuDeleteWithStatus(choice);
                TEST_CALL_REPORT("ProMenuDeleteWithStatus()", 
		    "ProTestMenuAction()", status, status != PRO_TK_NO_ERROR);
		break;
	    case 2:
	        status = ProMenuDelete();
                TEST_CALL_REPORT("ProMenuDelete()", "ProTestMenuAction()", 
					status, status != PRO_TK_NO_ERROR);
		break;
	    }
	    break;

	case PRO_TEST_MENU_ACTIVATE:
	  status = ProMenubuttonDeactivate((char*)"TkMenu", (char*)"Activate");
            TEST_CALL_REPORT("ProMenubuttonDeactivate()", "ProTestMenuAction()",
					status, status != PRO_TK_NO_ERROR);
	    status = ProMenubuttonActivate((char*)"TkMenu", (char*)"Deactivate");
            TEST_CALL_REPORT("ProMenubuttonActivate()", "ProTestMenuAction()",
					status, status != PRO_TK_NO_ERROR);
	    break;

	case PRO_TEST_MENU_DEACTIVATE:
	    status = ProMenubuttonActivate((char*)"TkMenu", (char*)"Activate");
            TEST_CALL_REPORT("ProMenubuttonActivate()", "ProTestMenuAction()",
					status, status != PRO_TK_NO_ERROR);
	    status = ProMenubuttonDeactivate((char*)"TkMenu", (char*)"Deactivate");
            TEST_CALL_REPORT("ProMenubuttonDeactivate()", "ProTestMenuAction()",
					status, status != PRO_TK_NO_ERROR);
	    break;

	case PRO_TEST_MENU_STRINGS:
	    status = ProMenubuttonUnhighlight((char*)"TkMenu", (char*)"StringsSelect");
            TEST_CALL_REPORT("ProMenubuttonUnhighlight()", 
    		"ProTestMenuAction()", status, status != PRO_TK_NO_ERROR);
	    ProStringToWstring(title, (char*)"Strings");
	    status = ProMenuStringsSelect(title, strings, 1, help, &selected, 
	    							&n_selected);
            TEST_CALL_REPORT("ProMenuStringsSelect()", "ProTestMenuAction()", 
	    				status, status != PRO_TK_NO_ERROR);
	    for( i = 0; i < n_selected; i++ )
	    {
		ProWstringToString(button, selected[i]);
		ProTKSprintf(mess, "%s was selected", button); 
		ProTKFprintf(fp, "%s\n", mess);
	        ProUtilMsgPrint("gen", "TEST %0s", mess);
	    }
	    break;

	default:
	    break;
    }

    menuTestMode = PRO_B_FALSE;

    return(0);
}

/*============================================================================*\
  Function : ProTestMenuGenaction
  Purpose  : Available actions for menu picks
\*============================================================================*/
int ProTestMenuGenaction(FILE *fp, void *b, void *c, void *d, void *e, int *f)
{
    TEST_CALL_REPORT("ProMenubuttonGenaction()",
					"ProTestMenuGenaction()", PRO_TK_NO_ERROR, 0);
    ProTestMenuAction(fp, *f);
    return (0); 
}

/*============================================================================*\
  Function : ProTestMacro
  Purpose  : Run macro entered by user
\*============================================================================*/
int ProTestMacro( void* p_dummy, int int_dummy )
{
#ifndef PT_PRODUCTS_BUILD
    ProError	    status;
    ProMacro	    w_macro;
    ProMacro	    w_default_macro;
    char	    default_macro[] = "#MODE;#PART;#LIST;";
    char	    buffer[ PRO_LINE_SIZE ];


    ProStringToWstring( w_default_macro, default_macro );
    
    ProTKSprintf( buffer, "Enter macro keys [%s]: ", default_macro );
    ProUtilMsgPrint( "gen", "TEST %0s", buffer );
    if( !ProUtilStringGet( w_macro, w_default_macro, PRO_MACRO_SIZE ) )
	return -1;

    status = ProMacroLoad( w_macro );
    TEST_CALL_REPORT( "ProMacroLoad()", "ProTestMacro()", 
			status, status != PRO_TK_NO_ERROR );
#endif

    return 0;
}