/*
 * 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 .
 */
/*
 * AddAction.cpp
 *
 *  Created on: May 9, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "Descriptors/AtomIdDescriptor.hpp"
#include "Atom/atom.hpp"
#include "Element/element.hpp"
#include "CodePatterns/Log.hpp"
#include "molecule.hpp"
#include "LinearAlgebra/Vector.hpp"
#include "CodePatterns/Verbose.hpp"
#include "World.hpp"
#include 
#include 
#include "Actions/AtomAction/AddAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "AddAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
{
  atom * first = World::getInstance().createAtom();
  first->setType(_params.elemental.get());
  first->setPosition(_params.position.get());
  return first;
}
atom * regetNewAtom(const AtomAddAction::AtomAddParameters &_params, const atomId_t _id)
{
  atom * first = World::getInstance().recreateAtom(_id);
  first->setType(_params.elemental.get());
  first->setPosition(_params.position.get());
  return first;
}
std::vector createAtoms(
    const AtomAddAction::AtomAddParameters &_params,
    std::vector &_molecules)
{
  std::vector ids;
  if (!_molecules.empty()) {
    if (_molecules.size() == 1) {
        atom *first = getNewAtom(_params);
        molecule *mol = *_molecules.begin();
        LOG(1, "Adding new atom with element " << first->getType()->getName()
            << " at " << (first->getPosition()) << " to selected molecule "
            << mol->getName()+".");
        mol->AddAtom(first);
      ids.push_back(first->getId());
    }
  } else {
    atom *first = getNewAtom(_params);
    molecule *mol = World::getInstance().createMolecule();
    mol->setName("none");
    mol->AddAtom(first);
    LOG(1, "Adding new atom with element " << first->getType()->getName()
        << " at " << (first->getPosition()) << " to new molecule.");
    ids.push_back(first->getId());
  }
  return ids;
}
std::vector recreateAtoms(
    const AtomAddAction::AtomAddParameters &_params,
    const std::vector &_ids,
    std::vector &_molecules)
{
  molecule *mol = NULL;
  if (!_molecules.empty()) {
    ASSERT (_molecules.size() == 1,
        "recreateAtoms() - more than one molecules given.");
    mol = *_molecules.begin();
  } else {
    mol = World::getInstance().createMolecule();
    mol->setName("none");
  }
  std::vector newids;
  for (std::vector::const_iterator iter = _ids.begin();
      iter != _ids.end(); ++iter) {
    atom *first = regetNewAtom(_params, *iter);
    if (first != NULL) {
      newids.push_back(first->getId());
      mol->AddAtom(first);
      LOG(1, "Adding new atom with element " << first->getType()->getName()
          << " at " << (first->getPosition()) << " to new molecule.");
    } else {
      ELOG(1, "Could not recreate atom with id " << *iter);
      break;
    }
  }
  return newids;
}
ActionState::ptr AtomAddAction::performCall() {
  // execute action
  std::vector molecules = World::getInstance().getSelectedMolecules();
  std::vector ids = createAtoms(params, molecules);
  if (molecules.size() > 1)
     return Action::failure;
  else
    return ActionState::ptr(new AtomAddState(ids, params));
}
ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state) {
  AtomAddState *state = assert_cast(_state.get());
  for (std::vector::const_iterator iter = state->ids.begin();
      iter != state->ids.end(); ++iter) {
    LOG(1, "Removing atom with id " << *iter << ".");
    World::getInstance().destroyAtom(*iter);
  }
  return ActionState::ptr(_state);
}
ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state){
  AtomAddState *state = assert_cast(_state.get());
  std::vector molecules = World::getInstance().getSelectedMolecules();
  std::vector newids = recreateAtoms(state->params, state->ids, molecules);
  if (newids.size() != state->ids.size()) {
    STATUS("Could not recreate all atoms after undo.");
    for (std::vector::const_iterator iter = newids.begin(); iter != newids.end();++iter)
      World::getInstance().destroyAtom(*iter);
    return Action::failure;
  }
  return ActionState::ptr(_state);
}
bool AtomAddAction::canUndo() {
  return true;
}
bool AtomAddAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */