// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

#include <sm_resource.h>
#include <sm_w_batch.h>

windows_batch_mode* winbmode;

// set up the main window
void batch_application::InitMainWindow(void)
{
// create a main TWindow
  heap_check(tfwp = new TFrameWindow(NULL, "WMIDAS 17.00 (Batch mode)"));

// make this the main window
  SetMainWindow(tfwp);

  tfwp->Attr.X = 0;
  tfwp->Attr.Y = 0;
  tfwp->Attr.W = 640 + 2 * frame_width();
  tfwp->Attr.H = 120;
  tfwp->Attr.Style = tfwp->Attr.Style & ~WS_THICKFRAME;
  tfwp->Attr.Style = tfwp->Attr.Style & ~WS_MAXIMIZEBOX;

  busy_display = new TStatic((PTWindow)MainWindow, ID_BUSY_DISPLAY,
                              "Static1", 0, 40, 640 + frame_width(), 20,
                               150);
  status_display = new TStatic((PTWindow)MainWindow, ID_BUSY_DISPLAY,
                               "Static2", 0, 20, 640 + frame_width(), 20,
                                150);
  echo_display = new TStatic((PTWindow)MainWindow, ID_BUSY_DISPLAY,
                              "Static3", 0, 0, 640 + frame_width(), 20,
                               150);

  message_display = new TStatic((PTWindow)MainWindow, ID_BUSY_DISPLAY,
                              "Static4", 0, 60, 640 + frame_width(), 20,
                               150);

  busy_display->Attr.Style = busy_display->Attr.Style & ~WS_VISIBLE;
  busy_display->Attr.Style = busy_display->Attr.Style | SS_LEFTNOWORDWRAP;
  busy_display->Attr.X = 0;
  busy_display->Attr.Y = 40;

  status_display->Attr.Style = status_display->Attr.Style & ~WS_VISIBLE;
  status_display->Attr.Style = status_display->Attr.Style | SS_LEFTNOWORDWRAP;
  status_display->Attr.X = 0;
  status_display->Attr.Y = 20;

  echo_display->Attr.Style = echo_display->Attr.Style & ~WS_VISIBLE;
  echo_display->Attr.Style = echo_display->Attr.Style | SS_LEFTNOWORDWRAP;
  echo_display->Attr.X = 0;
  echo_display->Attr.Y = 0;

  message_display->Attr.Style = message_display->Attr.Style & ~WS_VISIBLE;
  message_display->Attr.Style = message_display->Attr.Style | SS_LEFTNOWORDWRAP;
  message_display->Attr.X = 0;
  message_display->Attr.Y = 60;

// load the MIDAS icon
  LoadIcon("midas.ico");
}

// destructor. This is just to be neat and tidy
batch_application::~batch_application(void)
{ delete tfwp;
}

// constructor
windows_batch_mode::windows_batch_mode(const char* filename,
                                       const float rev)
  : batch_mode(filename, rev),
    win_printer_pointer(0),
    winp(0)
{ app = new batch_application("An application", this);
  app->InitApplication();
  app->InitInstance();

  _generate_command_list();
  _initialise();
}

// destructor
windows_batch_mode::~windows_batch_mode(void)
{ destroy(win_printer_pointer);
  destroy(winp);
}

// initialise variables
void windows_batch_mode::_initialise(void)
{ batch_mode::_initialise();

// see comments in sm_w_mode.cc regarding the size of the printer
  const int printer_width = 1350, //2700,      // 9 x 6.5 on the page
            printer_height = 975; //1950;
  heap_check(winp = new popup(0, 0, 0, printer_width, printer_height,
                               /* (PTWindow)MainWindow */ NULL, "NO TITLE", false));

  winp->Attr.W = printer_width;
  winp->Attr.H = printer_height;
  winp->init_bitmap();

  heap_check(win_printer_pointer = new windows_printer(winp, false));
  Hardcopy_device = win_printer_pointer;
  Pad = Hardcopy_device;
}

// generate the list of known commands
void windows_batch_mode::_generate_command_list(void)
{ known_commands += "!";
  dispatch_functions += &batch_mode::_batch_file_comment;

  known_commands += "COMMENT";
  dispatch_functions += &batch_mode::_comment;

  known_commands += "ECHO";
  dispatch_functions += &batch_mode::_echo;

  known_commands += "EXIT";
  dispatch_functions += &batch_mode::_exit;

  known_commands += "INFILETYPE";
  dispatch_functions += &batch_mode::_infiletype;
 
// _open is overloaded, so we need the following trick
//  typedef int (batch_mode::*PMFSS)(DREstring&, DREstring&);
  
  known_commands += "OPEN";
  dispatch_functions += &batch_mode::_Open; 

  known_commands += "PLOT";
  dispatch_functions += &batch_mode::_plot;
  
  known_commands += "PLOTMODE";
  dispatch_functions += &batch_mode::_plotmode;
  
  known_commands += "VERSION";
  dispatch_functions += &batch_mode::_version;

  known_commands += "WAIT";
  dispatch_functions += &windows_batch_mode::_wait;

  known_commands += "SHOW";
  dispatch_functions += &batch_mode::_show;
  
  known_commands += "PLOTALL";
  dispatch_functions += &batch_mode::_plotall;  

  known_commands += "SET";
  dispatch_functions += &windows_batch_mode::_set;

  known_commands += "VERBOSE";
  dispatch_functions += &batch_mode::_verbose_toggle;
 
  known_commands += "FIRST"; 
  dispatch_functions += &batch_mode::_first;
  
  known_commands += "NEXT";
  dispatch_functions += &batch_mode::_next;
}

// SET command
int windows_batch_mode::_set(DREstring& c, DREstring& p)
{ enum {QUERY = 1, DURATION, SCMODE, PRAMODE, STARTCHANNEL, ENDCHANNEL,
        PLOTMODE, FILETYPE };

  glist valid_parms;

  valid_parms += "?";			// must be first
  valid_parms += "DURATION";
  valid_parms += "SCMODE";
  valid_parms += "PRAMODE";
  valid_parms += "STARTCHANNEL";
  valid_parms += "ENDCHANNEL";
  valid_parms += "PLOTMODE";
  valid_parms += "FILETYPE";

  int subcommand_number;
  DREstring subcommand;
  const int space_position = find(p, " ");

  if (space_position) then
    subcommand = subDREstring(p, 1, space_position - 1).toupper();
  else
    subcommand = p.toupper();
  if (!(subcommand_number = valid_string(subcommand, valid_parms))) then
    error((DREstring)"in _set(DREstring&, DREstring&) : command " + c +
          (DREstring)" " + p + (DREstring)" unknown");

  switch (subcommand_number)
  { case QUERY :
             { DREstring command_list;
               for (int n = 2; n <= valid_parms.length(); n++)
               { command_list += (DREstring)(char*)valid_parms[n];
                 command_list += "\r\n";
               }
               error((DREstring)"\r\nvalid subcommands are: \r\n" + command_list);
               break;
             }
    case DURATION :
             Duration = atoi(subDREstring(p, space_position + 1));
             verbose((DREstring)"Plot duration now " + DREstring(Duration, 10));
             break;
    case SCMODE :
             _set_scmode(subDREstring(p, space_position + 1));
             break;
    case PRAMODE :
             _set_pramode(subDREstring(p, space_position + 1));
             break;
    case STARTCHANNEL :
             Start_channel = atoi(subDREstring(p, space_position + 1));
             verbose((DREstring)"Start channel now " + DREstring(Start_channel, 10));
             break;
    case ENDCHANNEL :
             End_channel = atoi(subDREstring(p, space_position + 1));
             verbose((DREstring)"End channel now " + DREstring(End_channel, 10));
             break;
    case PLOTMODE :
             _set_plotmode(subDREstring(p, space_position + 1));
             break;
    case FILETYPE :
             _infiletype((DREstring)"SET FILETYPE",
                         subDREstring(p, space_position + 1));
             break;
  }
  return 0;
}

void windows_batch_mode::execute(void)
{ istream& input = *file;

  while ((input >> command_line).good())
  { if (command_line.length()) then
    { _parse_command_line();
      (this->*_current_dispatch_function)(_current_command,
                                          _current_parameters);
    }
  }
}

void windows_batch_mode::busy(DREstring& msg)
{ DREstring t = "BUSY: MIDAS is busy...";

  if ((int)msg) then
	t += ((DREstring)"(" + msg + (DREstring)")");

  app->busy_display->Show(SW_SHOWNORMAL);
  app->busy_display->SetText(t);
}

void windows_batch_mode::unbusy(void)
{ DREstring t = "BUSY:";

  app->busy_display->SetText(t);
}

void windows_batch_mode::display_status(DREstring& msg)
{ DREstring t = (DREstring)"STATUS: " + msg;

  app->status_display->Show(SW_SHOWNORMAL);
  app->status_display->SetText(t);
}

void windows_batch_mode::echo_command(DREstring& cmd)
{ DREstring t = (DREstring)"ECHO: " + cmd;

  app->echo_display->Show(SW_SHOWNORMAL);
  app->echo_display->SetText(t);
}

int windows_batch_mode::_wait(DREstring&, DREstring&)
{ MessageBox(NULL, "Press button to Continue", "MIDAS -- WAIT", MB_OK);
  return 0;
}

void windows_batch_mode::_show_pra_modes(void)
{ if (!Edr_f) then
    fatal_error("No file open");
  else
    display_message((DREstring)"PRAMODES present are: " + _pra_modes_string());
}

void windows_batch_mode::_show_range(void)
{ if (!Edr_f) then
    fatal_error("No file open");
  else 
    display_message((DREstring)"TIME RANGE : " + _time_range_string());
}

void windows_batch_mode::_show_sc_mode(void)
{ display_message((DREstring)"Current SCMODE is : " +
                  _sc_mode_name[Current_sc_mode]);
}

void windows_batch_mode::_show_sc_modes(void)
{ if (!Edr_f) then
    fatal_error("No file open");
  else
    display_message((DREstring)"SCMODES present are: " + _sc_modes_string());
}

void windows_batch_mode::display_message(DREstring& msg)
{ DREstring t = (DREstring)"MESSAGE: " + msg;

  app->message_display->Show(SW_SHOWNORMAL);
  app->message_display->SetText(t);
}



