/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2021 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 .
 */
/*
 * EvaluateStabilityAction.cpp
 *
 *  Created on: Apr 18, 2021
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include 
#include "Fragmentation/Evaluation/StabilityEvaluator.hpp"
#include "World.hpp"
#include "Actions/FragmentationAction/EvaluateStabilityAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "EvaluateStabilityAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr FragmentationEvaluateStabilityAction::performCall() {
  if (World::getConstInstance().countSelectedMolecules() != 1) {
    STATUS("Exactly one molecule needs to be selected.");
    return Action::failure;
  }
  const molecule *mol = *(World::getConstInstance().getSelectedMolecules().begin());
  // evaluate
  const StabilityEvaluator evaluator(mol);
  const StabilityEvaluator::stabilities_t stabilities = evaluator();
  // write to file
  const std::string outputfilename = params.outputFilename.get().string();
  LOG(1, "INFO: Stability criterions are "+toString(stabilities));
  {
    std::ofstream outputstream(outputfilename.c_str());
    const char separator = '\t';
    if (outputstream.good()) { // check if opened
      // write header
      outputstream << "educt1\teduct2\tenergy_educts\tproduct1\tproduct2\tenergy_products" << std::endl;
      for (StabilityEvaluator::stabilities_t::const_iterator iter = stabilities.begin();
          iter != stabilities.end(); ++iter) {
        outputstream << iter->formula_educt1 << separator;
        outputstream << iter->formula_educt2 << separator;
        outputstream << iter->energy_educts << separator;
        outputstream << iter->formula_product1 << separator;
        outputstream << iter->formula_product2 << separator;
        outputstream << iter->energy_products << std::endl;
      }
      if (outputstream.fail()) { // check if correctly written
        STATUS("Failed to write to file "+outputfilename+".");
        return Action::failure;
      } else
        outputstream.close();
    } else {
      STATUS("Failed to open file "+outputfilename+" for writing.");
      return Action::failure;
    }
  }
  return Action::success;
}
ActionState::ptr FragmentationEvaluateStabilityAction::performUndo(ActionState::ptr _state) {
  return Action::success;
}
ActionState::ptr FragmentationEvaluateStabilityAction::performRedo(ActionState::ptr _state){
  return Action::success;
}
bool FragmentationEvaluateStabilityAction::canUndo() {
  return true;
}
bool FragmentationEvaluateStabilityAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */