// -*- Mode: c++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil -*-
// NOTE: the first line of this file sets up source code indentation rules
// for Emacs; it is also a hint to anyone modifying this file.

// File         : the_main.cxx
// Author       : Paul A. Koshevoy
// Created      : Sun Jun 23 21:53:36 MDT 2002
// Copyright    : (C) 2002
// License      : GPL.
// Description  :

// configure default trail recording behaviour based on build type:
#if 1
// #ifndef NDEBUG
static const bool RECORD_BY_DEFAULT = true;
#else
static const bool RECORD_BY_DEFAULT = false;
#endif

// system includes:
#include <iostream>

// thelib includes:
#include <Qt/the_qt_mutex.hxx>
#include <Qt/the_qt_thread.hxx>
#include <Qt/the_qt_trail.hxx>
#include <Qt/the_qt_view.hxx>
#include <Qt/the_qt_desktop_metrics.hxx>
#include <opengl/OpenGLCapabilities.h>
#include <opengl/glsl.hxx>

// Qt includes:
#include <QTimer>
#include <QtPlugin>

// local includes:
#include "the_main_window.hxx"

// import the image plugins:
#if 0 // def QT_NO_DEBUG
Q_IMPORT_PLUGIN(qtiff)
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
Q_IMPORT_PLUGIN(qmng)
#endif

// namespace accessors:
using std::cerr;
using std::cout;
using std::endl;


//----------------------------------------------------------------
// the_application_t
// 
class the_application_t : public the_qt_trail_t
{
public:
  the_application_t(int & argc, char ** argv, bool record_by_default = false):
    the_qt_trail_t(argc, argv, record_by_default)
  {}
  
protected:
  bool event(QEvent * e)
  {
    if (e->type() != QEvent::FileOpen)
    {
      return the_qt_trail_t::event(e);
    }
    
    // handle the apple event to open a document:
    QString fn_load = static_cast<QFileOpenEvent *>(e)->file();
    the_main_window_t * main_window =
      dynamic_cast<the_main_window_t *>(the_document_ui_t::doc_ui());
    // FIXME: main_window->file_open(fn_load);
    return true;
  }
};

//----------------------------------------------------------------
// usage
// 
static void
usage(char ** argv, const char * message = NULL, int exit_code = 1)
{
  cerr << "USAGE: " << argv[0]
       << " [-ask] [-replay in.txt] [-record out.txt]"
       << " [-wait seconds] [file_to_open]" << endl
       << endl
       << "\t-ask:    confirm every major event prior to playback" << endl
       << "\t-replay: replay a given event trail file" << endl
       << "\t-record: record events to a specific file" << endl
       << "\t-wait:   number of seconds to wait for a milestone" << endl
       << endl;
  
  if (RECORD_BY_DEFAULT)
  {
    cerr << "NOTE:\tIf -record is not specified, the event trail file\n"
	 << "\tname is picked automatically as .dont_record.txt"<<endl;
  }
  
  if (message != NULL)
  {
    cerr << "NOTE:\t" << message << endl;
  }
  
  exit(exit_code);
}

//----------------------------------------------------------------
// main
// 
int
main(int argc, char ** argv)
{
  // this is so that the printouts look better:
  cout.precision(2);
  cout.setf(ios::scientific);
  
  // necessary on some platforms for resources stored in a static library:
  Q_INIT_RESOURCE(bernstein);
  
  // parse command line for input/output trail file options:
  the_application_t A(argc, argv, RECORD_BY_DEFAULT);
  
  // setup the desktop metrics provider:
  the_desktop_metrics(new the_qt_desktop_metrics_t());
  
  // parse the rest of the command line:
  QString fn_load;
  
  for (int i = 1; i < argc; i++)
  {
    if (strcmp(argv[i], "--help") == 0 ||
	strcmp(argv[i], "-help") == 0 ||
	strcmp(argv[i], "-h") == 0)
    {
      usage(argv, argv[i], 0);
    }
    else
    {
      QFileInfo fi(argv[i]);
      if (!fi.isFile())
      {
	usage(argv, the_text_t("unrecogrinzed parameter: ") + argv[i], 1);
      }
      else
      {
	fn_load = QString(argv[i]);
      }
    }
  }
  
  // setup thread storage for the main thread:
  set_the_thread_storage_provider(the_qt_thread_t::thread_storage);
  the_qt_thread_t MAIN_THREAD_DUMMY;
  MAIN_THREAD_DUMMY.set_stopped(false);
  
  // setup thread and mutex interface creators:
  the_mutex_interface_t::set_creator(the_qt_mutex_t::create);
  the_thread_interface_t::set_creator(the_qt_thread_t::create);
  
  // setup the UI:
  the_main_window_t main_window;
  main_window.show();
  
  OpenGLCapabilities & gl0 = OpenGL(0, main_window.view(0));
  gl0.max_texture_ = OpenGLCapabilities::maxTextureSize(GL_LUMINANCE,
							GL_LUMINANCE,
							GL_UNSIGNED_BYTE);
  glsl_init();
  
  // load the parameter:
  if (fn_load.size() != 0)
  {
    cerr << "FIXME: load " << qPrintable(fn_load) << endl;
    // main_window.go_here(fn_load);
  }
  
  // give the window manager a little time to set up the window decorations,
  // and then start the trail replay:
  QTimer::singleShot(178, &A, SLOT(replay()));
  
  // start the event loop:
  int result = A.exec();
  return result;
}

