/*
* Function checking if a noun is a compound number.
*
* compound cardinal number "twenty one"
*
* compound ordinal number "twenty first"
*
* Les combinaisons incorrects: "twentieth-first", "twentieth-one"
*/

module.exports = function (ruleManager, sentence, result) {

    if (!ruleManager || !result || !result.tokenIndices || result.tokenIndices.length < 2 || !sentence) {
        debugger;
        console.error("inputs are wrong");
        return false;
    }

    if (result.tokenRules.length < 2) {
        return false;
    }

    var dico_ordinalNumbers = ruleManager.dictionaries.ordinal_numbers;

    if (!dico_ordinalNumbers) {
        debugger;
        return false;
    }

    var dico_cardinalNumbers = ruleManager.dictionaries.numbers;

    if (!dico_cardinalNumbers) {
        debugger;
        return false;
    }

    var dico_Ord_Card_numberCorrespondence = ruleManager.dictionaries["ordinal-cardinal_number_correspondence"];

    if (!dico_Ord_Card_numberCorrespondence) {
        debugger;
        return false;
    }

    var dico_Card_Ord_numberCorrespondence = ruleManager.dictionaries["cardinal-ordinal_number_correspondence"];

    if (!dico_Card_Ord_numberCorrespondence) {
        debugger;
        return false;
    }

    if (result.tokenRules[1].tokenIndices.length > 1) {
        return false;
    }

    let compound_noun = sentence.tokens[result.tokenRules[1].tokenIndices[0]].word;
    let compoundNoun_Index = result.tokenRules[1].tokenIndices[0];
    // console.log(compound_noun);

    if (!compound_noun) {
        debugger;
        return false;
    }

    let numbers = compound_noun.split("-");
    let number1 = numbers[0];
    let number2 = numbers[1];

    let number1CardinalType = 0;
    let number2CardinalType = 0;

    for (var key in dico_cardinalNumbers) {
        if (number1 === key) {
            // console.log("le premier mot " + number1 + " est un nombre cardinal");
            number1CardinalType = 1;
            debugger;
            break;
        }
    }

    for (var key in dico_cardinalNumbers) {
        if (number2 === key) {
            // console.log("le deuxième mot " + number2 + " est un nombre cardinal");
            number2CardinalType = 1;
            //debugger;
            break;
        }
    }

    // console.log("number1CardinalType = " + number1CardinalType);
    // console.log("number2CardinalType = " + number2CardinalType);

    if (number1CardinalType == 1) {
        // console.log("le premier mot " + number1 + " est un nombre cardinal");
        debugger;
        return false;
    }

    let number1OrdinalType = 0;
    let number2OrdinalType = 0;

    for (var key in dico_ordinalNumbers) {
        if (number1 === key) {
            // console.log("le premier mot " + number1 + " est un nombre ordinal");
            number1OrdinalType = 1;
            //debugger;
            break;
        }
    }

    for (var key in dico_ordinalNumbers) {
        if (number2 === key) {
            // console.log("le deuxième mot " + number2 + " est un nombre ordinal");
            number2OrdinalType = 1;
            //debugger;
            break;
        }
    }

    // console.log("number1OrdinalType = " + number1OrdinalType);
    // console.log("number2OrdinalType = " + number2OrdinalType);

    if (number1OrdinalType == 0) {
        // console.log("le premier mot " + number1 + " n'est pas un nombre ordinal");
        debugger;
        return false;
    }

    if ((number1CardinalType == number1OrdinalType) || (number2CardinalType == number2OrdinalType)) {
        // console.log(compound_noun + " n'est pas un nombre");
        debugger;
        return false;
    }

    // erreur 1: twentieth-first
    if (((number1CardinalType == 0) && (number1OrdinalType == 1)) && ((number2CardinalType == 0) && (number2OrdinalType == 1))) {

        for (var key in dico_Ord_Card_numberCorrespondence) {
            if (number1 === key) {
                // console.log(number1 + " n'est pas correct");
                number1 = dico_Ord_Card_numberCorrespondence[number1].Cardinal;
                // console.log(number1 + " est correct");
                compound_noun = number1.concat("-",number2);
                // console.log("compound_noun corrigé: " + compound_noun);

                var correction = {
                    "type": "replaceAttValue",
                    "suggestionIndice": 0,
                    "att": "word",
                    "text": compound_noun,
                    "tokenIndice": compoundNoun_Index
                };
            
                ruleManager.AddCorrection(result, 0, correction);

                var correction = {
                    "type": "replaceAttValue",
                    "suggestionIndice": 0,
                    "att": "lemma",
                    "text": compound_noun,
                    "tokenIndice": compoundNoun_Index
                };
            
                ruleManager.AddCorrection(result, 0, correction);

                //debugger;

                return true;
            }
        }
    }

// erreur 2 : twentieth-one

    if (((number1CardinalType == 0) && (number1OrdinalType == 1)) && ((number2CardinalType == 1) && (number2OrdinalType == 0))) {

        for (var key in dico_Ord_Card_numberCorrespondence) {
            if (number1 === key) {
                // console.log(number1 + " n'est pas correct");
                number1 = dico_Ord_Card_numberCorrespondence[number1].Cardinal;
                // console.log(number1 + " est correct");
                for (var key in dico_Card_Ord_numberCorrespondence) {
                    if (number2 === key) {
                        // console.log(number2 + " n'est pas correct");
                        number2 = dico_Card_Ord_numberCorrespondence[number2].Ordinal;
                        // console.log(number1 + " est correct");
                    }
                }
                compound_noun = number1.concat("-",number2);
                // console.log("compound_noun corrigé: " + compound_noun);

                var correction = {
                    "type": "replaceAttValue",
                    "suggestionIndice": 0,
                    "att": "word",
                    "text": compound_noun,
                    "tokenIndice": compoundNoun_Index
                };
            
                ruleManager.AddCorrection(result, 0, correction);

                var correction = {
                    "type": "replaceAttValue",
                    "suggestionIndice": 0,
                    "att": "lemma",
                    "text": compound_noun,
                    "tokenIndice": compoundNoun_Index
                };
            
                ruleManager.AddCorrection(result, 0, correction);

                //debugger;

                return true;
            }
        }
    }

    return false;

};