/***************************************************************************
 *   Copyright (C) 2004 by Kraschinski Arnold                              *
 *   arnold.kra67@gmx.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 Steet, Fifth Floor, Boston, MA 02110-1301, USA.           *
 ***************************************************************************/



#include "kverbos.h"


// Qt header files
#include <QVBoxLayout>
#include <QStackedLayout>
#include <QDomDocument>
#include <QKeyEvent>
#include <QKeySequence>

// KDE header files
#include <kaction.h>
#include <kapplication.h>
#include <kconfig.h>
#include <khelpmenu.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kconfigdialog.h>
#include <kconfigskeleton.h>
#include <krandomsequence.h>
#include <kdebug.h>

//#include <keduvocdocument.h>
//#include <keduvocexpression.h>
//#include <keduvocwordtype.h>
#include <kurl.h>

// project header files
#include "kverbossettings.h"
#include "mainsettings.h"
#include "kvtmlsettings.h"
#include "jigsawsettings.h"
#include "tensesselection.h"
#include "verbselection.h"
#include "controlline.h"
#include "qmenu.h"
#include "qlearning.h"


const int    Kverbos::MENU = 1;
const int    Kverbos::LEARNING = 2;


Kverbos::Kverbos() : QWidget(0)
{
    // Prepare the GUI for the application.
    QVBoxLayout* layout = new QVBoxLayout;
    m_stackedLayout = new QStackedLayout(layout);

    m_menu = new QMenu(this);
    m_stackedLayout->addWidget(m_menu);
    m_learning = new QLearning(this);
    m_stackedLayout->addWidget(m_learning);

    m_controlLine = new ControlLine;
    layout->addWidget(m_controlLine);
    setLayout(layout);

    m_controlLine->connectButtons( this );
    m_controlLine->connectNextButton( m_learning );
    connect(m_menu, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int)), this, SLOT( slotNext() ) );
    connect(m_learning, SIGNAL( stopLearning() ), this, SLOT( slotMenu() ) );
    m_learning->connectTrainerControlline(m_controlLine);

    setMinimumSize(750, 510);
    show();
    m_mode = Kverbos::MENU;

    m_helpMenu = new KHelpMenu(this, KGlobal::mainComponent().aboutData());

    Verbdata::readSettings();

    if ( KVerbosSettings::loadKVTML() ) {
        //loadKVTMLFiles();
        kDebug() << "load the kvtml files here.";
    }
}

Kverbos::~Kverbos()
{
}

void Kverbos::loadKVTMLFiles()
{
/*
    QStringList fileNames = KVerbosSettings::fileList();
    for ( QStringList::iterator it = fileNames.begin(); it < fileNames.end(); it++ ) {
        kDebug() << "load file: " << *it;
        KEduVocDocument vocDoc;
        if (vocDoc.open( KUrl(*it) ) ) {
            kDebug() << "file opened: title: " << vocDoc.title();
            for (int i = 0; i < vocDoc.identifierCount(); i++) {
                kDebug() << "Title: " << vocDoc.identifier( i ).name()<< " Locale: " << vocDoc.identifier( i ).locale();
            }
            for (int i = 0; i < vocDoc.entryCount(); i++) {
                KEduVocExpression* expression = vocDoc.entry(i);
//<<<<<<< .mine
                kDebug() << i << expression;
//=======
                foreach ( int translationIndex,  expression->translationIndices()) {
                    kDebug() << expression->translation(translationIndex).text() << " is " << vocDoc.identifier( translationIndex ).name();
                    if ( expression->translation(translationIndex).type() == vocDoc.wordTypes()->specialTypeVerb() ) {
                        kDebug() << expression->translation(translationIndex).text() << " is a verb!";
                    }
                }

//              kDebug() << i;
//>>>>>>> .r708212
            }
        }
    }
*/
}

void Kverbos::keyPressEvent ( QKeyEvent * event )
{
    if ( event->matches( QKeySequence::HelpContents ) ) {
        m_helpMenu->appHelpActivated();
        return;
    }
    
    if ( event->matches( QKeySequence::New ) ) {
        slotMenu();
        return;
    }

    if ( ( event->key() == Qt::Key_Q ) && (event->modifiers() == Qt::ControlModifier ) )
        kapp->quit();
    
    if ( event->matches( QKeySequence::WhatsThis ) ) {
        kDebug() << "I want to know what this is?";
    }

    event->ignore();
}

void Kverbos::slotConfig()
{
    emit signalStopTimer();
    if(KConfigDialog::showDialog("settings"))
        return;

    KConfigDialog *configDialog = new KConfigDialog( this, "settings", KVerbosSettings::self() );

    configDialog->addPage( new MainSettings( configDialog ), i18n( "General" ), "preferences-other" );
    configDialog->addPage( new TensesSelection( configDialog ), i18n( "Tenses" ), "chronometer" );
    configDialog->addPage( new VerbSelection(configDialog), i18n( "Verbs"), "document-properties");
    configDialog->addPage( new KvtmlSettings( configDialog ), i18n( "KVTML-files" ), "accessories-dictionary" );
    configDialog->addPage( new JigsawSettings( configDialog ), i18n( "Jigsaw" ), "view-preview" );

    configDialog->exec();
    delete configDialog;

    // apply the settings
    m_learning->applySettings();
    Verbdata::readSettings();
    emit signalStartTimer();
}

void Kverbos::slotHandbook()
{
    m_helpMenu->appHelpActivated();
}

void Kverbos::slotAboutApp()
{
    m_helpMenu->aboutApplication();
}

void Kverbos::slotAboutKDE()
{
    m_helpMenu->aboutKDE();
}

void Kverbos::slotNext()
{
    switch (m_mode) {
    case Kverbos::MENU:
        if (m_menu->selected()) {
            //kDebug() << "filename: " << m_menu->selectedFilename() << endl;
            //kDebug() << "classname: " << m_menu->selectedClassname() << endl;
            if ( m_learning->startExercise( m_menu->selectedClassname(), m_menu->selectedFilename() ) ) {
                m_stackedLayout->setCurrentWidget(m_learning);
                m_mode = Kverbos::LEARNING;
            }
        }
        break;
    case Kverbos::LEARNING:
        // In mode learning this class hasn't got to do something.
        break;
    default:
        kDebug() << "This is an error! The mode number " << m_mode << " doesn't exist." << endl;
    }
}

void Kverbos::slotMenu()
{
    if ( m_learning->learning() ) {
        m_learning->stopExercise();
    }
    m_stackedLayout->setCurrentWidget(m_menu);
    m_mode = Kverbos::MENU;
}

void Kverbos::slotTimerExpired()
{
    slotMenu();
}

void Kverbos::slotExit()
{
    kapp->quit();
}



#include "kverbos.moc"
