/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * Copyright (C)  2013 Frederik Heber. 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 .
 */
/*
 * UpdateMoleculesAction.cpp
 *
 *  Created on: May 9, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Descriptors/AtomIdDescriptor.hpp"
#include "Descriptors/MoleculeDescriptor.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "Graph/BondGraph.hpp"
#include "Graph/DepthFirstSearchAnalysis.hpp"
#include "molecule.hpp"
#include "World.hpp"
#include 
#include 
typedef std::map< moleculeId_t, std::vector > MolAtomList;
typedef std::map< atomId_t, atomId_t > AtomAtomList;
#include "Actions/GraphAction/UpdateMoleculesAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "UpdateMoleculesAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr GraphUpdateMoleculesAction::performCall() {
  // first create stuff for undo state
  LOG(0, "STATUS: Creating undo state.");
  MolAtomList moleculelist;
  {
    vector allmolecules = const_cast(World::getInstance()).
        getAllMolecules();
    for (vector::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
      std::vector atomlist;
      atomlist.resize((*moliter)->size());
      for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
        atomlist.push_back((*atomiter)->getId());
      }
      moleculelist.insert( std::pair< moleculeId_t, std::vector > ((*moliter)->getId(), atomlist));
    }
  }
  GraphUpdateMoleculesState *UndoState = new GraphUpdateMoleculesState(moleculelist, params);
  // 0a. remove all present molecules
  LOG(0, "STATUS: Removing all present molecules.");
  vector allmolecules = World::getInstance().getAllMolecules();
  for (vector::iterator MolRunner = allmolecules.begin();
      MolRunner != allmolecules.end();
      ++MolRunner)
    World::getInstance().destroyMolecule(*MolRunner);
  // 2. scan for connected subgraphs
  DepthFirstSearchAnalysis DFS;
  DFS();
  DFS.UpdateMoleculeStructure();
  const size_t numMolecules =
      const_cast(World::getInstance()).numMolecules();
  if (numMolecules == 0) {
    //World::getInstance().destroyMolecule(mol);
    STATUS("There are no molecules.");
    return Action::failure;
  }
  LOG(1, "I scanned " << numMolecules << " molecules.");
  return ActionState::ptr(UndoState);
}
ActionState::ptr GraphUpdateMoleculesAction::performUndo(ActionState::ptr _state) {
  GraphUpdateMoleculesState *state = assert_cast(_state.get());
  {
    // remove all present molecules
    vector allmolecules = World::getInstance().getAllMolecules();
    for (vector::iterator MolRunner = allmolecules.begin();
        MolRunner != allmolecules.end();
        ++MolRunner)
      World::getInstance().destroyMolecule(*MolRunner);
  }
  {
    // construct the old state
    molecule *mol = NULL;
    for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
      mol = World::getInstance().createMolecule();
      if (mol->getId() != (*iter).first)
        World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
      for (std::vector::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
        atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
        mol->AddAtom(Walker);
      }
    }
  }
  return ActionState::ptr(_state);
}
ActionState::ptr GraphUpdateMoleculesAction::performRedo(ActionState::ptr _state){
  return performCall();
}
bool GraphUpdateMoleculesAction::canUndo() {
  return true;
}
bool GraphUpdateMoleculesAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */