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

void FormAddition::init()
{
    /*
    QTimer *timer1=new QTimer(this);
    connect(timer1,SIGNAL(timeout()),this,SLOT(timerDone()) );
    timer1->start( 2000, TRUE ); // 2 seconds single-shot timer
    */
    bool ok=false;
    bool refaire=true;
    QString text="";
    while (refaire)
    {
	text=QInputDialog::getText("Enregistrement du nom", "Tapez votre nom svp : ", QLineEdit::Normal,QString::null,&ok,this);
	if ( ok && !text.isEmpty() )
	{
	    refaire=false;
	}
	else
	{ // l'utilisateur n'a rien tapé en nom ou a appuyer sur ANNULER
	    QMessageBox::warning(this,"ERREUR","Vous devez obligatoirement taper un nom !");
	}
    }
    nom_utilisateur=text;
    niveau=0;
    //nouveau();
}
void FormAddition::nouveau()
{
    int nb1,nb2,nb3,niv;
    QString nb1_str,nb2_str,nb3_str;
    srand((unsigned) time (NULL)); // initialisation de la fonction rand
    niv=niveau; // on récupère le niveau choisi
    if (niveau==3) // niveau au hasard
    {
	niv=rand()%3;
	srand((unsigned) time (NULL));
    }
    if (niv==0)
    {
	nb1=1+rand()%9;
	nb2=rand()%(9-nb1+1);
	// conversion des nombres entier de base 10 en QString
	nb1_str=QString::number(nb1,10);
	nb2_str=QString::number(nb2,10);
	nb3_str="?";
    }
    if (niv==1 or niv==2)
    {
	nb1=rand()%8; // premier nombre pris au hasard entre 0 et 8
	nb3=nb1+1+rand()%(9-(nb1+1)+1); // 2ème nombre, il dépend du premier
	// conversion des nombres entier de base 10 en QString
	nb1_str=QString::number(nb1,10);
	nb2_str="?";
	nb3_str=QString::number(nb3,10);
	// déplacement du "?" suivant le niveau 1 ou 2
	if (niveau==2)
	{
	    nb2_str=nb1_str;
	    nb1_str="?";
	}
    }
    // définition des couleurs des labels, ainsi que id_label : numéro du label "?"
    if (nb1_str=="?")
    {
	TextLabelNb1->setPaletteForegroundColor(QColor::QColor (255,255,0));
	TextLabelNb2->setPaletteForegroundColor(QColor::QColor (0,0,0));
	TextLabelNb3->setPaletteForegroundColor(QColor::QColor (0,0,0));
	id_label=1;
    }
    else if (nb2_str=="?")
    {
	TextLabelNb1->setPaletteForegroundColor(QColor::QColor (0,0,0));
	TextLabelNb2->setPaletteForegroundColor(QColor::QColor (255,255,0));
	TextLabelNb3->setPaletteForegroundColor(QColor::QColor (0,0,0));
	id_label=2;
    }
    else
    {
	TextLabelNb1->setPaletteForegroundColor(QColor::QColor (0,0,0));
	TextLabelNb2->setPaletteForegroundColor(QColor::QColor (0,0,0));
	TextLabelNb3->setPaletteForegroundColor(QColor::QColor (255,255,0));
	id_label=3;
    }
    
     // affichage des nombres dans les labels correspondants
    TextLabelNb1->setText(nb1_str);
    TextLabelNb2->setText(nb2_str);
    TextLabelNb3->setText(nb3_str);
    
    QString nb_compteur="0"; // nombre de coups
    LCDNumberCompteur->display(nb_compteur);
    
    QObjectList *l = topLevelWidget()->queryList( "QPushButton" );
    QObjectListIt it( *l ); // iterate over the buttons
    QObject *obj;
    while ( (obj = it.current()) != 0 )
    {
	// for each found object...
	++it;
	if ((((QPushButton*)obj)->isHidden()))
	    ((QPushButton*)obj)->show();
    }
    delete l; // delete the list, not the objects
    
    PushButtonNouveau->setEnabled(false);
}

void FormAddition::click_bouton()
{
    QPushButton * bt=(QPushButton*) sender(); // on récupère le bouton qui a envoyé le signal
    QString nb=bt->text(); // on récupère son texte
    //bt->setEnabled(false);
    bt->hide();
    //PushButtonNouveau->sho
    Click_bouton(nb); // on écrit le texte du bouton dans le label qui comporte le "?"
}

void FormAddition::Click_bouton(QString str_sur_bouton)
{
    if (id_label==1) // on insère le texte du bouton cliqué dans le label correspondant au "?"
    {
	TextLabelNb1->setText(str_sur_bouton);
    }
    else if (id_label==2)
    {
	TextLabelNb2->setText(str_sur_bouton);
    }
    else
    {
	TextLabelNb3->setText(str_sur_bouton);
    }
    verif();
}
void FormAddition::verif()
{
    int nb_compteur=LCDNumberCompteur->intValue();
    nb_compteur++;
    QString nb_compteur_str;
    nb_compteur_str=QString::number(nb_compteur,10);
    LCDNumberCompteur->display(nb_compteur_str);
    
    bool bon;
    QString nb1_str=TextLabelNb1->text();
    QString nb2_str=TextLabelNb2->text();
    QString nb3_str=TextLabelNb3->text();
    bool ok;
    // conversion des chaines en entier de base 10
    int nb1=nb1_str.toInt(&ok,10);
    int nb2=nb2_str.toInt(&ok,10);
    int nb3=nb3_str.toInt(&ok,10);
    
    if (nb1+nb2==nb3) // Vérification de l'opération
	bon=true;
    else
	bon=false;
    //affichage des couleurs suivant si le résultat est juste ou faux
    if (bon)
    {
	if (id_label==1)
	    TextLabelNb1->setPaletteForegroundColor(QColor::QColor (0,255,0));
	else if (id_label==2)
	    TextLabelNb2->setPaletteForegroundColor(QColor::QColor (0,255,0));
	else
	    TextLabelNb3->setPaletteForegroundColor(QColor::QColor (0,255,0));
	PushButtonNouveau->setEnabled(true);
    }
    else
    {
	if (id_label==1)
	    TextLabelNb1->setPaletteForegroundColor(QColor::QColor (255,0,0));
	else if (id_label==2)
	    TextLabelNb2->setPaletteForegroundColor(QColor::QColor (255,0,0));
	else
	    TextLabelNb3->setPaletteForegroundColor(QColor::QColor (255,0,0));
    }
}


void FormAddition::apd()
{
    QMessageBox::information(this, "A propos de l'application...",
    "Ceci est un petit logiciel pour apprendre les additions...\n\n"
    "Auteur : \tMarin Jean-Christophe\n"
    "Date de création : 29/10/2002\n"
    "Type du logiciel : Freeware\n"
    "Des problèmes, des améliorations ? Contactez moi à cette adresse : triscorp@ifrance.com !");
}


void FormAddition::selection_niveau(int num)
{
/* sélection du niveau :
valeur de num :
0=facile (a+b=?)
1=moyen (a+?=b)
2=difficile (?+a=b)
3=Pro (les 3 niveaux)
*/
    niveau=num;
}
