/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2016 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 .
 */
/*
 * ParseAtomFragmentsAction.cpp
 *
 *  Created on: Mar 07, 2016
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
// include headers that implement a archive in simple text format
// and before MemDebug due to placement new
#include 
#include 
#include "CodePatterns/MemDebug.hpp"
#include 
#include "CodePatterns/Log.hpp"
#include "CodePatterns/toString.hpp"
#include "Fragmentation/Homology/AtomFragmentsMap.hpp"
#include "Fragmentation/KeySet.hpp"
#include "World.hpp"
#include "Actions/PotentialAction/ParseAtomFragmentsAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "ParseAtomFragmentsAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
void parseAtomFragmentsFromFile(
    const boost::filesystem::path &atomfragments_file
    )
{
  AtomFragmentsMap &atomfragments_container = AtomFragmentsMap::getInstance();
  if (boost::filesystem::exists(atomfragments_file)) {
    std::ifstream returnstream(atomfragments_file.string().c_str());
    if (returnstream.good()) {
      boost::archive::text_iarchive ia(returnstream);
      ia >> atomfragments_container;
    } else {
      ELOG(2, "Failed to parse from " << atomfragments_file.string() << ".");
    }
    returnstream.close();
    // for debugging, list homologies
    if (DoLog(3)) {
      typedef AtomFragmentsMap::AtomFragmentsMap_t::const_iterator citer;
      AtomFragmentsMap::AtomFragmentsMap_t atommap = atomfragments_container.getMap();
      citer olditer = atommap.end();
      for (citer iter = atommap.begin();
          iter != atommap.end();
          ++iter) {
        // if it's the same as the old one, skip it
        if (olditer == iter)
          continue;
        else
          olditer = iter;
        LOG(3, "DEBUG:  atom #" << iter->first << " with " << toString(iter->second));
      }
    }
  } else {
    LOG(2, "Could not open " << atomfragments_file.string()
        << ", creating empty container.");
  }
}
ActionState::ptr PotentialParseAtomFragmentsAction::performCall()
{
  // append all keysets to atomfragments file
  if (!params.atomfragments_file.get().empty()) {
    const boost::filesystem::path &atomfragments_file = params.atomfragments_file.get();
    LOG(1, "INFO: Parsing AtomFragmentsGraphs from file " << atomfragments_file.string() << ".");
    parseAtomFragmentsFromFile(atomfragments_file);
    return Action::success;
  } else {
    STATUS("AtomFragments file name is empty.");
    return Action::failure;
  }
}
ActionState::ptr PotentialParseAtomFragmentsAction::performUndo(ActionState::ptr _state) {
  STATUS("Undo of PotentialParseAtomFragmentsAction not implemented.");
  return Action::failure;
}
ActionState::ptr PotentialParseAtomFragmentsAction::performRedo(ActionState::ptr _state){
  STATUS("Redo of PotentialParseAtomFragmentsAction not implemented.");
  return Action::failure;
}
bool PotentialParseAtomFragmentsAction::canUndo() {
  return false;
}
bool PotentialParseAtomFragmentsAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */