/*
 * 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 .
 */
/*
 * ChangeBoxAction.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 "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "LinearAlgebra/MatrixContent.hpp"
#include "LinearAlgebra/RealSpaceMatrix.hpp"
#include "World.hpp"
#include "Box.hpp"
#include 
#include 
#include "Actions/WorldAction/ChangeBoxAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "ChangeBoxAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
Action::state_ptr WorldChangeBoxAction::performCall() {
  // create undo state
  std::stringstream undostream;
  boost::archive::text_oarchive oa(undostream);
  const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
  oa << matrix;
  World::getInstance().setDomain(params.cell_size.get()); // this is needed as only this function is OBSERVEd.
  // give final box size
  LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
  // create undo state
  WorldChangeBoxState *UndoState =
      new WorldChangeBoxState(
          undostream.str(),
          params
          );
  return Action::state_ptr(UndoState);
}
Action::state_ptr WorldChangeBoxAction::performUndo(Action::state_ptr _state) {
  WorldChangeBoxState *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);
  // give final box size
  LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
  return Action::state_ptr(_state);
}
Action::state_ptr WorldChangeBoxAction::performRedo(Action::state_ptr _state){
  World::getInstance().setDomain(params.cell_size.get()); // this is needed as only this function is OBSERVEd.
  // give final box size
  LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
  return Action::state_ptr(_state);
}
bool WorldChangeBoxAction::canUndo() {
  return true;
}
bool WorldChangeBoxAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */