/*****************************************************************************\
*                                                                             *
*  Name   : query_text                                                        *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Asks for a piece of information from the user with an associated         *
*  message that describes what is expected.                                   *
*                                                                             *
*******************************************************************************
* Copyright (c) 2006-$now By Author.  This program is free software; you can  *
* redistribute it and/or modify it under the terms of the GNU General Public  *
* License as published by the Free Software Foundation; either version 2 of   *
* the License or (at your option) any later version.  This is online at:      *
*     http://www.fsf.org/copyleft/gpl.html                                    *
* Please send any updates to: fred@gruntose.com                               *
\*****************************************************************************/

#include <basis/convert_utf.h>
#include <basis/function.h>
#include <basis/istring.h>
#ifdef _AFXDLL
  #include <mfc_ext/info_edit.h>
  #include <mfc_ext/tiny_app.cpp>
  #include <mfc_ext/tiny_shell.h>
#else
  #include <opsystem/application_shell.h>
#endif
#include <opsystem/ini_config.h>
#include <data_struct/static_memory_gremlin.h>

#include <stdio.h>
#include <stdlib.h>

#define BASE_LOG(to_print) program_wide_logger().log(to_print)
#define LOG(to_print) STAMPED_EMERGENCY_LOG(program_wide_logger(), to_print)

#ifdef _AFXDLL
class query_text : public tiny_shell 
#else
class query_text : public application_shell 
#endif
{
public:
#ifdef _AFXDLL
  query_text() : tiny_shell("not to be seen", "query_text") {}
#else
  query_text() : application_shell("query_text") {}
#endif
  virtual ~query_text() {}
  IMPLEMENT_CLASS_NAME("query_text");
  virtual int execute();
};

////////////////////////////////////////////////////////////////////////////

const char *help_info = \
"This program requires a title and a description as the first and second\r\n\
parameters.  The third parameter is the initial value for the edit field\r\n\
that the user fills in.  The fourth parameter must be an INI file where\r\n\
the result of the edit will be written.  The INI file format is as follows:\r\n\
\t[query]\r\n\
\tanswer=USER-ENTERED-INFORMATION";

int query_text::execute()
{
  FUNCDEF("execute");



#ifdef _AFXDLL
  ShowWindow(SW_HIDE);
#endif
  if (__argc < 4) {
#ifdef _AFXDLL
    MessageBox(to_unicode_temp(help_info),
        to_unicode_temp("Insufficient Parameters"), MB_OK);
    PostMessage(WM_CLOSE);
#else
    BASE_LOG(help_info);
#endif
    return 1;
  }
  istring title = __argv[1];
  istring description = __argv[2];
  istring initial_edit = __argv[3];
  istring outfile = __argv[4];

  // process description since we get handed some unresolved escape codes.
  for (int i = 0; i < description.length(); i++) {
    if ( (description[i] == '\\') && (i <= description.length() - 4) ) {
      // we got the escape code start at least and enough text for it...
      if ( (description[i + 1] == 'r') && (description[i + 2] == '\\')
          && (description[i + 3] == 'n') ) {
        // yep, this is our escape code.
        description.zap(i, i + 3);  // whack the code.
        description.insert(i, "\r\n");  // replace with the real code.
      }
    }
  }

  istring to_return;
  while (!to_return) {
#ifdef _AFXDLL
    info_edit querier(0, initial_edit.s(), 100, description.s(), title.s());
    if (querier.DoModal() == IDOK) {
      // grab the string they entered and return it.
      to_return = querier.text();
    } else break;  // get out of loop due to unexpected return.
#else
    BASE_LOG(title);
    BASE_LOG("-------");
    BASE_LOG(description);
    BASE_LOG(istring("default value: ") + initial_edit);
    BASE_LOG("Please enter a value for the query...");
    char buff[2049];
    if (fscanf(stdin, "%2047s", buff) >= 1)
      to_return = buff;

BASE_LOG(istring("got answer of: \"") + to_return + "\"");

#endif
  }

  if (to_return.t()) {
    ini_configurator ini(outfile, configurator::RETURN_ONLY);
    ini.store("query", "answer", to_return);
#ifdef _AFXDLL
    PostMessage(WM_CLOSE);
#endif
    return 0;  // non-error return.
  } else {
    // this is a failure outcome.  the user probably cancelled.
#ifdef _AFXDLL
    PostMessage(WM_CLOSE);
#endif
    return 1;  // error return.
  }
}

////////////////////////////////////////////////////////////////////////////

HOOPLE_MAIN(query_text, )

