var refTypes = require("./adjectiveTypes");


module.exports = function(ruleManager, sentence, result) {
    if (!result || !result.tokenIndices || !sentence)
        return false;

    var indexesTable = [];
    for (var i=0; i<8;i++)
    {
        indexesTable.push([]);
    }
    var unknowns =[];
    var prevIndice = -1;
    var mistakes = [];
    result.log = "";
    
    for (var ti = 0; ti < result.tokenIndices.length; ti++) {
        // "tokenIndice" is the current index in the result
        var tokenIndice = result.tokenIndices[ti];
        // we identifie the token of the sentence corresponding to the index of the result
        var token = sentence.tokens[tokenIndice];
        //var prevToken = sentence.tokens[tokenIndice - 1];
        // if (prevToken )
        // we put in a list the type of the current token
        var index = refTypes[token.adjectiveType];
        
        // var listKeys = Object.keys(indexesTable);
        // console.log("La taille de tableau d'indexes est "+Object.keys(indexesTable).length);
        if (index == undefined) {
           unknowns.push(tokenIndice);
           continue;//repart à l'expression de mise à jour de la boucle for
        }
        indexesTable[index].push(tokenIndice);
        //
        if (prevIndice > index) {
            mistakes.push(tokenIndice);
            // console.log("Mot d'erreur : " + sentence.tokens[tokenIndice].word);
            //break;
        }

        prevIndice = index;

    }
    if (mistakes.length)
    {
        var iti = 0;
        while (iti<indexesTable.length && !indexesTable[iti].length)
        {
            iti++;
        }
        if (iti<indexesTable.length)
        {
            var tokenIndices = indexesTable[iti];
            for (var tii = 0; tii<tokenIndices.length; tii++)
            {
                result.log += sentence.tokens[tokenIndices[tii]].word + ", ";
            }
            var prevTokenIndice = indexesTable[iti][indexesTable[iti].length-1];
            iti++;
            for (; iti<indexesTable.length; iti++)
            {
                tokenIndices = indexesTable[iti];
                if (!tokenIndices.length)
                {
                    continue;
                }
                for (var tii = 0; tii<tokenIndices.length; tii++)
                {
                    // if (prevTokenIndice+1 != tokenIndices[tii] )
                    {
                        var correction = {
                            type : "moveAfter",
                            tokenIndice: tokenIndices[tii],
                            targetTokenIndice: prevTokenIndice
                        };
                        ruleManager.AddCorrection(result, 0, correction);
                    }
                    prevTokenIndice = tokenIndices[tii];
                    result.log += sentence.tokens[tokenIndices[tii]].word + ", ";
                }
            }
        }
        else{
            debugger;
        }
        // var correction = {
        //     type : "modifyTokenIndices",
        //     tokenIndices: {},
        //     // tokenIndiceWithErrors: mistakes
        // };
        // var counter = 0;
        // for (var iti=0; iti<indexesTable.length; iti++)
        // {
        //     // console.log("indexesTable[iti].length"+indexesTable[iti].length);
        //     for (var itti=0; itti<indexesTable[iti].length; itti++)
        //     {
        //         var tokenInd = indexesTable[iti][itti];
        //         var newIndice = result.tokenIndices[counter];
        //         while (unknowns.indexOf(newIndice) != -1)
        //         {
        //             // result.log += sentence.tokens[tokenInd].word + ", ";
        //             counter++;
        //             if (counter >= result.tokenIndices.length)
        //             {
        //                 debugger;
        //                 break;
        //             }
        //             newIndice = result.tokenIndices[counter];
        //         }

        //         correction.tokenIndices[tokenInd] = newIndice;
                
        //         counter++;
        //         result.log += sentence.tokens[tokenInd].word + ", ";
        //         //correctIndex.push(tokenIndice);
        //         //var tok = sentence.tokens[tokenIndice].word;
        //         //correctTokens.push(tok);
        //         // console.log(tok);
                
        //     }
        // }
    }
    
        //  try{
        //     Object.keys(indexesTable);
        //     Object.values(indexesTable);
        //  console.log("Liste des valeurs ");
        // }
        // catch(e)
        // {
        //     console.error(e);
        // }

        // console.log(Object.entries(indexesTable)[0]);
        // console.log("pair de l'objet "+pair);

        // while (; i++)
        // {
        //    while (indexesTable[mistake]>indexesTable[mistake+1])
        //    {

        //    }
        // }

    //}
    // 2: tokenIndice
    // if (!result.corrections[2])
    // {
    //     result.corrections[2] = [];
    // }
    // result.corrections[2].push(correction);
    // var correction2 = {
    //     tokenIndice: ?
    //     type: "modifyWord",
    //     word: "toto"
    // }
    // result.corrections.push(correction2);
    // result.corrections[2].push(correction2);
    // result.log = "The adjective "+"\""+prevToken.word+"\""+" is in a wrong place.";
    // return true;

    // if (!result.corrections)
    // {
    //     result.corrections = [];
    // }


    // var correction = {
    //     tokenIndice: ?
    //     type: "moveBefore",
    //     targetTokenIndice: 1
    // };
    // result.corrections.push(correction);
    // console.log(result.log);
    if (mistakes.length)
        return true;
    return false;
};