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

// "Adjectives_IdentOrder" function allows not to identify the LC when there is an error in the order of the adjectives

module.exports = function(ruleManager, sentence, result) {
    // "result" est un objet {tokenRules:Array[1],TokenIndices[1],mainTokenIndices:[]}
    if (!result ||  !result.tokenIndices || !sentence)
        return false;
    // var refTypes = ruleManager.dictionaries.adjectives_type;
    //var refTypes = ruleManager.dictionaries["adjectives_type"];
    // var typesListe = new Map;
    // typesListe = Object.keys(refTypes);
    // if (!refTypes)
    // {
    //     debugger;
    //     return false;
    // }
    // if (refTypes.length == undefined || refTypes.length === 0){
    //     return false;
    //     console.log("dico est vide");
    // }
    // var tokenID = -1;
    // var refID = -1;

    // for (var key in refTypes)
    // {
    //     var indice = refTypes[key];
    // }

    // ERIC
    var prevIndice = -1;
    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];
        // we put in a list the type of the current token
        //var index = refTypes.indexOf(token.adjectiveType);
        //if (index==-1)
        var index = refTypes[token.adjectiveType];
        if (!index)
        {
            //debugger;
            //return false;
        }
        if (prevIndice > index)
        {
            return false;
        }
        prevIndice = index;
    }
    return true;



    // IRINA
    var resultTypes = [];
    // we go through the indexes of the detected adjectives 
    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];
        // we put in a list the type of the current token
        resultTypes.push(token.adjectiveType);
    }
    //console.log(" sorti du boucle for");
    // On filtre refType avec le contenu de resultTypes
    //resultTypes.includes("toto");
//     const filterRefTypes = refTypes.filter(function(adjType,index, array){
//         return resultTypes.indexOf(adjType)!=-1;
// });
    var prevIndices = -1;
    for (var i=0; i<resultTypes.length; i++)
    {
        var index = refTypes.indexOf(resultTypes[i]);
        if (index==-1)
        {
            debugger;
            return false;
        }
        if (prevIndices > index)
        {
            return false;
        }
        prevIndices = index;
    }
    return true;


        // if (refTypes[token.adjectiveType] != undefined)
        // {
        //     tokenID = ti;
        //     console.log(" word "+token.word+" tokenID "+tokenID);
        //     refID = typesListe[token.adjectiveType];
        //     console.log(" type "+token.adjectiveType+" refID "+refID);
        //     break;
        // }
        // else{
        //     console.log("undefined= "+tokenIndice+" word "+token.word);
        // }
        // for (var key in refTypes)
        // {
        //     var refType == 
        //     if (token.adjectiveType != refType)
        //     {
        //         console.log("undefind");
        //     }
        //     else {
        //         tokenID = refTypes.indexOf(refType);
        //     }
        // }
    // }

    return true;
};