/*
 * 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 .
 */
/*
 * ActionHistory.cpp
 *
 *  Created on: Mar 25, 2010
 *      Author: crueger
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Actions/ActionHistory.hpp"
#include "Actions/ActionExceptions.hpp"
#include 
#include "CodePatterns/Assert.hpp"
using namespace MoleCuilder;
ActionHistory::ActionHistory()
{
  Action::createStaticStateEntities();
}
ActionHistory::~ActionHistory()
{
  Action::removeStaticStateEntities();
}
void ActionHistory::undoLast(){
  if (!hasUndo()){
    LOG(1, "Undo performed when the undo-queue was empty.");
    return;
  }
  //ASSERT(history.size(),"Undo performed when the undo-queue was empty");
  HistoryElement elem = history.back();
  history.pop_back();
  ActionState::ptr newState = elem.action->undo(elem.state);
  if (newState == Action::failure)
    throw ActionFailureException() << ActionNameString(elem.action->getName());
  yrotsih.push_back(HistoryElement(elem.action,newState));
}
void ActionHistory::redoLast(){
  if (!hasRedo()){
    LOG(1, "Redo performed when the redo-queue was empty.");
    return;
  }
  //ASSERT(yrotsih.size(),"Redo performed when the redo-queue was empty");
  HistoryElement elem = yrotsih.back();
  yrotsih.pop_back();
  ActionState::ptr oldState = elem.action->redo(elem.state);
  if (oldState == Action::failure)
    throw ActionFailureException() << ActionNameString(elem.action->getName());
  history.push_back(HistoryElement(elem.action,oldState));
}
bool ActionHistory::hasUndo(){
  return history.size()>0;
}
bool ActionHistory::hasRedo(){
  return yrotsih.size()>0;
}
void ActionHistory::addElement(Action* action,ActionState::ptr state){
  yrotsih.clear();
  history.push_back(HistoryElement(action,state));
}
void ActionHistory::clear(){
  history.clear();
  yrotsih.clear();
}
//void ActionHistory::init(){
//  ActionHistory *hist = new ActionHistory();
//  setInstance(hist);
//}
/****************** Contained actions *******************/