#include "poilaumodule.h"
#include "message.h"
#include <QTimer>
#include <time.h>

#define VOY_NONE -1
enum{VOY_A, VOY_E, VOY_I, VOY_O, VOY_U, VOY_ET, VOY_AI, VOY_OU, VOY_IN, VOY_ON, VOY_OI, VOY_EN, VOY_OUILLE, VOY_ATTE, VOY_US};

bool PoilAuModule::messageHandler(Message msg)
{
    if (msg.args.startsWith("!")) return false;
    if(!isReady)
        return false;
    if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) != 0)
        return false;

    int voy = VOY_NONE;
    QString truncated = msg.args;
    if(truncated.endsWith("+", Qt::CaseInsensitive))
    {
        voy = VOY_US;
    }
    else
    {
        while(true)
        {
            if(truncated.size() == 1 || truncated.at(truncated.size()-1).isNumber())
                return false;
            if(truncated.at(truncated.size()-1).isLetter() && truncated.at(truncated.size()-2).isLetter())
                break;
            truncated.chop(1);
        }
    }

    if(truncated.endsWith("eu", Qt::CaseInsensitive))
        voy = VOY_E;
    else if(truncated.endsWith("eux", Qt::CaseInsensitive))
        voy = VOY_E;
    else if(truncated.endsWith("au", Qt::CaseInsensitive))
        voy = VOY_O;
    else if(truncated.endsWith("ou", Qt::CaseInsensitive))
        voy = VOY_OU;
    else if(truncated.endsWith("où", Qt::CaseInsensitive))
        voy = VOY_OU;
    else if(truncated.endsWith("u", Qt::CaseInsensitive))
        voy = VOY_U;

    if(truncated.endsWith("oo", Qt::CaseInsensitive))
        voy = VOY_OU;
    else if(truncated.endsWith("o", Qt::CaseInsensitive))
        voy = VOY_O;

    if(truncated.endsWith("ait", Qt::CaseInsensitive))
        voy = VOY_AI;
    if(truncated.endsWith("ais", Qt::CaseInsensitive))
        voy = VOY_AI;
    if(truncated.endsWith("ai", Qt::CaseInsensitive))
        voy = VOY_AI;
    else if(truncated.endsWith("oi", Qt::CaseInsensitive))
        voy = VOY_OI;
    else if(truncated.endsWith("i", Qt::CaseInsensitive))
        voy = VOY_I;

    if(truncated.endsWith("et", Qt::CaseInsensitive))
        voy = VOY_ET;
    if(truncated.endsWith("é", Qt::CaseInsensitive))
        voy = VOY_ET;
    if(truncated.endsWith("er", Qt::CaseInsensitive))
        voy = VOY_ET;
    if(truncated.endsWith("è", Qt::CaseInsensitive))
        voy = VOY_AI;
    if(truncated.endsWith("an", Qt::CaseInsensitive))
        voy = VOY_EN;
    if(truncated.endsWith("en", Qt::CaseInsensitive))
        voy = VOY_EN;
    if(truncated.endsWith("ment", Qt::CaseInsensitive))
        voy = VOY_EN;
    if(truncated.endsWith("in", Qt::CaseInsensitive))
        voy = VOY_IN;
    if(truncated.endsWith("on", Qt::CaseInsensitive))
        voy = VOY_ON;
    if(truncated.endsWith("ont", Qt::CaseInsensitive))
        voy = VOY_ON;
    if(truncated.endsWith("oie", Qt::CaseInsensitive))
        voy = VOY_OI;
    if(truncated.endsWith("ois", Qt::CaseInsensitive))
        voy = VOY_OI;
    if(truncated.endsWith("oit", Qt::CaseInsensitive))
        voy = VOY_OI;
    if(truncated.endsWith("a", Qt::CaseInsensitive))
        voy = VOY_A;
    if(truncated.endsWith("à", Qt::CaseInsensitive))
        voy = VOY_A;

    if(truncated.endsWith("ouille", Qt::CaseInsensitive))
        voy = VOY_OUILLE;
	
	if(truncated.endsWith("atte", Qt::CaseInsensitive))
        voy = VOY_ATTE;

    if(truncated.endsWith("us", Qt::CaseInsensitive))
        voy = VOY_US;

    if(voy == VOY_NONE)
        return false;

    switch(voy)
    {
        case VOY_A :
            answer = say("Poil au bras.");
        break;
        case VOY_E :
            answer = say("Poil aux cheveux.");
        break;
        case VOY_I :
            answer = say("Poil à la vessie.");
        break;
        case VOY_O :
            answer = say("Poil au dos.");
        break;
        case VOY_U :
            answer = say("Poil au cul.");
        break;
        case VOY_US :
            answer = say("Poil à l'anus.");
        break;
        case VOY_ET :
            answer = say("Poil au nez.");
        break;
        case VOY_AI :
            answer = say("Poil au nez.");
        break;
        case VOY_OU :
            answer = say("Poil au cou.");
        break;
        case VOY_IN :
            answer = say("Poil aux mains.");
        break;
        case VOY_ON :
            answer = say("Poil au fion.");
        break;
        case VOY_OI :
            answer = say("Poil aux doigts.");
        break;
        case VOY_EN :
            answer = say("Poil aux dents.");
        break;
        case VOY_OUILLE :
            answer = say("Poil aux couilles.");
        break;
		case VOY_ATTE :
            answer = say("Poil à la chatte.");
        break;
    }
    isReady = false;
    std::srand(time(NULL));
    int waitTime;
    if(std::rand()%2)
        waitTime = std::rand()%900000; // short (0 - 15 min)
    else
        waitTime = std::rand()%9000000; // long (15 min - 3 h)
    QTimer::singleShot(waitTime, (QObject*)this, SLOT(setReady()));
    return true;
}

QString PoilAuModule::getName()
{
    return "poilau";
}

void PoilAuModule::setReady()
{
    isReady = true;
}