/***************************************************************************
                           quizwidget.cpp  -  description
                             -------------------
    begin                : Tue Feb 12 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

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

#include <stdlib.h>
#include <time.h>

#include <kmainwindow.h>
#include <kpushbutton.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kdebug.h>

#include <qtimer.h>
#include <qfont.h>

#include "quizwidget.h"
#include "quizwidget.moc"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

QuizWidget::QuizWidget(KStatusBar *statusBar, QWidget *parent, const char *name) : QWidget(parent, name) 
{
  completeCardList = 0;
  reversed = false;

  srand(time(0));

  mainStatusBar = statusBar;

  progressBar = 0;
  feedbackLabel = 0;
  
  showAnswer = 2;

  setupLayout();
}

QuizWidget::~QuizWidget()
{
  if(mainStatusBar)
    delete(mainStatusBar);
}

void QuizWidget::setCardList(const CardList *list)
{
  completeCardList = list;
  remainingCardList = *list;
}

bool QuizWidget::frontBackReversed() const
{
  return(reversed);
}

////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

Card QuizWidget::getCard(bool random)
{
  if(!completeCardList->isEmpty()) {
    if(remainingCardList.isEmpty()) {
      KMessageBox::information( this, completeMessage(), i18n("FlashKard"));
      remainingCardList = *completeCardList;
    }
    
    int cardNumber;
    if(random)
      cardNumber = rand() % remainingCardList.count();
    else
      cardNumber = 0;
    
    currentCard = remainingCardList[cardNumber];
    remainingCardList.remove(remainingCardList.at(cardNumber));

    currentCard.setFrontBackReversed(reversed);

    showCard(currentCard);

    if(completeCardList->count() > 0)
      progressBar->setValue(int(100.0 * (1.0 - float(remainingCardList.count()) / float(completeCardList->count() - 1)) + .5));

    return(currentCard);
  }
  else {
    return(Card(QString::null, QString::null));
  }
}

void QuizWidget::setFrontBackReversed(bool reverse)
{
  if(reversed != reverse) {
    currentCard.setFrontBackReversed(reverse);
    showCard(currentCard);
  }

  reversed = reverse;
}

void QuizWidget::setShowAnswerLength(int length)
{
  showAnswer = length;
}

////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

QBoxLayout *QuizWidget::layout()
{
  return(mainLayout);
}

void QuizWidget::setFeedbackMessage(const QString &message)
{
  if(feedbackLabel) {
    feedbackLabel->setText(message);
    QTimer::singleShot( 2 * 1000, feedbackLabel, SLOT(clear())); // clear the label after 2 seconds
  }
}

void QuizWidget::showFeedbackLabel(bool show)
{
  if(feedbackLabel) {
    if(show)
      feedbackLabel->show();
    else 
      feedbackLabel->hide();
  }
}

// This should be the same as DataWidget::eventFilter() -- I'm just too lazy at the 
// moment to create a base class for QuizWidget and DataWidget that has just this.

bool QuizWidget::eventFilter(QObject *object, QEvent *event)
{
  if(event && event->type() == QEvent::FocusIn) {
    if(KLineEdit *lineEdit = dynamic_cast<KLineEdit *>(object))
      emit(lineEditFocusIn(lineEdit));
  }
  if(event && event->type() == QEvent::FocusOut) {
    QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
    if(dynamic_cast<KLineEdit *>(object) && focusEvent->reason() != QFocusEvent::Popup)
      emit(lineEditFocusOut());
  }
  return(QWidget::eventFilter(object, event));
}

QString QuizWidget::completeMessage()
{
  return(i18n("Restarting the deck."));
}

int QuizWidget::numberOfCards()
{
  if(completeCardList)
    return(completeCardList->size());
  else
    return(0);
}

int QuizWidget::showAnswerLength()
{
  return(showAnswer);
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void QuizWidget::setupLayout()
{ 
  // setup statusbar
  if(mainStatusBar) {
    //    QBoxLayout *layout = new QHBoxLayout(mainStatusBar);
    //    layout->setAutoAdd(true); 
    
    feedbackLabel = new QLabel(mainStatusBar);
    feedbackLabel->setAlignment(AlignHCenter | AlignAuto | AlignVCenter | ExpandTabs);
    feedbackLabel->setFixedWidth(100);
    mainStatusBar->addWidget(feedbackLabel, 1);
    showFeedbackLabel(false);
    
    progressBar = new KProgress(mainStatusBar);
    progressBar->setMaximumHeight(mainStatusBar->height() - 12);
    mainStatusBar->addWidget(progressBar, 3);
  }
  
  // main layout 
  mainLayout = new QVBoxLayout(this, 3, 2 , "mainLayout");
}
