/*
 * 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 .
 */
/*
 * NotAtomByElementAction.cpp
 *
 *  Created on: May 12, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "Descriptors/AtomSelectionDescriptor.hpp"
#include "Descriptors/AtomTypeDescriptor.hpp"
#include "Atom/atom.hpp"
#include "Element/element.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "World.hpp"
#include 
#include 
#include 
#include "NotAtomByElementAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "NotAtomByElementAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr SelectionNotAtomByElementAction::performCall() {
  LOG(1, "Unselecting atoms of type " << *params.elemental.get());
  // build undo state: get all previously unselected atoms of the given type
  const World &world = World::getConstInstance();
  const std::vector atoms_of_element = world.
      getAllAtoms(AtomByType(params.elemental.get()));
  std::vector unselectedAtomIds;
  unselectedAtomIds.reserve(atoms_of_element.size());
  BOOST_FOREACH(const atom * _atom, atoms_of_element) {
    if (!world.isSelected(_atom))
      unselectedAtomIds.push_back(_atom->getId());
  }
  World::getInstance().unselectAllAtoms(AtomByType(params.elemental.get()));
  LOG(0, world.countSelectedAtoms() << " atoms remain selected.");
  return ActionState::ptr(new SelectionNotAtomByElementState(unselectedAtomIds,params));
}
ActionState::ptr SelectionNotAtomByElementAction::performUndo(ActionState::ptr _state) {
  SelectionNotAtomByElementState *state = assert_cast(_state.get());
  World::getInstance().selectAllAtoms(AtomByType(state->params.elemental.get()));
  BOOST_FOREACH(const atomId_t _atomid, state->unselectedAtomIds)
    World::getInstance().unselectAtom(_atomid);
  return ActionState::ptr(_state);
}
ActionState::ptr SelectionNotAtomByElementAction::performRedo(ActionState::ptr _state){
  SelectionNotAtomByElementState *state = assert_cast(_state.get());
  World::getInstance().unselectAllAtoms(AtomByType(state->params.elemental.get()));
  return ActionState::ptr(_state);
}
bool SelectionNotAtomByElementAction::canUndo() {
  return true;
}
bool SelectionNotAtomByElementAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */