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


#include <ProToolkit.h>
#include <ProMessage.h>
#include <PTApplsUnicodeUtils.h>
#include <TestError.h>
#include <ProUtil.h>

#define MAX_IN_LEN PRO_NAME_SIZE

/*==================================================================*\
FUNCTION: UserMessageDemo()
PURPOSE:  Display messages and read user input.
\*==================================================================*/
ProError UserMessageDemo()
{
  int           i1, i2;
  const int     default_int = 0;
  double        d;
  int           irange[2] = {0, 10};
  ProName       wstr, default_wstr;
  ProCharName   str;
  ProLine       msg_line;
  ProCharLine   line;
  ProError      err;
  ProFileName   msgfil;
  FILE          *msg_test;
/*------------------------------------------------------------------*\
  Set up the name of the message file.
\*------------------------------------------------------------------*/
  ProStringToWstring (msgfil, "msg_ugmessage.txt");
/*------------------------------------------------------------------*\
  Read an integer without a default value.  Message will display as a 
  prompt.
\*------------------------------------------------------------------*/
  err = ProMessageDisplay (msgfil, "USER Enter any integer: ");
  err = ProMessageIntegerRead (NULL, &i1);
  if (err != PRO_TK_NO_ERROR)
      return (err);
/*------------------------------------------------------------------*\
  Read an integer with a restricted range and a default value.  Message 
  will display as a prompt.
\*------------------------------------------------------------------*/
  err = ProMessageDisplay (msgfil,
      "USER Enter any integer between %0d and %1d: |||%2d", &irange[0],
      &irange[1], &default_int);
  err = ProMessageIntegerRead (irange, &i2);
  if (err != PRO_TK_NO_ERROR && err != PRO_TK_GENERAL_ERROR)
      return (err);
/*------------------------------------------------------------------*\
  If user entered the default value - warn the user.  Message will 
  display as a warning.
\*------------------------------------------------------------------*/
  if (err == PRO_TK_GENERAL_ERROR)
    {
      i2 = default_int;   /*  Using the default  */
      err = ProMessageDisplay (msgfil,
			       "USER Warning: using default value", &default_int);
    }
/*------------------------------------------------------------------*\
  Read a double without a default value.  Message will display as a 
  prompt.
\*------------------------------------------------------------------*/
  err = ProMessageDisplay (msgfil, "USER Enter any double: ");
  err = ProMessageDoubleRead (NULL, &d);
  if (err != PRO_TK_NO_ERROR)
      return (err);
/*------------------------------------------------------------------*\
  Read a string with a default value.  Message will display as a 
  prompt.
\*------------------------------------------------------------------*/
  ProStringToWstring (default_wstr, "default string");
  err = ProMessageDisplay (msgfil, "USER Enter any string: |||%0w",
      default_wstr);
  err = ProMessageStringRead (MAX_IN_LEN, wstr);
  if (err != PRO_TK_NO_ERROR && err != PRO_TK_GENERAL_ERROR)
      return (err);
  if (err == PRO_TK_GENERAL_ERROR)
    ProUtilWstrcpy (wstr, default_wstr);  /*  Using the default  */
/*------------------------------------------------------------------*\
  Write a message that states the values entered.  Message will display
  as info.
\*------------------------------------------------------------------*/
  err = ProMessageDisplay (msgfil,
      "USER Values entered were %0d, %1d, %2(5.3)f, %3w", &i1, &i2,
      &d, wstr);
/*------------------------------------------------------------------*\
  Write the values to a file.
\*------------------------------------------------------------------*/
  msg_test = PTApplsUnicodeFopen ("msg_test.dat", "w");
  if (msg_test != NULL)
  {
      err = ProMessageToBuffer (msg_line, msgfil,
          "USER Values entered", wstr, &d, &i2, &i1);

      ProWstringToString (line, msg_line);
      ProTKFprintf (msg_test,
          "ProMessageToBuffer output |%s|\n", line);
      fclose (msg_test);
  }
  return (PRO_TK_NO_ERROR);
}

#undef MAX_IN_LEN