/*
 * 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 .
 */
/*
 * TremoloPotentialFileParser.cpp
 *
 *  Created on: Mar 9, 2016
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "TremoloPotentialFileParser.hpp"
#include 
#include 
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Registry.hpp"
#include "Potentials/EmpiricalPotential.hpp"
#include "Potentials/Exceptions.hpp"
#include "Potentials/Particles/Particle.hpp"
#include "Potentials/Particles/ParticleFactory.hpp"
#include "Potentials/Particles/ParticleRegistry.hpp"
#include "Potentials/PotentialFactory.hpp"
#include "Potentials/PotentialRegistry.hpp"
#include "Potentials/RegistryDeserializer.hpp"
#include "Potentials/StreamFactory.hpp"
#include "Potentials/TremoloPotentialTypes.hpp"
static TremoloPotentialFileParser::allowed_types_t potential_types =
    boost::assign::list_of< TremoloPotentialTypes::tokentype_t >
    ( TremoloPotentialTypes::bonds)
    ( TremoloPotentialTypes::angles)
    ( TremoloPotentialTypes::torsions)
    ( TremoloPotentialTypes::impropers)
    ( TremoloPotentialTypes::nonbonded_2body_potentials);
static TremoloPotentialFileParser::allowed_types_t particle_types =
    boost::assign::list_of< TremoloPotentialTypes::tokentype_t >
    ( TremoloPotentialTypes::particles);
template 
bool parseItems(std::istream &_serialized,
    StreamFactory &_factory,
    Registry &_registry,
    const std::string _name,
    const TremoloPotentialFileParser::allowed_types_t &_allowed_types)
{
  std::string linestring;
  std::string token;
  while (_serialized.good()) {
    getline(_serialized, linestring);
    const std::string comment("#");
    if (linestring.find(comment) != std::string::npos) {
      LOG(4, "DEBUG: Skipping comment line:"+linestring);
      continue;
    }
    const std::string whitespace(" \t");
    const size_t strBegin = linestring.find_first_not_of(whitespace);
    const size_t curlyopenpos = linestring.find("{");
    const size_t curlyclosedpos = linestring.find("}");
    if (curlyopenpos != std::string::npos) {
      // begin of new type section
      token = linestring.substr(strBegin, curlyopenpos);
      const size_t tokenEnd = token.find_first_of(whitespace);
      token = token.substr(0, tokenEnd);
      LOG(1, "INFO: Token is " << token);
      const TremoloPotentialTypes::tokentype_t tokentype =
          TremoloPotentialTypes::getTokenTypeFromType(token);
      // get either particles {} or potentials {}
      if (_allowed_types.count(tokentype)) {
        try {
          RegistryDeserializer deserialize(
              _serialized,
              _factory,
              _registry,
              _name);
          deserialize();
        } catch (SerializerMissingValueException &e) {
          if (const std::string *key = boost::get_error_info(e))
            ELOG(0, "Missing value when parsing information for particle "+*key+".");
          else
            ELOG(0, "Missing value parsing information for particle with unknown key.");
          return false;
        } catch (SerializerIllegalKeyException &e) {
          if (const std::string *key = boost::get_error_info(e))
            ELOG(0, "Illegal key parsing information for particle "+*key+".");
          else
            ELOG(0, "Illegal key parsing information for particle with unknown key.");
          return false;
        }
      }
    } else if (curlyclosedpos != std::string::npos) {
      // section end encountered, just read next line
    } else {
      // skipping a section we do not parse
    }
  }
  return true;
}
bool TremoloPotentialFileParser::parseParticles(
    std::istream &_stream)
{
  if (!parseItems(
      _stream,
        ParticleFactory::getInstance(),
        ParticleRegistry::getInstance(),
        std::string("particle"),
        particle_types))
    return false;
  return true;
}
bool TremoloPotentialFileParser::parsePotentials(std::istream &_stream)
{
  if (!parseItems(
      _stream,
      PotentialFactory::getInstance(),
      PotentialRegistry::getInstance(),
      std::string("potential"),
      potential_types))
    return false;
  return true;
}