/*
 * 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 .
 */
/*
 * AllAtomsInsideVolumeAction.cpp
 *
 *  Created on: Aug 9, 2010
 *      Author: heber, ankele
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Descriptors/AtomShapeDescriptor.hpp"
#include "Descriptors/AtomSelectionDescriptor.hpp"
#include "Atom/atom.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "LinearAlgebra/Vector.hpp"
#include "Shapes/ShapeRegistry.hpp"
#include "World.hpp"
#include 
#include 
#include 
#include "AllAtomsInsideVolumeAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "AllAtomsInsideVolumeAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
Action::state_ptr SelectionAllAtomsInsideVolumeAction::performCall() {
  LOG(1, "Selecting all atoms inside a volume.");
  std::vector selectedShapes = ShapeRegistry::getInstance().getSelectedShapes();
  if (selectedShapes.size() != 1){
    ELOG(1, "SelectionAllAtomsInsideVolumeAction::performCall(): there has to be exactly 1 selected shape.");
    return Action::failure;
  }
  std::vector selectedAtoms = World::getInstance().getAllAtoms(AtomsBySelection() && AtomsByShape(*selectedShapes[0]));
  World::getInstance().selectAllAtoms(AtomsByShape(*selectedShapes[0]));
  LOG(0, World::getInstance().countSelectedAtoms() << " atoms selected.");
  return Action::state_ptr(new SelectionAllAtomsInsideVolumeState(selectedAtoms, params));
}
Action::state_ptr SelectionAllAtomsInsideVolumeAction::performUndo(Action::state_ptr _state) {
  SelectionAllAtomsInsideVolumeState *state = assert_cast(_state.get());
  std::vector selectedShapes = ShapeRegistry::getInstance().getSelectedShapes();
  if (selectedShapes.size() != 1)
    return Action::failure;
  World::getInstance().unselectAllAtoms(AtomsByShape(*selectedShapes[0]));
  BOOST_FOREACH(atom *_atom, state->selectedAtoms)
    World::getInstance().selectAtom(_atom);
  return Action::state_ptr(_state);
}
Action::state_ptr SelectionAllAtomsInsideVolumeAction::performRedo(Action::state_ptr _state){
  //SelectionAllAtomsInsideVolumeState *state = assert_cast(_state.get());
  std::vector selectedShapes = ShapeRegistry::getInstance().getSelectedShapes();
  if (selectedShapes.size() != 1)
    return Action::failure;
  World::getInstance().selectAllAtoms(AtomsByShape(*selectedShapes[0]));
  return Action::state_ptr(_state);
}
bool SelectionAllAtomsInsideVolumeAction::canUndo() {
  return true;
}
bool SelectionAllAtomsInsideVolumeAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */