/*
* Function to correct a/an article according to the first character of the following
* word.
* "a" article if followed by a consonant
* "an" article if followed by a vowel
* the exceptions are:
* silent "h",
* "u" in an open syllable
*/
const vowels = { "a": true, "e": true, "i": true, "o": true, "u": true };
//const vowels = ["a", "e", "i", "o", "u"];
// "ruleManager" contains "dictionaries"

const consonants = {
    "b": true, "c": true, "d": true, "f": true, "g": true, "h": true, "j": true, "k": true, "l": true, "m": true, "n": true,
    "p": true, "q": true, "r": true, "s": true, "t": true, "v": true, "w": true, "x": true, "z": true
};

// words with silent "h" that can have derivatives
const excepts = ["honour", "honor", "hour", "honest"];
// const excepts = ["honest"];

//lettres forming one sound when combined with "h"
const oneConsonantSound = ["t", "s", "p", "c", "w"];

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

    if (!ruleManager || !result || !result.tokenIndices || result.tokenIndices.length < 2 || !sentence) {
        debugger;
        console.error("inputs are wrong");
        return false;
    }
    // the word without article
    // if (result.tokenRules.length >= 6 &&
    //     result.tokenRules[6].tokenIndices.length) {
        
        var wordTokenIndice = wordTokenIndiceInsertAfter;
        var wordTokenIndiceNext = wordTokenIndice + 1;
        var wordAfterArticle = sentence.tokens[wordTokenIndiceNext].lemma;
        wordAfterArticle = wordAfterArticle.toLowerCase();
        // console.log("Result words : " + article+ " "+ wordAfterArticle);
        // console.log("Caractère : " + wordAfterArticle[0]);
        var dico = ruleManager.dictionaries.firstSilentH;

        // console.log("Dico : " + Object.keys(dico));

        /*
        * variable "determinerAn" return "true" when the article should be changed to "an"
        */

        var character = wordAfterArticle[0];
        if (!dico) {
            console.error("dictionnary error");
            return false;
        }
        if (IsVowel(character) == true) {
            // if the word after the article starts with a vowel
            if (character == "u") {
                // console.log("Word length : "+ wordAfterArticle.length);
                //if ((wordAfterArticle.length == 1) || ((wordAfterArticle.length == 2) && (wordAfterArticle.endsWith(".")))) {
                if (wordAfterArticle.length <= 2) {
                    return IsCorrectionToA(wordTokenIndice, ruleManager, result);
                }
                //if the word consists of 2 letters and the second is a consonant ==> the pronounciation is [ju] anyway: ut, UN, US

                else if ((IsConsonant(wordAfterArticle[1]) == true) && (wordAfterArticle.length > 2)) {
                    // console.log("The second letter is : " + "\"" + wordAfterArticle[1] + "\"" + " It is a consonant. The word length is " + wordAfterArticle.length)
                    if (IsVowel(wordAfterArticle[2]) == true) {
                        return IsCorrectionToA(wordTokenIndice, ruleManager, result);
                    }
                    else {
                        for (var k in oneConsonantSound) {
                            if (oneConsonantSound[k] === wordAfterArticle[1]) {
                                if (wordAfterArticle[2] === "h") {
                                    if ((wordAfterArticle.length == 3) || (IsConsonant(wordAfterArticle[3]) == true)) {
                                        return IsCorrectionToAn(wordTokenIndice, ruleManager, result);
                                    }
                                    else
                                        return IsCorrectionToA(wordTokenIndice, ruleManager, result);
                                }

                            }
                        }
                        return IsCorrectionToAn(wordTokenIndice, ruleManager, result);
                    }
                    //return IsCorrectionToA(wordTokenIndice, ruleManager, result);  
                }
                return IsCorrectionToA(wordTokenIndice, ruleManager, result);
            }

            return IsCorrectionToAn(wordTokenIndice, ruleManager, result);
        }
        else {
            if (character === "h") {
                // if the word after the article starts with "h"
                // checking if "h" is silent
                for (var key in dico) {
                    if (wordAfterArticle === key)
                    // if the found word is the same as in the dictionnary of words with a silent "h"
                    {
                        return IsCorrectionToAn(wordTokenIndice, ruleManager, result);
                    }
                    else if (wordAfterArticle.length > 4)
                    // if the found word starts with the "h" and is a derivative of one of words in the list
                    {
                        for (var l in excepts) {
                            // console.log("Réponse : " + wordAfterArticle.startsWith(excepts[l]));
                            if (wordAfterArticle.startsWith(excepts[l]) == true) {
                                return IsCorrectionToAn(wordTokenIndice, ruleManager, result);
                            }
                        }
                    }
                }
            }
            return IsCorrectionToA(wordTokenIndice, ruleManager, result);
        }
    // }


    // const consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"];
};

function IsVowel(character) {
    // if (vowels.indexOf(character) != -1) {
    //     return true;
    // }
    return vowels[character];
    // return false;
}

function IsConsonant(character) {
    // if (consonants.indexOf(character) != -1) {
    //     return true;
    // }
    // return false;
    return consonants[character];
}

function IsCorrectionToAn(wordTokenIndiceAn, ruleManagerAn, resultAn)
// insert article "an"
{
    var correction = {
        "type": "insertAfter",
        "token": {
            "pos": "DT",
            "lemma": "a",
            "word": "an"
        },
        "targetTokenIndice": wordTokenIndiceAn
    };
    ruleManagerAn.AddCorrection(resultAn, 0, correction);
    return true;

}

function IsCorrectionToA(wordTokenIndiceA, ruleManagerA, resultA)
// insert article "a"
{
    var correction = {
        "type": "insertAfter",
        "token": {
            "pos": "DT",
            "lemma": "a",
            "word": "a"
        },
        "targetTokenIndice": wordTokenIndiceA
    };
    ruleManagerA.AddCorrection(resultA, 0, correction);
    return true;

}