/***************************************************************************
 *   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 "aceswriter.h"
#include <vector>
#include <iomanip>
#include "tokenizer.h"


AcesWriter::~AcesWriter()
{
}

std::ostream& operator << (std::ostream& s, AcesWriter& aw)
{

   CMolecule& molecule=aw.m_molecule;
   std::vector<CAtom>::iterator at,at1;

   
   std::stringstream svalues;
   int counter;
   
   for(at=molecule.m_atom.begin(), counter=0; at!=molecule.m_atom.end();at++,counter++)
   {
        
	s << StringTokenizer::toupper(at->name) << " ";
   
	if( counter )
	{
         AcesWriter::zmatlabel r;
         r.label << "r" <<counter;
         r.value=coordinate::distance(at->c,(at-1)->c);
         s <<  counter << " " << r.label.str()  << " ";
	 
	 svalues <<  r.label.str() << "=" <<  std::setiosflags(std::ios::fixed) <<std::setprecision(4) << 
	   r.value   << std::resetiosflags(std::ios::fixed) << std::endl;
	 }
	 
	 if( counter > 1)
	 {
	   AcesWriter::zmatlabel a;
	    a.label << "a" << counter-1;
	    a.value=molecule.GetAngle(counter,counter-1,counter-2,true);
	    s << counter-1 << " " << a.label.str() << " ";
	    svalues << a.label.str() << "=" <<  std::setiosflags(std::ios::fixed) <<std::setprecision(3) << 
	   a.value   << std::resetiosflags(std::ios::fixed) << std::endl;
	    }
	 
	 if(counter > 2)
	 {
	   AcesWriter::zmatlabel d;
	   d.label << "d" << counter-2;
	   d.value=molecule.GetDihedral(counter,counter-1,counter-2,counter-3,true);
	   s << counter -2 << " " << d.label.str();
	   svalues << d.label.str() << "=" << std::setiosflags(std::ios::fixed) <<std::setprecision(2) << 
	   d.value   << std::resetiosflags(std::ios::fixed) << std::endl;
	 }
         
	 s << std::endl;

     
   }
   
   s << std::endl << svalues.str() << std::endl;
   
   return s;
   
   
}

