/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2013 Frederik Heber. 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 .
 */
/*
 * ActionQueue.cpp
 *
 *  Created on: Aug 16, 2013
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Actions/ActionQueue.hpp"
#include "CodePatterns/Assert.hpp"
#include "CodePatterns/IteratorAdaptors.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Singleton_impl.hpp"
#include 
#include 
#include 
#include "Actions/ActionHistory.hpp"
#include "Actions/ActionRegistry.hpp"
using namespace MoleCuilder;
ActionQueue::ActionQueue() :
    AR(new ActionRegistry()),
    history(new ActionHistory)
{}
ActionQueue::~ActionQueue()
{
  queue.clear();
  delete history;
  delete AR;
}
void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
{
  Action* _action = AR->getActionByName(name);
  queue.push_back(_action);
  _action->call(state);
}
void ActionQueue::queueAction(Action *_action, enum Action::QueryOptions state)
{
  queue.push_back(_action);
  _action->call(state);
}
Action* ActionQueue::getActionByName(const std::string &name)
{
  return AR->getActionByName(name);
}
bool ActionQueue::isActionKnownByName(const std::string &name) const
{
  return AR->isActionPresentByName(name);
}
void ActionQueue::registerAction(Action *_action)
{
  AR->registerInstance(_action);
}
void ActionQueue::outputAsCLI(std::ostream &output) const
{
  for (ActionQueue_t::const_iterator iter = queue.begin();
      iter != queue.end();
      ++iter) {
    // skip store-session in printed list
    if ( ((*iter)->getName() != std::string("store-session"))
        && ((*iter)->getName() != std::string("load-session"))) {
      if (iter != queue.begin())
        output << " ";
      (*iter)->outputAsCLI(output);
    }
  }
  output << std::endl;
}
void ActionQueue::outputAsPython(std::ostream &output) const
{
  const std::string prefix("pyMoleCuilder");
  output << "import " << prefix << std::endl;
  output << "# ========================== Stored Session BEGIN ==========================" << std::endl;
  for (ActionQueue_t::const_iterator iter = queue.begin();
      iter != queue.end();
      ++iter) {
    // skip store-session in printed list
    if ( ((*iter)->getName() != std::string("store-session"))
        && ((*iter)->getName() != std::string("load-session")))
      (*iter)->outputAsPython(output, prefix);
  }
  output << "# =========================== Stored Session END ===========================" << std::endl;
}
const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const
{
  // this const_cast is just required as long as we have a non-const getActionByName
  const Action * const action = const_cast(this)->getActionByName(name);
  return action->Traits;
}
void ActionQueue::addElement(Action* _Action,ActionState::ptr _state)
{
  history->addElement(_Action, _state);
}
void ActionQueue::clear()
{
  history->clear();
}
const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
{
  ActionTokens_t returnlist;
  returnlist.insert(
      returnlist.end(),
      MapKeyConstIterator(AR->getBeginIter()),
      MapKeyConstIterator(AR->getEndIter()));
  return returnlist;
}
void ActionQueue::undoLast()
{
	history->undoLast();
}
void ActionQueue::redoLast()
{
	history->redoLast();
}
CONSTRUCT_SINGLETON(ActionQueue)