/*
 * 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 .
 */
/*
 * SaveAtomFragmentsAction.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 "Fragmentation/Homology/AtomFragmentsMap.hpp"
#include "Fragmentation/KeySet.hpp"
#include "Actions/PotentialAction/SaveAtomFragmentsAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "SaveAtomFragmentsAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
bool writeAtomFragmentsToFile(
    const boost::filesystem::path &atomfragments_file
    )
{
  /// store homology container again
  AtomFragmentsMap &atomfragments_container = AtomFragmentsMap::getInstance();
  std::ofstream outputstream(atomfragments_file.string().c_str());
  if (outputstream.good()) { // check if opened
    boost::archive::text_oarchive oa(outputstream);
    oa << atomfragments_container;
    if (outputstream.fail()) { // check if correctly written
      LOG(1, "Failed to write to file " << atomfragments_file.string() << ".");
      return false;
    } else
      outputstream.close();
  } else {
    LOG(1, "Failed to open file " << atomfragments_file.string()
        << " for writing.");
    return false;
  }
  return true;
}
ActionState::ptr PotentialSaveAtomFragmentsAction::performCall()
{
  if (!params.atomfragments_file.get().empty()) {
    const boost::filesystem::path &atomfragments_file = params.atomfragments_file.get();
    LOG(1, "INFO: Appending AtomFragment associations to file " << atomfragments_file.string() << ".");
    if (!writeAtomFragmentsToFile(atomfragments_file)) {
      STATUS("Failed to write atomfragments file.");
      return Action::failure;
    }
    return Action::success;
  } else {
    STATUS("Homology file name empty.");
    return Action::failure;
  }
}
ActionState::ptr PotentialSaveAtomFragmentsAction::performUndo(ActionState::ptr _state) {
  return Action::success;
}
ActionState::ptr PotentialSaveAtomFragmentsAction::performRedo(ActionState::ptr _state){
  return Action::success;
}
bool PotentialSaveAtomFragmentsAction::canUndo() {
  return false;
}
bool PotentialSaveAtomFragmentsAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */