/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro-Vazquez                         *
 *   qoajnv@usc.es                                                         *
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "macromodelparser.h"
#include "tokenizer.h"
#include "molecule.h"

MacroModelParser::MacroModelParser(const char* file)
 : QuantumParser(file)
{
}

MacroModelParser::MacroModelParser(std::istream* stream) : QuantumParser(stream)
{}

MacroModelParser::~MacroModelParser()
{
}

bool MacroModelParser::ParseFile(std::streampos pos)
{

   std::cerr << "parsing macromodel" << std::endl;
    
    std::streampos framepos=0;
    std::string line;
    //Get the position of the frame here are defined the connectivities
    while(std::getline(*m_file,line))
    {
       if( line.find("f_m_ct",0) != std::string::npos ) break;
       
    }
    framepos=m_file->tellg();
    //if (!framepos) return false;
    std::cerr << "hola1" << std::endl;
    std::vector<std::streampos> molpos;
    //Now get each frame of the dynamics
    while(std::getline(*m_file,line))
    {
      if ( line.find("p_m_ct",0)!=std::string::npos ) molpos.push_back(m_file->tellg());
    }
    
    if(molpos.empty()) return false;
    
    std::cerr << "hola2" << std::endl;
    CMolecule framemolecule;
    
    
    
    //Lets read the frame to get connectivities and so on
     m_file->clear();
     m_file->seekg(framepos,std::ios::beg);
     //skip two sets of :::
     int counter=0;
     while(std::getline(*m_file,line))
     {
       std::cerr << line << std::endl;
       if (line.find(":::",0) != std::string::npos) counter++;
       if(counter == 2) break;
     }
     
     if( counter < 2) return false; //oops some problem in the file
     std::cerr << "----------------" << std::endl;
     while(std::getline(*m_file,line))
     {
      StringTokenizer token(line); 
      size_t size=token.size();
      if( size  > 3 )
      {
       std::cerr << line << std::endl;
        CAtom atom;
//        atom.label=token[0];
	
        StringTokenizer::iterator it;
	for (it=token.begin();it!=token.end();it++) std::cerr << (*it) << std::endl;
        atom.name=CMolecule::GetAtomName(atoi(token[size-4].c_str()));
	std::cerr << "one atom" << std::endl;
        atom.c.x=std::atof(token[2].c_str());
        atom.c.y=std::atof(token[3].c_str());
        atom.c.z=std::atof(token[4].c_str());
        framemolecule.m_atom.push_back(atom);
	std::cerr << "one atom" << std::endl;



      }
      else
        break;
        
     }
     std::cerr << "hola3" << std::endl;
     //Ok lets find now the next :::
     while(std::getline(*m_file,line))
     {
        if(line.find(":::",0) != std::string::npos) break;
     }
     while(std::getline(*m_file,line))
     {
       std::cerr << line << std::endl;
       if( line.find(":::",0) != std::string::npos) break;
       StringTokenizer token(line," ");
       bondtype bt=eSINGLE;
       switch(std::atoi(token[3].c_str()))
       {
        case 1:
	bt=eSINGLE;
	break;
	case 2:
	bt=eDOUBLE;
	break;
	case 3:
	bt=eTRIPLE;
	break;
	default:
	break;
       }
       
       framemolecule.m_atom[std::atoi(token[1].c_str())-1].conexion.push_back(bond(std::atoi(token[2].c_str())-1,bt));
       
     }
     std::cerr << "hola4" << std::endl;
     m_molecule->resize(molpos.size(),framemolecule);
     
     std::vector<std::streampos>::iterator st;
     std::vector<CMolecule>::iterator mt;
     m_file->clear();
     m_file->seekg(0,std::ios::beg);
     std::cerr << "hola5" << std::endl;
     for(st=molpos.begin(),mt=m_molecule->begin();st!=molpos.end();++st,++mt)
     {
       m_file->seekg(*st,std::ios::beg);
       while(std::getline(*m_file,line))
       {
         if(line.find(":::",0)!=std::string::npos) break;
       }
       //process here the energies
       while(std::getline(*m_file,line))
       {
         if(line.find(":::",0)!=std::string::npos) break;
       }
       size_t counter=0;
       std::cerr << "hola6" <<std::endl;
       while(std::getline(*m_file,line))
       {
         //std::cerr << line << std::endl;
	 if(line.find(":::",0)!=std::string::npos) break;
         StringTokenizer token(line," ");
	 std::cerr << token[2] << " " << token[3] << " " << token[4] << std::endl;
         mt->m_atom[counter].c.x=std::atof(token[1].c_str());
	 mt->m_atom[counter].c.y=std::atof(token[2].c_str());
	 mt->m_atom[counter].c.z=std::atof(token[3].c_str());
	 counter++;
	 
       }
       
       mt->CalculateMassCenter();

     }
     
     std::cerr << m_molecule->back();

 }

