/*
 * 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 .
 */
/*
 * SaveFragmentResultsAction.cpp
 *
 *  Created on: Sep 04, 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/Summation/Containers/FragmentationResultContainer.hpp"
#include "Actions/FragmentationAction/SaveFragmentResultsAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "SaveFragmentResultsAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
bool writeFragmentResultsToFile(
    const boost::filesystem::path &fragmentresults_file
    )
{
  /// if file is given, parse from file into ResultsContainer
  FragmentationResultContainer& container = FragmentationResultContainer::getInstance();
  if (!boost::filesystem::exists(fragmentresults_file)) {
    LOG(1, "INFO: Saving fragment results to " << fragmentresults_file.string() << ".");
    std::ofstream returnstream(fragmentresults_file.string().c_str());
    if (returnstream.good()) {
      boost::archive::text_oarchive oa(returnstream);
      oa << container;
      if (returnstream.bad()) { // check if correctly written
        LOG(1, "Failed to write to file " << fragmentresults_file.string() << ".");
        return false;
      } else
        returnstream.close();
    }
  } else {
    ELOG(1, "Given file" << fragmentresults_file.string() << " already exists.");
    return false;
  }
  return true;
}
ActionState::ptr FragmentationSaveFragmentResultsAction::performCall()
{
  if (!params.fragmentresults_file.get().empty()) {
    const boost::filesystem::path &fragmentresults_file = params.fragmentresults_file.get();
    if (writeFragmentResultsToFile(fragmentresults_file)) {
      STATUS("SUCCESS: Saving fragment results to file "+toString(fragmentresults_file.string())+".");
      return Action::success;
    } else {
      STATUS("FAIL: Failed to write fragment results file.");
      return Action::failure;
    }
  } else {
    STATUS("FAIL: Fragment results file name empty.");
    return Action::failure;
  }
}
ActionState::ptr FragmentationSaveFragmentResultsAction::performUndo(ActionState::ptr _state) {
  return Action::success;
}
ActionState::ptr FragmentationSaveFragmentResultsAction::performRedo(ActionState::ptr _state){
  return Action::success;
}
bool FragmentationSaveFragmentResultsAction::canUndo() {
  return false;
}
bool FragmentationSaveFragmentResultsAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */