/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
 * This file is a test script.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skgtestmacro.h"
#include "skgimportexportmanager.h"
#include "skgbankincludes.h"

/**
 * The main function of the unit test
 * @param argc the number of arguments
 * @param argv the list of arguments
 */
int main(int argc, char** argv)
{
    Q_UNUSED(argc);
    Q_UNUSED(argv);

    // Init test
    SKGINITTEST(true);

    QStringList extensions;
    extensions << "csv" << "cfo" << "gnucash" << "gsb" << "kmy" << "mmb" << "mt940" << "ofx" << "qif" << "skg" << "xhb";

    int nb = extensions.count();
    for (int i = 0; i < nb; ++i) {
        QString ext = extensions.at(i);
        QString filename = "http://skrooge.org/files/skgtestimporturl/test." % ext;
        SKGTRACE << i + 1 << "/" << nb << ": Import " << filename << endl;

        // Test import
        SKGDocumentBank document1;
        SKGTESTERROR("document1.initialize()", document1.initialize(), true);
        SKGError err;
        {
            // Scope of the transaction
            SKGBEGINTRANSACTION(document1, "IMPORT", err);

            SKGImportExportManager imp1(&document1, QUrl::fromUserInput(filename));
            SKGTESTERROR(ext % ".importFile", imp1.importFile(), true);
        }

        if (ext == "skg" && SKGServices::getEnvVariable("USER") == "s") {
            QStringList extensionsExport;
            extensionsExport.push_back("csv");
            extensionsExport.push_back("kmy");
            extensionsExport.push_back("qif");
            extensionsExport.push_back("skg");
            extensionsExport.push_back("xml");
            extensionsExport.push_back("sqlite");

            int nb2 = extensionsExport.count();
            for (int j = 0; j < nb2; ++j) {
                QString ext2 = extensionsExport.at(j);
                QString filename2 = "ftp://skrooge.org/files/skgtestimporturl/output/test." % ext2;
                SKGTRACE << "    " << j + 1 << "/" << nb2 << ": Export " << filename2 << endl;

                // Scope of the transaction
                SKGBEGINTRANSACTION(document1, "EXPORT", err);

                SKGImportExportManager imp1(&document1,  QUrl::fromUserInput(filename2));
                SKGTESTERROR(ext2 % ".exportFile", imp1.exportFile(), true);
            }
        }
    }

    {
        SKGDocumentBank document1;
        SKGTESTERROR("document1.initialize()", document1.initialize(), true);
        SKGTESTERROR("document1.load()", document1.load("http://skrooge.org/files/skgtestimporturl/test.skg"), true);
    }

    // End test
    SKGENDTEST();
}
