/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder 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.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * builder_init.cpp
 *
 *  Created on: Dec 15, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include 
#include 
#include "Actions/ActionHistory.hpp"
#include "Actions/ActionExceptions.hpp"
#include "builder_init.hpp"
#include "cleanUp.hpp"
#include "CodePatterns/Log.hpp"
#include "Graph/BondGraph.hpp"
#include "Parser/ChangeTracker.hpp"
#include "Parser/FormatParserStorage.hpp"
#include "UIElements/UIFactory.hpp"
#include "UIElements/CommandLineUI/CommandLineParser.hpp"
#include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
#ifdef USE_GUI_QT
#include "UIElements/Qt4/QtUIFactory.hpp"
#endif
#include "UIElements/TextUI/TextUIFactory.hpp"
#include "UIElements/MainWindow.hpp"
#include "version.h"
#include "World.hpp"
#include 
/** Print some initial information output the program.
 *
 */
void ProgramHeader()
{
  // print version check and copyright notice
  cout << MOLECUILDERVERSION << endl;
  cout << "MoleCuilder comes with ABSOLUTELY NO WARRANTY; for details type" << endl;
  cout << "`molecuilder --warranty'." << endl;
  cout << "MoleCuilder - to create and alter molecular systems." << endl;
  cout << "Copyright (C) 2008-2012  University Bonn. All rights reserved." << endl;
  cout << "This is free software, and you are welcome to redistribute it under" << endl;
  cout << "certain conditions; type `molecuilder --help-redistribute' for details" << endl;
  cout << endl;
}
/** General stuff to intialize before UI.
 *
 */
void initGeneral()
{
  // while we are non interactive, we want to abort from asserts
  ASSERT_DO(Assert::Abort);
  ASSERT_HOOK(dumpMemory);
  ProgramHeader();
  setVerbosity(1);
  // need to init the history before any action is created
  MoleCuilder::ActionHistory::init();
  // from this moment on, we need to be sure to deeinitialize in the correct order
  // this is handled by the cleanup function
  atexit(cleanUp);
}
/** Initialize specific UIFactory.
 *
 * @param argc argument count
 * @param argv argument array
 */
void initUI(int argc, char **argv)
{
  std::string BondGraphFileName("\n");
  // Parse command line options and if present create respective UI
  // construct bond graph
  if (boost::filesystem::exists(BondGraphFileName)) {
    std::ifstream input(BondGraphFileName.c_str());
    if ((input.good()) && (World::getInstance().getBondGraph()->LoadBondLengthTable(input))) {
      LOG(0, "Bond length table loaded successfully.");
    } else {
      ELOG(1, "Bond length table loading failed.");
    }
    input.close();
  }
  // handle remaining arguments by CommandLineParser
  if (argc>1) {
    LOG(0, "Setting UI to CommandLine.");
    CommandLineParser::getInstance().InitializeCommandArguments();
    CommandLineParser::getInstance().Run(argc,argv);
    UIFactory::registerFactory(new CommandLineUIFactory::description());
    UIFactory::makeUserInterface("CommandLine");
  } else {
    // In the interactive mode, we can leave the user the choice in case of error
    ASSERT_DO(Assert::Ask);
    #ifdef USE_GUI_QT
      LOG(0, "Setting UI to Qt4.");
      UIFactory::registerFactory(new QtUIFactory::description());
      UIFactory::makeUserInterface("Qt4");
    #else
      LOG(0, "Setting UI to Text.");
      cout << MOLECUILDERVERSION << endl;
      UIFactory::registerFactory(new TextUIFactory::description());
      UIFactory::makeUserInterface("Text");
    #endif
  }
}
/** Create MainWindow and displays.
 * I.e. here all the Actions are parsed and done.
 */
void doUI()
{
  MainWindow *mainWindow = UIFactory::getInstance().makeMainWindow();
  try {
    mainWindow->display();
  } catch(ActionFailureException &e) {
    std::cerr << "Action " << *boost::get_error_info(e) << " has failed." << std::endl;
    World::getInstance().setExitFlag(5);
  }
  delete mainWindow;
}