/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro-Vazquez                         *
 *   uscqoanv@cesga.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 <qimage.h>

#include "kryomolthumbcreator.h"
#include "glvisorbase.h"
#include "quantumfile.h"
#include "gaussianparser.h"
#include "acesparser.h"
#include "archiveparser.h"
#include "gamessparser.h"
#include "macromodelparser.h"
#include "MolParser.h"
#include "nwchemparser.h"
#include "psi3parser.h"
#include "pdbparser.h"
#include "tinkerparser.h"
#include "xyzparser.h"


extern "C"
{
  KDE_EXPORT ThumbCreator *new_creator()
  {

    return new KryoMolThumbCreator();

  }
};


KryoMolThumbCreator::KryoMolThumbCreator() : m_qparser(NULL)
{
  m_qparser=NULL;
}


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



bool KryoMolThumbCreator::create(const QString &path, int width, int height, QImage
                                 &img)
{
  std::cout<< "about to create" << std::endl;
  CGlVisorBase visor;

  QString m_file=path;

  //if ( (m_file.findRev(".out") !=-1) || (m_file.findRev(".log") !=-1) )
  QuantumFile file(m_file.latin1());

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


    switch( jobtype )
    {
    case QuantumFile::None:
      return false;
    case QuantumFile::Gaussian:
      m_qparser= new GaussianParser(m_file.latin1());
      break;
    case QuantumFile::GaussianArchive:
      m_qparser= new ArchiveParser(m_file.latin1());
      break;
    case QuantumFile::Aces:
      m_qparser= new AcesParser(m_file.latin1());
      break;
    case QuantumFile::Gamess:
      m_qparser= new GamessParser(m_file.latin1());
      break;
    case QuantumFile::MacroModel:
      m_qparser = new MacroModelParser(m_file.latin1());
      break;
    case QuantumFile::Mol:
      m_qparser= new MolParser(m_file.latin1());
      break;
    case QuantumFile::Psi3:
      m_qparser= new Psi3Parser(m_file.latin1());
      break;
    case QuantumFile::PDB:
      m_qparser = new PDBParser(m_file.latin1());
      break;
    case QuantumFile::XYZ:
      m_qparser = new XYZParser(m_file.latin1());
      break;
    case QuantumFile::Tinker:
      m_qparser= new TinkerParser(m_file.latin1());
      break;
    case QuantumFile::NwChem:
      m_qparser = new NwChemParser(m_file.latin1());
      break;
    default:
      return false;
    }
  }
  catch(...)
  {
    return false;
  }
  BuildPeriodicTable();

  if(m_qparser)
  {
    std::vector<Quantum::jobheader> jobs;
    jobs=m_qparser->GetJobs();

    if( jobs.size() == 0 ) return false; //oops some error

    m_qparser->SetMolecule(visor.GetMolecule());


    try
    {
      m_qparser->ParseFile(jobs.back().pos);
    }
    catch( ...)
    {
      std::cerr << "Error Parsing File" << std::endl;
      return false;
    }

  }
  visor.Initialize();

  visor.GetImage(img,width,height);


  return true;

}

ThumbCreator::Flags KryoMolThumbCreator::flags() const
{
  return (ThumbCreator::Flags) (ThumbCreator::None);
}

