/*****************************************************************************\
*                                                                             *
*  Name   : list_chooser                                                      *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*  Purpose:                                                                   *
*                                                                             *
*    Shows a list of options from a provided file and gets the user to pick   *
*  one of the options.                                                        *
*                                                                             *
*******************************************************************************
* Copyright (c) 1995-$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/byte_array.h>
#include <basis/istring.h>
#include <opsystem/application_shell.h>
#include <opsystem/byte_filer.h>
#include <loggers/console_logger.h>
#include <opsystem/application_shell.h>
#include <opsystem/ini_config.h>
#include <data_struct/static_memory_gremlin.h>
#include <textual/byte_format.h>
#include <textual/parser_bits.h>

#ifdef __WIN32__
  #include <mfc_ext/list_dialog.h>
  #include <mfc_ext/tiny_app.cpp>
  #include <mfc_ext/tiny_shell.h>
#endif

#ifdef __WIN32__
  #define base_implementation tiny_shell
#else
  #define base_implementation application_shell
#endif

class list_chooser : public base_implementation
{
public:
  list_chooser();

  IMPLEMENT_CLASS_NAME("list_chooser");

  int execute();
    // performs main body of test.
};

list_chooser::list_chooser()
#ifdef __WIN32__
: base_implementation("List Chooser", "list_chooser")
#else
: base_implementation("list_chooser")
#endif
{}

int list_chooser::execute()
{
#ifdef __WIN32__

  ShowWindow(SW_HIDE);  // hide the tiny shell window.

  // set up our output file right now.
  istring ini_name = portable::env_string("TEMP") + "/" + "list_chooser.ini";
  ini_configurator ini(ini_name, configurator::RETURN_ONLY);
  ini.put("answer", "okay", "0");
  ini.put("answer", "choice", "");

  if (__argc < 4) {
    log("This program needs two parameters.  The first is the caption that will be used");
    log("in the list box.  The second is the number of lines to skip in the input file.");
    log("The third parameter is the filename from which to load the options that will");
    log("be shown in the list.");
    PostMessage(WM_CLOSE);

    return 34;
  }

  istring caption = __argv[1];
  int skip_count = istring(__argv[2]).convert(0);
  istring filename = __argv[3];

  byte_filer option_file(filename, "rb");
  if (!option_file.good()) {
    log(istring("Could not open the file named: ") + filename);
    PostMessage(WM_CLOSE);

    return 35;
  }

  list_dialog list(this, caption);

  int skippy = 0;
  while (true) {
    istring next_option;
    int chars_read = option_file.getline(next_option, 255);
    if (!chars_read) break;
    skippy++;
    if (skippy <= skip_count) continue;  // not ready yet.
    // clean EOL chars off.
    next_option.shrink();
    while (next_option[0] == ' ') {
      next_option.zap(0, 0);
    }
    while (parser_bits::is_eol(next_option[next_option.end()])
        || (next_option[next_option.end()] == ' ') ) {
      next_option.zap(next_option.end(), next_option.end());
    }
//log(text_dump(byte_array(next_option.length() + 1, (byte *)next_option.s())));
    list.add_string(next_option.s(), true);
  }
  option_file.close();

  list.SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

  if (list.DoModal() != IDOK) {
    PostMessage(WM_CLOSE);
    return 36;
  }

  istring curr;
  list.get_current(curr);

  while (parser_bits::is_eol(curr[curr.end()])) {
    curr.zap(curr.end(), curr.end());
  }

  ini.put("answer", "choice", curr);
  ini.put("answer", "okay", "1");

  PostMessage(WM_CLOSE);

#endif //win32

  return 0;
}

HOOPLE_MAIN(list_chooser, )

