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


#include <ProUI.h>
#include <ProUIPushbutton.h>
#include <ProUIDialog.h>
#include <ProUIInputpanel.h>
#include <UtilString.h>
#include <ProUILabel.h>
#define UGUIPROMPT "ugutiluiprompt"

/*====================================================================*\
STRUCTURE: UserUtilUIPromptData
PURPOSE:   Pass relevant data to UI callback functions
\*====================================================================*/
typedef struct 
{
	wchar_t** input;
	int limit;
} UserUtilUIPromptData;

/*====================================================================*\
FUNCTION: UserPromptUIOKAction
PURPOSE:  Action function for OK button press
\*====================================================================*/
static int UserPromptUIOKAction (char* dialog, char* component, 
									ProAppData data)
{
	UserUtilUIPromptData* prompt_data = (UserUtilUIPromptData*)data;
	wchar_t** input = prompt_data->input;

	ProUIInputpanelValueGet (UGUIPROMPT, "PromptNamePanel", input);

	ProUIDialogExit (UGUIPROMPT, PRO_TK_NO_ERROR);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
FUNCTION: UserPromptUICancelAction
PURPOSE:  Action function for Cancel button press
\*====================================================================*/
static int UserPromptUICancelAction (char* dialog, char* component, 
										ProAppData data)
{	
	ProUIDialogExit (UGUIPROMPT, PRO_TK_GENERAL_ERROR);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
FUNCTION: UserPromptUIInputAction
PURPOSE:  Action function for input of text in the prompt inputpanel -
			activates the OK button if there is some content
\*====================================================================*/
static int UserPromptUIInputAction (char* dialog, char* component, 
										ProAppData data)
{	
	wchar_t* value = NULL;
	UserUtilUIPromptData* prompt_data = (UserUtilUIPromptData*)data;
	int limit_chars = prompt_data->limit;

	ProUIInputpanelValueGet (UGUIPROMPT, "PromptNamePanel", &value);
	
	if (value != NULL && ProUtilWstrLen (value) > 0)
	{
		ProUIPushbuttonEnable (UGUIPROMPT, "OK");

/*--------------------------------------------------------------------*\
		Prevent users from entering too many characters
\*--------------------------------------------------------------------*/
		if (limit_chars != PRO_VALUE_UNUSED && 
				ProUtilWstrLen (value) > limit_chars)
		{
			wchar_t* truncated_value = 
				(wchar_t*)calloc (limit_chars + 1, sizeof (wchar_t));
			memcpy (truncated_value, value, limit_chars * sizeof (wchar_t));
			ProUIInputpanelValueSet (UGUIPROMPT, "PromptNamePanel", 
										truncated_value);
			free (truncated_value);	
		}
	}
	else
		ProUIPushbuttonDisable (UGUIPROMPT, "OK");

	ProWstringFree (value);

	return PRO_TK_NO_ERROR;
}

/*====================================================================*\
FUNCTION: UserPromptUI
PURPOSE:  Prompts user to enter a string
\*====================================================================*/
int UserPromptUI (char* new_title, char* new_label, int max_length, 
					wchar_t** input)
{
	int exit_status;
	UserUtilUIPromptData data;
	data.input = input;
	data.limit = max_length;

	ProUIDialogCreate (UGUIPROMPT, UGUIPROMPT);

/*--------------------------------------------------------------------*\
	Set up dialog title and prompt
\*--------------------------------------------------------------------*/
	if (new_title != NULL)
	{
		ProPath new_title_line;
		ProStringToWstring (new_title_line, new_title);

		ProUIDialogTitleSet (UGUIPROMPT, new_title_line);
	}

	if (new_label != NULL)
	{
		ProPath new_label_line;
		ProStringToWstring (new_label_line, new_label);

		ProUILabelTextSet (UGUIPROMPT, "PromptNameLabel", 
							new_label_line);
	}

	ProUIPushbuttonDisable (UGUIPROMPT, "OK");

/*--------------------------------------------------------------------*\
	Set up the component action functions
\*--------------------------------------------------------------------*/
	ProUIPushbuttonActivateActionSet (UGUIPROMPT, "OK", 
										(ProUIAction)UserPromptUIOKAction,
										(ProAppData)&data);
	ProUIPushbuttonActivateActionSet (UGUIPROMPT, "Cancel", 
										(ProUIAction)UserPromptUICancelAction,
										NULL);

	ProUIInputpanelInputActionSet (UGUIPROMPT, "PromptNamePanel", 
										(ProUIAction)UserPromptUIInputAction,
										(ProAppData)&data);

	ProUIDialogActivate (UGUIPROMPT, &exit_status);

	ProUIDialogDestroy (UGUIPROMPT);

	return exit_status;
}