/***************************************************************************
 *   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 <iostream>

#include <kurl.h>
#include <kio/job.h>
#include <qfileinfo.h>
#include <qstringlist.h>
#include <qdir.h>

#include "nmrfile.h"

NMRFile::NMRFile()
{}


NMRFile::~NMRFile()
{}

NMRFile::NMRFileType NMRFile::GetFileType(QString& file)
{

  QFileInfo ft(file);
 // std::cout << "file is " << std::endl;
  //Take into account that file can point to a directory containing the data of to the own data

  //First, resolve symlinks
  if ( ft.isSymLink() ) ft=QFileInfo(ft.readLink());

  if( ft.isDir() )
  {
    //std::cout << file << " IsDir" << std::endl;
    QDir dir(file);
    QStringList filelist=dir.entryList();
    // if exists 1r should be a Bruker processed spectrum, if acqus Bruker FID and procpar if Varian
    QStringList::Iterator it;

    for(it=filelist.begin();it!=filelist.end();it++)
    {
      // std::cout << "comp " << *it << std::endl;
      if( (*it) == "acqus" ) return BrukerFID;
      if( (*it) == "1i" || (*it) == "1r" ) return BrukerSpectrum;
      if( (*it) == "procpar" ) return VarianFID;
    }

    return None;
  }

  if( ft.isFile() )
  {
    QString  fname=ft.fileName();

    if( fname == "fid" )  //could be Varian or Bruker
    {
     /* QString fdir=file;
      fdir.setLength(file.length()-fname.length());
      std::cerr << "directory is " << fdir << std::endl;*/
      QFileInfo ft(file);
      QStringList::Iterator it;
      QStringList filelist=ft.dir(true).entryList();
      for(it=filelist.begin();it!=filelist.end();it++)
      {
        if( (*it) == "acqus" ) return BrukerFID;
        if( (*it) == "procpar" ) return VarianFID;
      }
      return None;
    }

    if (fname == "fid.raw")  return RawAudio;

    if(ft.extension(false) == "gemini") return Gemini;

    if( fname == "1i" || fname == "1r" ) return BrukerSpectrum;
    // if( fname == "procpar" ) return VarianFID;

    if ( ft.extension(false) == "dx" || ft.extension(false) == "jdx" ) return JCAMPDX;

  }

  return None;
}



void NMRFile::OnDataArrived(KIO::Job*, const QByteArray& data)
{}

void NMRFile::OnJobResult(KIO::Job* job)
{

  std::cout << "On Job Result" << std::endl;
  if ( job->error() )
  {
    std::cout << "Error is " << job->error() << std::endl;
  }
}


