/*
 * 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 .
 */
/*
 * FragmentationAction.cpp
 *
 *  Created on: May 9, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Atom/atom.hpp"
#include "CodePatterns/Log.hpp"
#include "Fragmentation/Fragmentation.hpp"
#include "Fragmentation/HydrogenSaturation_enum.hpp"
#include "Graph/DepthFirstSearchAnalysis.hpp"
#include "molecule.hpp"
#include "World.hpp"
#include 
#include 
#include "Actions/FragmentationAction/FragmentationAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "FragmentationAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
Action::state_ptr FragmentationFragmentationAction::performCall() {
  clock_t start,end;
  molecule *mol = NULL;
  int ExitFlag = 0;
  LOG(0, "STATUS: Fragmenting molecular system with current connection matrix maximum bond distance "
      << params.distance.get() << " up to "
      << params.order.get() << " order. Fragment files begin with "
      << params.prefix.get() << " and are stored as: "
      << params.types.get() << "." << std::endl);
  DepthFirstSearchAnalysis DFS;
  for (World::MoleculeSelectionConstIterator iter = World::getInstance().beginMoleculeSelection(); iter != World::getInstance().endMoleculeSelection(); ++iter) {
    mol = iter->second;
    ASSERT(mol != NULL, "No molecule has been picked for fragmentation.");
    LOG(2, "INFO: Fragmenting molecule with bond distance " << params.distance.get() << " angstroem, order of " << params.order.get() << ".");
    start = clock();
    if (mol->hasBondStructure()) {
      Fragmentation Fragmenter(mol, params.DoSaturation.get() ? DoSaturate : DontSaturate);
      Fragmenter.setOutputTypes(params.types.get());
      ExitFlag = Fragmenter.FragmentMolecule(params.order.get(), params.prefix.get(), DFS);
    }
    World::getInstance().setExitFlag(ExitFlag);
    end = clock();
    LOG(0, "STATUS: Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s.");
  }
  return Action::success;
}
Action::state_ptr FragmentationFragmentationAction::performUndo(Action::state_ptr _state) {
  return Action::success;
}
Action::state_ptr FragmentationFragmentationAction::performRedo(Action::state_ptr _state){
  return Action::success;
}
bool FragmentationFragmentationAction::canUndo() {
  return true;
}
bool FragmentationFragmentationAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */