/***************************************************************************
 *   Copyright (C) 2005 by Armando Navarro-Vazquez                         *
 *   armando@linux                                                         *
 *                                                                         *
 *   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 <sstream>
#include <iomanip>
#include <kgenericfactory.h>

#include "kryomolfile.h"
#include "gaussianparser.h"
#include "quantumfile.h"
#include "molecule.h"

typedef KGenericFactory<KryoMolFile> krfilefactory;
K_EXPORT_COMPONENT_FACTORY(libkryomolfile, krfilefactory( "libkryomolfile" ));

KryoMolFile::KryoMolFile(QObject* parent, const char* name, const QStringList& args)
    : KFilePlugin(parent,name,args)
{

  m_info = addMimeTypeInfo( "chemical/x-kryomol" );

}


KryoMolFile::~KryoMolFile()
{
  delete m_qparser;
}


bool KryoMolFile::readInfo(KFileMetaInfo& info, uint what )
{

  QuantumFile file(info.path().latin1());
  try
  {
    QuantumFile::qfiletype jobtype=file.GetFileType();


    switch( jobtype )
    {

    case QuantumFile::Gaussian:
      m_qparser= new GaussianParser(info.path().latin1());
      break;
    default:
      return false;
    }
  }
  catch(...)
  {
    return false;
  }

  if ( ! m_qparser ) return false;

  Parse(info);


  return true;



}

void KryoMolFile::Parse(KFileMetaInfo& info )
{
#warning warning check here
  std::vector<Quantum::jobheader> jobs=m_qparser->GetJobs();
  std::vector<Quantum::jobheader>::iterator it;
  m_qparser->SetMolecule(&m_molecules);
  if ( m_mode == KFileMimeTypeInfo::Thumbnail )
  {
    m_qparser->ParseFile(jobs.back().pos);
    AddOptGroup(info);
  }

}

void KryoMolFile::AddOptGroup(KFileMetaInfo& info)
{
  static QString str("_");
  std::cout << "info " << m_info << std::endl;
  KFileMimeTypeInfo::GroupInfo* pgroup = addGroupInfo(m_info, str, i18n("Optimization"));
  KFileMimeTypeInfo::ItemInfo* item;
  // our new items in the group
  KFileMetaInfoGroup group = appendGroup(info, str);

  item= addItemInfo(pgroup, "Energy", i18n("Energy"), QVariant::String);
  std::stringstream f;
  f << "E = " << std::setiosflags(std::ios::fixed) << std::setprecision(5) << m_molecules.back().GetEnergy();
  appendItem(group,"Energy",QString(f.str().c_str()));
  f.str("");
  if ( m_molecules.back().GetRMSForce() < 0 )
  {
    str+="_";
    return;
  }
  item = addItemInfo(pgroup, "Maximum Force", i18n("Max. Force"), QVariant::String);




  f << std::setiosflags(std::ios::fixed) << std::setprecision(6) << m_molecules.back().GetMaximumForce() << "[" << m_molecules.back().GetThreshold().maxforce << "]";
  if ( m_molecules.back().GetMaximumForce() < m_molecules.back().GetThreshold().maxforce ) f << " YES";
  else f << " NO";

  appendItem(group,"Maximum Force",QString(f.str().c_str()));
  f.str("");

  item = addItemInfo(pgroup, "RMS Force", i18n("RMS Force"), QVariant::String);
  f << std::setiosflags(std::ios::fixed) << std::setprecision(6) << m_molecules.back().GetRMSForce() << "[" <<m_molecules.back().GetThreshold().rmsforce << "]";
  if ( m_molecules.back().GetRMSForce() < m_molecules.back().GetThreshold().rmsforce ) f << " YES";
  else f << " NO";
  appendItem(group,"RMS Force",QString(f.str().c_str()));
  f.str("");

  item = addItemInfo(pgroup, "Maximum Displacement", i18n("Max. Displacement"), QVariant::String);
  f << std::setiosflags(std::ios::fixed) << std::setprecision(6) << m_molecules.back().GetMaximumDisplacement() << "[" << m_molecules.back().GetThreshold().maxdisplacement << "]";
  if ( m_molecules.back().GetMaximumDisplacement() < m_molecules.back().GetThreshold().maxdisplacement ) f << " YES";
  else f << " NO";
  appendItem(group,"Maximum Displacement",QString(f.str().c_str()));
  f.str("");

  item = addItemInfo(pgroup, "RMS Displacement", i18n("RMS Displacement"), QVariant::String);
  f << std::setiosflags(std::ios::fixed) << std::setprecision(6) << m_molecules.back().GetRMSDisplacement() << "[" << m_molecules.back().GetThreshold().rmsdisplacement << "]";
  if ( m_molecules.back().GetRMSDisplacement() < m_molecules.back().GetThreshold().rmsdisplacement ) f << " YES";
  else f << " NO";
  appendItem(group,"RMS Displacement",QString(f.str().c_str()));
  f.str("");


  str+="_";
}


