/*
 * 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 .
 */
/*
 * CenterOnEdgeAction.cpp
 *
 *  Created on: May 8, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
// include headers that implement a archive in simple text format
#include 
#include 
#include "boost/serialization/vector.hpp"
#include "CodePatterns/MemDebug.hpp"
#include "Atom/atom.hpp"
#include "CodePatterns/Log.hpp"
#include "LinearAlgebra/MatrixContent.hpp"
#include "LinearAlgebra/RealSpaceMatrix.hpp"
#include "LinearAlgebra/Vector.hpp"
#include "molecule.hpp"
#include "World.hpp"
#include 
#include 
#include 
#include "Actions/WorldAction/CenterOnEdgeAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "CenterOnEdgeAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr WorldCenterOnEdgeAction::performCall() {
  Vector Min;
  Vector Max;
  // create undo state
  std::stringstream undostream;
  boost::archive::text_oarchive oa(undostream);
  const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
  oa << matrix;
  std::vector< boost::shared_ptr > OldPositions;
  std::vector AllAtoms = World::getInstance().getAllAtoms();
  for (std::vector::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
    OldPositions.push_back(
        boost::shared_ptr(new Vector(
            (*AtomRunner)->getPosition()
            ))
        );
  // get maximum and minimum
  if (AllAtoms.empty()) {
    STATUS("There are no atoms present.");
    return Action::failure;
  }
  std::vector::iterator AtomRunner = AllAtoms.begin();
  Min = (*AtomRunner)->getPosition();
  Max = (*AtomRunner)->getPosition();
  for (; AtomRunner != AllAtoms.end(); ++AtomRunner) {
    for (int i=0;iat(i) > Max[i])
        Max[i] = (*AtomRunner)->at(i);
      if ((*AtomRunner)->at(i) < Min[i])
        Min[i] = (*AtomRunner)->at(i);
    }
  }
  // set new box size
  RealSpaceMatrix domain;
  for (int i=0;i=1. ? tmp : 1.0;
    domain.at(i,i) = tmp;
  }
  World::getInstance().setDomain(domain);
  // translate all atoms, such that Min is aty (0,0,0)
  for (std::vector::iterator AtomRunner = AllAtoms.begin(); AtomRunner != AllAtoms.end(); ++AtomRunner)
    *(*AtomRunner) -= Min;
  // give final box size
  LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
  // create undo state
  WorldCenterOnEdgeState *UndoState =
      new WorldCenterOnEdgeState(
          undostream.str(),
          Min,
          Max,
          params
          );
  return ActionState::ptr(UndoState);
}
ActionState::ptr WorldCenterOnEdgeAction::performUndo(ActionState::ptr _state) {
  WorldCenterOnEdgeState *state = assert_cast(_state.get());
  // restore domain
  RealSpaceMatrix matrix;
  std::stringstream undostream(state->undostring);
  boost::archive::text_iarchive ia(undostream);
  ia >> matrix;
  World::getInstance().setDomain(matrix);
  // translate all atoms back
  std::vector AllAtoms = World::getInstance().getAllAtoms();
  for (vector::iterator AtomRunner = AllAtoms.begin();
      AtomRunner != AllAtoms.end();
      ++AtomRunner)
    *(*AtomRunner) += state->Min;
  // give final box size
  LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
  return ActionState::ptr(_state);
}
ActionState::ptr WorldCenterOnEdgeAction::performRedo(ActionState::ptr _state){
  WorldCenterOnEdgeState *state = assert_cast(_state.get());
  // set new box size
  RealSpaceMatrix rmatrix;
  for (int i=0;iMax[i]-state->Min[i];
    tmp = fabs(tmp)>=1. ? tmp : 1.0;
    rmatrix.at(i,i) = tmp;
  }
  World::getInstance().setDomain(rmatrix);
  // translate all atoms, such that Min is aty (0,0,0)
  std::vector AllAtoms = World::getInstance().getAllAtoms();
  for (vector::iterator AtomRunner = AllAtoms.begin();
      AtomRunner != AllAtoms.end();
      ++AtomRunner)
    *(*AtomRunner) -= state->Min;
  // give final box size
  LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
  return ActionState::ptr(_state);
}
bool WorldCenterOnEdgeAction::canUndo() {
  return true;
}
bool WorldCenterOnEdgeAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */