#include "Revision.h"

using namespace std;

/// Verb >
Verb::Verb( string mot[4] )
{
    for ( int i(0); i<4; i++ )
        m_mot[i] = mot[i];
}

void Verb::afficher( unsigned int hide )
{
    for ( unsigned int i(0); i<4; i++ )
    {
        if ( i != hide )
            cout << "???";
        else
            cout << m_mot[i];

        cout << "  -  ";
    }

    cout << endl;
}

string Verb::get( unsigned int select )
{
    if ( select < 4 )
        return m_mot[ select ];
    return "FAILURE:HORS BORNES";
}


/// Revision >
Revision::Revision():m_total(0)
{
    srand(time(0));
}

Revision::~Revision()
{
    while ( !m_file.empty() )
    {
        delete m_file.front();
        m_file.pop();
    }
}

bool Revision::load( string file )
{
    /// Démarrage
    ifstream flux( file.c_str() );
    string mot[4];
    unsigned int n(0);
    Verb* verbTempo;

    /// Lecture
    while ( flux >> mot[n] )
    {
        n++;
        if ( n == 4 )
        {
            verbTempo = new Verb( mot );
            m_file.push( verbTempo );
            n = 0;
            m_total++;
        }
    }

    /// Fin
    return 0;
}

void Revision::afficher()
{
    m_select = rand() % 4;
    m_file.front()->afficher( m_select );
}

bool Revision::corriger( string saisie[3] )
{
    /// Vérification
    bool exact(true);
    unsigned int i(0), n(0);
    for ( i = 0; i<4; i++)
    {
        if ( i != m_select ) {
            if ( m_file.front()->get( i ) != saisie[n] )
                exact = false;
            n++;
        }
    }

    /// Réussite
    if ( exact ) {
        cout << "\033[1;32m" << "Well done ! Progression : ";
        cout << 100 - ( m_file.size() * 100 / m_total ) << "%" << "\033[0m" << endl;

        /// Retirer de la liste
        delete m_file.front();
        m_file.pop();
        if ( m_file.empty() ) return false;
    }

    /// Echec
    else {
        cout << "\033[1;31m" << "Oups ! You will try again later ..." << "\033[0m" << endl;
        cout << "The good answer were";
        for ( int i(0); i<4; i++ )
            cout << ' ' << '"' << m_file.front()->get( i ) << '"';
        cout << endl;

        /// Déplacer à la fin
        Verb* verbTempo( m_file.front() );
        m_file.pop();
        m_file.push( verbTempo );
    }

    cout << endl << "Next :" << endl;
    return true;
}