/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2017 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 .
 */
/*
 * GeometryRemoveAction.cpp
 *
 *  Created on: Mar 31, 2017
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include 
#include "Geometry/GeometryObject.hpp"
#include "Geometry/GeometryRegistry.hpp"
#include "Actions/GeometryAction/GeometryRemoveAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "../GeometryAction/GeometryRemoveAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr GeometryRemoveAction::performCall() {
  // check preconditions
  if (!GeometryRegistry::getInstance().isPresentByName(params.name.get())) {
    STATUS("Name "+params.name.get()+" is not present in registry.");
    return Action::failure;
  }
  if (GeometryRegistry::getInstance().isProtectedName(params.name.get())) {
    STATUS("Name "+params.name.get()+" is protected and cannot be removed.");
    return Action::failure;
  }
  // get and create UndoState
  GeometryObject *gobject = GeometryRegistry::getInstance().getByName(params.name.get());
  const Vector vec = gobject->getVector();
  LOG(4, "DEBUG: Removing geometry object " << vec);
  GeometryRegistry::getInstance().removeGeometry(params.name.get());
  gobject = NULL;
  GeometryRemoveState *UndoState = new GeometryRemoveState(vec, params);
  return ActionState::ptr(UndoState);
}
ActionState::ptr GeometryRemoveAction::performUndo(ActionState::ptr _state) {
  GeometryRemoveState *state = assert_cast(_state.get());
  const GeometryObject vec(state->vec, params.name.get());
  LOG(4, "DEBUG: Re-adding geometry object " << vec);
  GeometryRegistry::getInstance().addGeometry(vec);
  return ActionState::ptr(_state);
}
ActionState::ptr GeometryRemoveAction::performRedo(ActionState::ptr _state){
//  GeometryRemoveState *state = assert_cast(_state.get());
  GeometryObject *vec = GeometryRegistry::getInstance().getByName(params.name.get());
  LOG(4, "DEBUG: Removing again geometry object " << *vec);
  GeometryRegistry::getInstance().removeGeometry(params.name.get());
  vec = NULL;
  return ActionState::ptr(_state);
}
bool GeometryRemoveAction::canUndo() {
  return true;
}
bool GeometryRemoveAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */