/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2020 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 .
 */
/*
 * AtomBondNeighborsAction.cpp
 *
 *  Created on: Oct 04, 2020
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "Descriptors/AtomIdDescriptor.hpp"
#include "World.hpp"
#include "AtomBondNeighborsAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "AtomBondNeighborsAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr SelectionAtomBondNeighborsAction::performCall()
{
  const World &world = World::getConstInstance();
  const std::vector atoms = world.getSelectedAtoms();
  std::vector undoatomids;
  undoatomids.reserve(atoms.size()*6);
  for(std::vector::const_iterator iter = atoms.begin();
      iter != atoms.end(); ++iter) {
    // look at all bond neighbors and select those
    const atom &Walker = **iter;
    const BondList& ListOfBonds = Walker.getListOfBonds();
    for (BondList::const_iterator bonditer = ListOfBonds.begin();
        bonditer != ListOfBonds.end(); ++bonditer) {
      const atom &OtherWalker = *(*bonditer)->GetOtherAtom(&Walker);
      if (!world.isSelected(&OtherWalker)) {
        undoatomids.push_back(OtherWalker.getId());
        World::getInstance().selectAtom(&OtherWalker);
      }
    }
  }
  LOG(0, World::getInstance().countSelectedAtoms()-atoms.size() << " atoms newly selected.");
  return ActionState::ptr(new SelectionAtomBondNeighborsState(undoatomids, params));
}
ActionState::ptr SelectionAtomBondNeighborsAction::performUndo(ActionState::ptr _state) {
  SelectionAtomBondNeighborsState *state = assert_cast(_state.get());
  for (atomids_t::const_iterator iter = state->undoatomids.begin();
      iter != state->undoatomids.end(); ++iter)
    World::getInstance().unselectAllAtoms(AtomById(*iter));
  return ActionState::ptr(_state);
}
ActionState::ptr SelectionAtomBondNeighborsAction::performRedo(ActionState::ptr _state){
  SelectionAtomBondNeighborsState *state = assert_cast(_state.get());
  for (atomids_t::const_iterator iter = state->undoatomids.begin();
      iter != state->undoatomids.end(); ++iter)
    World::getInstance().selectAllAtoms(AtomById(*iter));
  return ActionState::ptr(_state);
}
bool SelectionAtomBondNeighborsAction::canUndo() {
  return true;
}
bool SelectionAtomBondNeighborsAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */