/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 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 .
 */
/*
 * MakroAction.cpp
 *
 *  Created on: Dec 17, 2009
 *      Author: crueger
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include 
#include "Actions/MakroAction.hpp"
#include "Actions/Action.hpp"
#include "Actions/ActionExceptions.hpp"
#include "Actions/ActionRegistry.hpp"
#include "Actions/ActionSequence.hpp"
#include "CodePatterns/Assert.hpp"
#include "UIElements/Dialog.hpp"
#include "UIElements/UIFactory.hpp"
using namespace MoleCuilder;
class MakroActionState : public ActionState{
public:
  MakroActionState(ActionSequence::stateSet _states) :
    states(_states)
  {}
  virtual ~MakroActionState(){
    // All contained states are destroyed by the shared ptrs
  }
  ActionSequence::stateSet states;
};
MakroAction::MakroAction(const ActionTrait &_trait,ActionSequence& _actions) :
    Action(_trait),
    actions(_actions)
{
}
MakroAction::~MakroAction()
{
  // emptying the sequence is someone else responsibility
//  Action* action;
//  while((action=actions.removeLastAction())){
//    delete action;
//  }
}
void MakroAction::prepare(ActionRegistry &AR)
{}
void MakroAction::unprepare(ActionRegistry &AR)
{}
void MakroAction::setLoop(unsigned int _loop)
{
  actions.setLoop(_loop);
}
bool MakroAction::removeAction(const std::string &name)
{
  return actions.removeAction(name);
}
Dialog* MakroAction::fillOwnDialog(Dialog *dialog) {
  return dialog;
}
Dialog* MakroAction::fillDialog(Dialog *dialog) {
  dialog = fillOwnDialog(dialog);
  return actions.fillAllDialogs(dialog);
}
ActionState::ptr MakroAction::performCall(){
  ActionSequence::stateSet states = actions.callAll(true);
  return ActionState::ptr(new MakroActionState(states));
}
ActionState::ptr MakroAction::performUndo(ActionState::ptr _state) {
  MakroActionState *state = dynamic_cast(_state.get());
  ASSERT(state,"Type mismatch for the state of the MakroAction");
  ActionSequence::stateSet states = actions.undoAll(state->states);
  return ActionState::ptr(new MakroActionState(states));
}
ActionState::ptr MakroAction::performRedo(ActionState::ptr _state){
  MakroActionState *state = dynamic_cast(_state.get());
  ASSERT(state,"Type mismatch for the state of the MakroAction");
  ActionSequence::stateSet states = actions.redoAll(state->states);
  return ActionState::ptr(new MakroActionState(states));
}
bool MakroAction::canUndo() {
  return actions.canUndo();
}
bool MakroAction::shouldUndo() {
  return actions.shouldUndo();
}
Action* MakroAction::clone(enum QueryOptions flag) const
{
  if (flag == Interactive)
    return new MakroAction(Traits, actions);
  else
    return new MakroAction(*this);
}
void MakroAction::prepare(enum QueryOptions flag)
{
  if (flag == Interactive) {
    // create and run our dialog and also of all members of the sequence
    Dialog* dialog = createDialog();
    if (dialog->hasQueries()) {
      if (!dialog->display())
        // dialog error or aborted -> throw exception
        throw ActionFailureException() << ActionNameString(getName());
    }
    delete(dialog);
  }
}
void MakroAction::outputAsCLI(std::ostream &ost) const
{
  actions.outputAsCLI(ost);
}
void MakroAction::outputAsPython(std::ostream &ost, const std::string &prefix) const
{
  actions.outputAsPython(ost, prefix);
}