/***************************************************************************
 *   Copyright (C) 2006 by Matthew Williams <matt@milliams.com>            *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

//header
#include "keducamainwindow.h"

#include "mainwidget.h"

//kde
#include <KApplication>
#include <KConfigDialog>
#include <KStatusBar>
#include <KActionCollection>
#include <KDebug>
#include <KLocale>
#include <KFileDialog>
#include <KIO/NetAccess>
#include <QDomDocument>

#include "kqtiassessmentitem.h"

//generated
//#include "settings.h"

KEducaMainWindow::KEducaMainWindow() : KXmlGuiWindow(), mainWidget(new MainWidget), assessmentItem(0)
{
    setCentralWidget(mainWidget);
    initObject();
}

KEducaMainWindow::~KEducaMainWindow()
{
}

void KEducaMainWindow::initObject()
{
    setupActions();
    //statusBar()->show();
}

void KEducaMainWindow::setupActions()
{
    KStandardAction::open(this, SLOT(openTest()), actionCollection());
    
    KStandardAction::quit(this, SLOT(close()), actionCollection());
    
    KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());

    setupGUI();
}

void KEducaMainWindow::openTest()
{
    QString fileName = KFileDialog::getOpenFileName();
    
    QString tmpFile;
    if( KIO::NetAccess::download( fileName, tmpFile, this ) )
    {
        QFile file(tmpFile);
        if(!file.open(QIODevice::ReadOnly))
        {
            return;
        }
        QDomDocument doc;
        doc.setContent(&file);
        kDebug() << "Opening " << fileName << "( tmp =" << tmpFile << ")";
        
        if (assessmentItem)
        {
            delete assessmentItem;
        }
        assessmentItem = new KQtiAssessmentItem(doc);
        
        kDebug() << endl << assessmentItem->document().toString().toLatin1();

        KIO::NetAccess::removeTempFile(tmpFile);
        
        mainWidget->setItemBody(assessmentItem->itemBody());
        file.close();
    }
    else
    {
        kDebug() << "FileRead::openFile(): download NOT successful: " << fileName << endl;
    }
}

void KEducaMainWindow::optionsPreferences()
{
    /*KConfigDialog *dialog = new KConfigDialog(this, "settings", Settings::self());

    QWidget *displaySettingsDialog = new QWidget;
    ui_prefs_display.setupUi(displaySettingsDialog);
    dialog->addPage(displaySettingsDialog, i18n("Display"), "preferences-desktop-display");

    QWidget *aiSettingsDialog = new QWidget;
    ui_prefs_ai.setupUi(aiSettingsDialog);
    dialog->addPage(aiSettingsDialog, i18n("Computer Player"), "user");

    connect(dialog, SIGNAL(settingsChanged(const QString &)), m_view, SLOT(setBoardSize()));
    dialog->show();*/
}

#include "keducamainwindow.moc"
