/***************************************************************************
 *   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 <fstream>
#include <kurl.h>
#include <knuminput.h>
#include <kfiledialog.h>
#include <qtoolbutton.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include "qnmrwidget.h"
#include "nmrhandler.h"
#include "quantumfile.h"
#include "gaussianparser.h"

QNMRWidget::QNMRWidget ( QWidget* parent ) : QWidget( parent )
{
  setupUi(this);
  m_handler = new NMRHandler();
  _applyReferenceButton->setEnabled ( false );
  connect ( _loadReferenceButton,SIGNAL ( clicked() ),this,SLOT ( OnLoadReferenceFile() ) );
  connect ( _applyReferenceButton,SIGNAL ( clicked() ),this, SLOT ( OnApplyReferences() ) );
  connect ( _nucleiBox,SIGNAL ( activated ( const QString& ) ),this,SLOT ( OnSetNucleus(const QString& ) ) );
}


QNMRWidget::~QNMRWidget()
{
  delete m_handler;
}

void QNMRWidget::OnLoadReferenceFile()
{
  KUrl url = KFileDialog::getOpenUrl ( QString(), QString(), this );

  if ( url.isValid() )
  {
#warning change ifstream to qt equivalent
    std::ifstream f ( url.path().toLatin1() );
    if ( f )
    {
      QuantumFile file ( &f );
      QuantumFile::qfiletype jobtype=file.GetFileType();
      GaussianParser* parser=NULL;
      if ( jobtype == QuantumFile::Gaussian )
        parser= new GaussianParser ( url.path().latin1() );

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

        m_molecules.resize ( 1 );
        parser->SetMolecule ( &m_molecules );
        for ( it=jobs.begin();it!=jobs.end();++it )
        {
          if ( it->type == Quantum::nmr )
          {
            std::cout << "Parsing nmr file" << std::endl;
            parser->ParseFile ( it->pos );
            parser->ParseNMR ( it->pos );
            StoreReferences();

          }
        }
      }


    }

  }
}

void QNMRWidget::StoreReferences()
{
  std::vector<CAtom>::const_iterator at;
  for ( at=m_molecules.back().m_atom.begin();at!=m_molecules.back().m_atom.end();++at )
  {
    if ( m_handler->References().find ( at->name ) == m_handler->References().end() )
    {
      m_handler->References().insert ( std::pair<std::string,float> ( at->name,at->CShift() ) );
      _nucleiBox->insertItem ( QString ( at->name.c_str() ) );
    }



  }
  //fill up combo

  _applyReferenceButton->setEnabled ( true );
}

void QNMRWidget::OnApplyReferences()
{
  emit references ( m_handler );
}

void QNMRWidget::OnSetNucleus ( const QString& nucleus )
{
  if ( m_handler->References().find ( std::string ( nucleus.latin1() ) ) != m_handler->References().end() )
  {
    float value=m_handler->References() [std::string ( nucleus.latin1() ) ];
    _referenceBox->setValue ( value );
  }
}

