/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "fileimport/importer.h"
#include "fileimport/importerinterface.h"
#include "fileimport/garmintrainingcenterimporter.h"
#include "fileimport/gpximporter.h"
#include "fileimport/fitnesslogimporter.h"

#include "util/xml.h"

#include <QXmlStreamReader>
#include <QFile>
#include <QStringList>

static ImporterInterfaces importers()
{
	ImporterInterfaces rval;

	rval.append(new GarminTrainingCenterImporter());
	rval.append(new GpxImporter());
	rval.append(new FitnessLogImporter());

	return rval;
}

Importer::Importer(const QStringList& fileNames_) :
	m_FileNames(fileNames_),
	m_Activities(),
	m_Importers(importers()),
	m_RunningImporter(NULL),
	m_ErrorFileNames()
{
	foreach(ImporterInterface* imp, m_Importers)
		connect(imp, SIGNAL(progress(int)), SIGNAL(progress(int)));
}

Importer::~Importer()
{
	qDeleteAll(m_Importers);
	m_Importers.clear();
}

QStringList Importer::supportedFileFormats()
{
	ImporterInterfaces imps = importers();

	QStringList rval;

	foreach(ImporterInterface* imp, imps)
		rval << imp->fileFormat();

	qDeleteAll(imps);
	return rval;
}

void Importer::run()
{
	emit totalSteps(m_FileNames.size() - 1);

	int count = 0;

	bool ok = false;

	foreach (const QString& fileName, m_FileNames)
	{
		QFile file(fileName);

		ok = false;

		if (file.open(QIODevice::ReadOnly))
		{
			QXmlStreamReader xml(&file);

			if (xmlReadNextStartElement(xml))
			{
				foreach(ImporterInterface* imp, m_Importers)
				{
					if (imp->canImport(xml.name().toString()))
					{
						ok = imp->run(xml, activities());
						break;
					}
				}
			}
		}

		if (!ok)
			m_ErrorFileNames << fileName;

		emit progress(count++);
	}
}
