/*
 * 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 .
 */
/*
 * CreateShapeAction.cpp
 *
 *  Created on: Sep 13, 2012
 *      Author: ankele
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Shapes/ShapeFactory.hpp"
#include "Shapes/ShapeRegistry.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include 
#include 
#include "CreateShapeAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "CreateShapeAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr ShapeCreateShapeAction::performCall() {
  LOG(1, "Adding shape into ShapeRegistry.");
  Shape s = ShapeFactory::getInstance().produce(ShapeFactory::getInstance().getShapeByName(params.shape_type.get()),
      params.translation.get(), params.stretch.get(),
      params.angle_x.get() * M_PI/180., params.angle_y.get() * M_PI/180., params.angle_z.get() * M_PI/180.);
  if (params.shape_name.get() != "")
    s.setName(params.shape_name.get());
  else {
    const std::string baseName = 
        ShapeFactory::getInstance().getShapeName(s.getType());
    s.setName(ShapeRegistry::getInstance().getDefaultNameForShape(baseName));
  }
  std::string old_name = s.getName();
  std::cout << s.getName() << "\n";
  ShapeRegistry::getInstance().addShape(s);
  return ActionState::ptr(new ShapeCreateShapeState(old_name, params));
}
ActionState::ptr ShapeCreateShapeAction::performUndo(ActionState::ptr _state) {
  ShapeCreateShapeState *state = assert_cast(_state.get());
  LOG(1, "INFO: Removing '" << state->old_name << "' from ShapeRegistry.");
  std::cout << state->old_name << "\n";
  ShapeRegistry::getInstance().removeShape(state->old_name);
  return ActionState::ptr(_state);
}
ActionState::ptr ShapeCreateShapeAction::performRedo(ActionState::ptr _state){
  ShapeCreateShapeState *state = assert_cast(_state.get());
  Shape s = ShapeFactory::getInstance().produce(ShapeFactory::getInstance().getShapeByName(state->params.shape_type.get()),
      state->params.translation.get(), state->params.stretch.get(),
      state->params.angle_x.get() * M_PI/180., state->params.angle_y.get() * M_PI/180., state->params.angle_z.get() * M_PI/180.);
  if (state->params.shape_name.get() != "")
    s.setName(state->params.shape_name.get());
  else {
    const std::string baseName = 
        ShapeFactory::getInstance().getShapeName(s.getType());
    s.setName(ShapeRegistry::getInstance().getDefaultNameForShape(baseName));
  }
  std::string old_name = s.getName();
  std::cout << s.getName() << "\n";
  ShapeRegistry::getInstance().addShape(s);
  return ActionState::ptr(_state);
}
bool ShapeCreateShapeAction::canUndo() {
  return true;
}
bool ShapeCreateShapeAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */