/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2013 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 .
 */
/*
 * ParseHomologiesAction.cpp
 *
 *  Created on: Jun 24, 2013
 *      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/HomologyContainer.hpp"
#include "World.hpp"
#include "Actions/PotentialAction/ParseHomologiesAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "ParseHomologiesAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
void parseHomologiesFromFile(
    const boost::filesystem::path &homology_file
    )
{
  HomologyContainer &homology_container = World::getInstance().getHomologies();
  if (boost::filesystem::exists(homology_file)) {
    std::ifstream returnstream(homology_file.string().c_str());
    if (returnstream.good()) {
      boost::archive::text_iarchive ia(returnstream);
      ia >> homology_container;
    } else {
      ELOG(2, "Failed to parse from " << homology_file.string() << ".");
    }
    returnstream.close();
    // for debugging, list homologies
    if (DoLog(3)) {
		HomologyContainer::const_key_iterator olditer = homology_container.key_end();
		for (HomologyContainer::const_key_iterator iter =
				homology_container.key_begin(); iter != homology_container.key_end();
			iter = homology_container.getNextKey(iter)) {
		  // if it's the same as the old one, skip it
		  if (olditer == iter)
		    continue;
		  else {
	      const HomologyContainer::range_t range = homology_container.getHomologousGraphs(*iter);
	      LOG(2, "DEBUG: " << *iter << " with " << std::distance(range.first, range.second) << " values.");
        const HomologyContainer::const_iterator lowest_contribution_graph =
            std::min_element(range.first, range.second, HomologyContainer::compareEnergyContribution);
        const HomologyContainer::const_iterator highest_contribution_graph =
            std::max_element(range.first, range.second, HomologyContainer::compareEnergyContribution);
        LOG(3, "DEBUG: Energy contributions range from "
            <<  lowest_contribution_graph->second.contribution << " Ht to "
            << highest_contribution_graph->second.contribution << " Ht.");
		    olditer = iter;
		  }
		}
    }
  } else {
    LOG(2, "Could not open " << homology_file.string()
        << ", creating empty container.");
  }
}
ActionState::ptr PotentialParseHomologiesAction::performCall()
{
  // append all keysets to homology file
  if (!params.homology_file.get().empty()) {
    const boost::filesystem::path &homology_file = params.homology_file.get();
    LOG(1, "INFO: Parsing HomologyGraphs from file " << homology_file.string() << ".");
    parseHomologiesFromFile(homology_file);
    return Action::success;
  } else {
    STATUS("Homology file name is empty.");
    return Action::failure;
  }
}
ActionState::ptr PotentialParseHomologiesAction::performUndo(ActionState::ptr _state) {
  STATUS("Undo of PotentialParseHomologiesAction not implemented.");
  return Action::failure;
}
ActionState::ptr PotentialParseHomologiesAction::performRedo(ActionState::ptr _state){
  STATUS("Redo of PotentialParseHomologiesAction not implemented.");
  return Action::failure;
}
bool PotentialParseHomologiesAction::canUndo() {
  return false;
}
bool PotentialParseHomologiesAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */