/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  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 .
 */
/*
 * StretchBondAction.cpp
 *
 *  Created on: Sep 26, 2012
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "Actions/MoleculeAction/StretchBondAction.hpp"
#include 
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "LinearAlgebra/Plane.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "Bond/StretchBond.hpp"
#include "Graph/BoostGraphHelpers.hpp"
#include "World.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "StretchBondAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr MoleculeStretchBondAction::performCall()
{
  const std::vector< atom *> atoms = World::getInstance().getSelectedAtoms();
  StretchBondUtil stretcher(atoms);
  const double bonddistance = params.bonddistance.get();
  bool status = stretcher(bonddistance);
  if (status) {
    MoleculeStretchBondState *UndoState =
        new MoleculeStretchBondState(
            stretcher.getShift(),
            stretcher.getBondSides(),
            &stretcher.getMolecule(),
            params);
    return ActionState::ptr(UndoState);
  } else {
    STATUS("Failed, exactly two atoms must be selected.");
    return Action::failure;
  }
}
ActionState::ptr MoleculeStretchBondAction::performUndo(ActionState::ptr _state) {
  MoleculeStretchBondState *state = assert_cast(_state.get());
  // use given plane to undo
  Box &domain = World::getInstance().getDomain();
  for (size_t i=0;i<2;++i) {
    for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
        iter != state->bondside_sets[i].end(); ++iter) {
      atom &walker = *World::getInstance().getAtom(AtomById(*iter));
      const Vector &position = walker.getPosition();
      walker.setPosition( domain.enforceBoundaryConditions(position-state->Shift[i]) );
    }
  }
  return ActionState::ptr(_state);
}
ActionState::ptr MoleculeStretchBondAction::performRedo(ActionState::ptr _state){
  MoleculeStretchBondState *state = assert_cast(_state.get());
  Box &domain = World::getInstance().getDomain();
  for (size_t i=0;i<2;++i) {
    for (BoostGraphHelpers::Nodeset_t::const_iterator iter = state->bondside_sets[i].begin();
        iter != state->bondside_sets[i].end(); ++iter) {
      atom &walker = *World::getInstance().getAtom(AtomById(*iter));
      const Vector &position = walker.getPosition();
      walker.setPosition( domain.enforceBoundaryConditions(position+state->Shift[i]) );
    }
  }
  return ActionState::ptr(_state);
}
bool MoleculeStretchBondAction::canUndo() {
  return true;
}
bool MoleculeStretchBondAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */