/*
* 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) {

    if (!ruleManager || !result || !result.tokenIndices || result.tokenIndices.length < 2 || !sentence) {
        console.error("inputs are wrong");
        return false;
    }
    else {
        var article = sentence.tokens[result.tokenIndices[0]].word;
        var articleTokenIndice = result.tokenIndices[0];
        // console.log("articleTokenIndice : " + articleTokenIndice);
        var wordAfterArticle = sentence.tokens[result.tokenIndices[1]].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(article, articleTokenIndice, 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(article, articleTokenIndice, 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(article, articleTokenIndice, ruleManager, result);
                                    }
                                    else
                                        return IsCorrectionToA(article, articleTokenIndice, ruleManager, result);
                                }

                            }
                        }
                        return IsCorrectionToAn(article, articleTokenIndice, ruleManager, result);
                    }
                    //return IsCorrectionToA(article, articleTokenIndice, ruleManager, result);  
                }
                return IsCorrectionToA(article, articleTokenIndice, ruleManager, result);
            }

            return IsCorrectionToAn(article, articleTokenIndice, 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(article, articleTokenIndice, 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(article, articleTokenIndice, ruleManager, result);
                            }
                        }
                    }
                }
            }
            return IsCorrectionToA(article, articleTokenIndice, 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(articleAn, articleTokenIndiceAn, ruleManagerAn, resultAn)
// si l'article "a", il est corrigé
{
    if (articleAn === "a") {
        var correction = {
            type: "replaceAttValue",
            tokenIndice: articleTokenIndiceAn,
            att: "word",
            text: "an"
        };
        ruleManagerAn.AddCorrection(resultAn, 0, correction);
        return true;
    }
    else if (articleAn === "A") {
        var correction = {
            type: "replaceAttValue",
            tokenIndice: articleTokenIndiceAn,
            att: "word",
            text: "An"
        };
        ruleManagerAn.AddCorrection(resultAn, 0, correction);
        return true;
    }
    return false;
}

function IsCorrectionToA(articleA, articleTokenIndiceA, ruleManagerA, resultA)
// si l'article "an", il est corrigé
{
    if (articleA === "an") {
        var correction = {
            type: "replaceAttValue",
            tokenIndice: articleTokenIndiceA,
            att: "word",
            text: "a"
        };
        ruleManagerA.AddCorrection(resultA, 0, correction);
        return true;
    }
    else if (articleA === "An") {
        var correction = {
            type: "replaceAttValue",
            tokenIndice: articleTokenIndiceA,
            att: "word",
            text: "A"
        };
        ruleManagerA.AddCorrection(resultA, 0, correction);
        return true;
    }
    return false;
}