/*
* Function changes the pronoun in the question tag
* according to the gender of the subject in the main clause,
* if the genders are different.
**/

function AddCorrections(ruleManager, result, suggestionIndice, tokenIndice, word) {
    let correction = {
        "type": "replaceAttValue",
        "att": "word",
        "text": word,
        "tokenIndice": tokenIndice
    };

    ruleManager.AddCorrection(result, suggestionIndice, correction);

    correction = {
        "type": "replaceAttValue",
        "att": "lemma",
        "text": word,
        "tokenIndice": tokenIndice
    };

    ruleManager.AddCorrection(result, suggestionIndice, correction);
}

// function AddCorrectionsThey(ruleManager, result, suggestionIndice, tokenIndice) {

//     let correction = {
//         "type": "replaceAttValue",
//         "att": "word",
//         "text": "they",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);

//     correction = {
//         "type": "replaceAttValue",
//         "att": "lemma",
//         "text": "they",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);
// }

// function AddCorrectionsShe(ruleManager, result, suggestionIndice, tokenIndice) {
//     correction = {
//         "type": "replaceAttValue",
//         "att": "word",
//         "text": "she",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);

//     correction = {
//         "type": "replaceAttValue",
//         "att": "lemma",
//         "text": "she",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);
// }

// function AddCorrectionsHe(ruleManager, result, suggestionIndice, tokenIndice) {
//     correction = {
//         "type": "replaceAttValue",
//         "att": "word",
//         "text": "he",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);

//     correction = {
//         "type": "replaceAttValue",
//         "att": "lemma",
//         "text": "he",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);
// }

// function AddCorrectionsIt(ruleManager, result, suggestionIndice, tokenIndice) {
//     correction = {
//         "type": "replaceAttValue",
//         "att": "word",
//         "text": "it",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);

//     correction = {
//         "type": "replaceAttValue",
//         "att": "lemma",
//         "text": "it",
//         "tokenIndice": tokenIndice
//     };

//     ruleManager.AddCorrection(result, suggestionIndice, correction);
// }

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

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

    /*
    * Identification of the gender of the pronoun in the question tag
    */

    let qtag_subj_index = result.tokenRules[tokenRuleIndex].tokenIndices[0];

    if (!qtag_subj_index) {
        console.error("problem with the qtag pronoun index");
        debugger;
        return false;
    }

    let qtag_gender = undefined;
    let qtag_pronoun = sentence.tokens[qtag_subj_index].word;

    if (!qtag_pronoun) {
        console.error("problem with the qtag pronoun");
        debugger;
        return false;
    }

    // let qtag_number = undefined;


    if (sentence.tokens[qtag_subj_index].gender != undefined) {
        qtag_gender = sentence.tokens[qtag_subj_index].gender;
    }
    else if (sentence.tokens[qtag_subj_index].pronounNumber != undefined) {
        qtag_number = sentence.tokens[qtag_subj_index].pronounNumber;
    }

    qtag_word = sentence.tokens[qtag_subj_index].word;

    if (!qtag_word) {
        console.error("problem with the qtag pronoun");
        debugger;
        return false;
    }

    /*
    * Identification of the gender of the subject in the main clause
    */

    let main_subj_gender = undefined;
    let main_subj_word = undefined;
    let main_subj_pos = undefined;
    // let main_subj_pl = undefined;
    let subjTokenNumber = result.tokenRules[0].tokenIndices.length;
    let subjectTokenIndex = undefined;
    let sentenceTokenOfsubject_Index = undefined;
    let sentenceTokenOfsubject_pos = undefined;

    /*
    * Verification if the pronoun dictionaries existe
    */

    let personal_pronouns_dico = ruleManager.dictionaries.personal_pronouns;
    if (!personal_pronouns_dico) {
        console.error("dictionnary of personal pronouns error");
        return false;
    }
    let indefinit_pl_pronouns_dico = ruleManager.dictionaries.indefinite_pronouns_as_subjects_pl;
    if (!indefinit_pl_pronouns_dico) {
        console.error("dictionnary of indefinite plural pronouns error");
        return false;
    }

    let indefinit_pronouns_sg_dico = ruleManager.dictionaries.indefinite_pronouns_as_subjects_sg;
    if (!indefinit_pronouns_sg_dico) {
        console.error("dictionnary of singular indefinite pronouns error");
        return false;
    }

    let indefinit_pronouns_sg_pl_dico = ruleManager.dictionaries.indefinite_pronouns_sg_pl;
    if (!indefinit_pronouns_sg_pl_dico) {
        console.error("dictionnary of indefinite pronouns that can be singular or plural error");
        return false;
    }

    let qtag_subj_word = undefined;
    // const indef_pronoun_they = [few, fewer, many, several, others];

    if (subjTokenNumber < 1) {
        console.error("problem with the first token");
        debugger;
        return false;
    }
    else if (subjTokenNumber == 1) {
        // if the 'gender' attribute existe, we don't need further verifications
        if (sentence.tokens[result.tokenRules[0].tokenIndices[0]].gender != undefined) {
            main_subj_gender = sentence.tokens[result.tokenRules[0].tokenIndices[0]].gender;

            if (!main_subj_gender) {
                console.error("problem with the gender of the main subject");
                debugger;
                return false;
            }

            if (qtag_gender === main_subj_gender) {
                //console.log("No error.");
                // debugger;
                return false;
            }
            else if (main_subj_gender == "mf") {
                if (qtag_pronoun == "he") {
                    console.log("No error.");
                    // debugger;
                    return false;
                }
                else if (qtag_pronoun == "she") {
                    console.log("No error.");
                    // debugger;
                    return false;
                }
                else {
                    // debugger;
                    AddCorrections(ruleManager, result, 0, qtag_subj_index, "he");
                    AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");
                    return true;
                }

            }
            else if (main_subj_gender == "mfn") {
                if (qtag_pronoun == "he") {
                    console.log("No error.");
                    // debugger;
                    return false;
                }
                else if (qtag_pronoun == "she") {
                    console.log("No error.");
                    // debugger;
                    return false;
                }
                else if (qtag_pronoun == "it") {
                    console.log("No error.");
                    // debugger;
                    return false;
                }
                else {
                    // AddCorrectionsHe(ruleManager, result, 0, qtag_subj_index);
                    AddCorrections(ruleManager, result, 0, qtag_subj_index, "he");
                    // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                    AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");
                    // AddCorrectionsIt(ruleManager, result, 2, qtag_subj_index);
                    AddCorrections(ruleManager, result, 2, qtag_subj_index, "it");
                    // debugger;
                    return true;
                }

            }
            else {
                if (main_subj_gender == "m") {
                    qtag_subj_word = "he";
                }
                else if (main_subj_gender == "f") {
                    qtag_subj_word = "she";
                }
                else if (main_subj_gender == "n") {
                    qtag_subj_word = "it";
                }
                else {
                    // debugger;
                    return false;
                }

                AddCorrections(ruleManager, result, 0, qtag_subj_index, qtag_subj_word);
                // debugger;
                return true;

            }
        }

        /*
        * The subject can be expressed by 
        * 1. a pronoun
        * 2. a noun
        */
        else {
            main_subj_word = sentence.tokens[result.tokenRules[0].tokenIndices[0]].word;
            main_subj_pos = sentence.tokens[result.tokenRules[0].tokenIndices[0]].pos;

            if (!main_subj_word) {
                console.error("problem with the subject word");
                debugger;
                return false;
            }
            if (main_subj_pos == "PRP") {
                // if the pronoun is a personal pronoun
                for (var key in personal_pronouns_dico) {
                    if (main_subj_word === key) {
                        if (qtag_word === main_subj_word) {
                            //console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else {

                            AddCorrections(ruleManager, result, 0, qtag_subj_index, main_subj_word);
                            // debugger;
                            return true;
                        }
                    }
                }
                for (var key in indefinit_pl_pronouns_dico) {
                    if (main_subj_word === key) {
                        if (qtag_word === "they") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else {

                            // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                            AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");
                            // debugger;
                            return true;
                        }
                    }
                }
                for (var key in indefinit_pronouns_sg_dico) {
                    if (main_subj_word === key) {
                        if (main_subj_word == "none") {
                            if ((qtag_word === "he") || (qtag_word === "she") || (qtag_word === "they") || (qtag_word === "it")) {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {
                                // suggestion 0

                                // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");

                                // // suggestion 1

                                // // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");

                                // // suggestion 2

                                // // AddCorrectionsHe(ruleManager, result, 2, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 2, qtag_subj_index, "he");

                                // // suggestion 3

                                // // AddCorrectionsIt(ruleManager, result, 3, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 3, qtag_subj_index, "it");
                                // // debugger;
                                // return true;
                                return false;
                            }
                        }

                        else if ((main_subj_word == "one") || (main_subj_word == "other") || (main_subj_word == "another")) {
                            if ((qtag_word === "he") || (qtag_word === "she") || (qtag_word === "it")) {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {
                                // suggestion 0

                                // AddCorrectionsIt(ruleManager, result, 0, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 0, qtag_subj_index, "it");

                                // // suggestion 1

                                // // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");

                                // // suggestion 2

                                // // AddCorrectionsHe(ruleManager, result, 2, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 2, qtag_subj_index, "he");

                                // // debugger;
                                // return true;
                                return false;
                            }

                        }

                        else if (main_subj_word.endsWith("body") || main_subj_word.endsWith("one")) {
                            if ((qtag_word === "he") || (qtag_word === "she") || (qtag_word === "they")) {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {
                                // suggestion 0

                                // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");

                                // // suggestion 1

                                // // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");

                                // // suggestion 2

                                // // AddCorrectionsHe(ruleManager, result, 2, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 2, qtag_subj_index, "he");

                                // // debugger;
                                // return true;
                                return false;
                            }
                        }

                        else if (main_subj_word.endsWith("thing") || (main_subj_word == "more") || (main_subj_word == "most") || (main_subj_word == "all")) {
                            //  more /most /some/all
                            // if ((qtag_word === "it") || (qtag_word === "they")) {
                            if (qtag_word === "it") {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {

                                // suggestion 0

                                // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);

                                // suggestion 1

                                // AddCorrectionsIt(ruleManager, result, 0, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 0, qtag_subj_index, "it");

                                // // debugger;
                                // return true;
                                return false;
                            }
                        }

                        else if (main_subj_word.endsWith("where") || (main_subj_word == "less") || (main_subj_word == "little") || (main_subj_word == "much")) {
                            if (qtag_word === "it") {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {

                                // AddCorrectionsIt(ruleManager, result, 0, qtag_subj_index);
                                AddCorrections(ruleManager, result, 0, qtag_subj_index, "it");
                                // debugger;
                                return true;

                            }

                        }

                        else {
                            console.log("unknown gender");
                            // debugger;
                            return false;
                        }

                    }
                }

                for (var key in indefinit_pronouns_sg_pl_dico) {
                    if (main_subj_word === key) {
                        if (main_subj_word == "some") {
                            if ((qtag_word === "it") || (qtag_word === "they")) {
                                console.log("No error.");
                                // debugger;
                                return false;
                            }
                            else {

                                // suggestion 0

                                // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");

                                // // suggestion 1

                                // // AddCorrectionsIt(ruleManager, result, 1, qtag_subj_index);
                                // AddCorrections(ruleManager, result, 1, qtag_subj_index, "it");
                                // // debugger;
                                // return true;
                                return false;
                            }

                        }
                        else {
                            console.log("unknown gender");
                            // debugger;
                            return false;
                        }
                    }

                }

            }

            else if (sentence.tokens[result.tokenRules[0].tokenIndices[0]].pos.startsWith("NN")) {
                if (sentence.tokens[result.tokenRules[0].tokenIndices[0]].pos.endsWith("S")) {
                    if (qtag_word === "they") {
                        console.log("No error.");
                        // debugger;
                        return false;
                    }
                    else {

                        // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                        AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");
                        // debugger;
                        return true;
                    }
                }

                else if (sentence.tokens[result.tokenRules[0].tokenIndices[0]].pos == "NN") {
                    if (qtag_word === "it") {
                        console.log("No error.");
                        // debugger;
                        return false;
                    }
                    else {

                        // AddCorrectionsIt(ruleManager, result, 0, qtag_subj_index);
                        AddCorrections(ruleManager, result, 0, qtag_subj_index, "it");
                        // debugger;
                        return true;
                    }
                }

                else {
                    console.log("subject " + main_subj_word + " unknown gender.");
                    // debugger;
                    return false;
                }

            }
        }
    }

    else if (subjTokenNumber > 1) {
        for (subjectTokenIndex = 0; subjectTokenIndex < subjTokenNumber; subjectTokenIndex++) {
            sentenceTokenOfsubject_Index = result.tokenIndices[result.tokenRules[0].tokenIndices[subjectTokenIndex]];
            sentenceTokenOfsubject_pos = sentence.tokens[sentenceTokenOfsubject_Index].pos;
            if (sentenceTokenOfsubject_pos.startsWith("NN")) {
                if (sentence.tokens[sentenceTokenOfsubject_Index].gender != undefined) {
                    main_subj_gender = sentence.tokens[sentenceTokenOfsubject_Index].gender;

                    if (!main_subj_gender) {
                        console.error("problem with the gender of the main subject");
                        debugger;
                        return false;
                    }

                    if (qtag_gender === main_subj_gender) {
                        console.log("No error.");
                        // debugger;
                        return false;
                    }
                    else if (main_subj_gender == "mf") {
                        if (qtag_pronoun == "he") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else if (qtag_pronoun == "she") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else {
                            // debugger;
                            // AddCorrectionsHe(ruleManager, result, 0, qtag_subj_index);
                            AddCorrections(ruleManager, result, 0, qtag_subj_index, "he");
                            // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                            AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");
                            return true;
                        }

                    }
                    else if (main_subj_gender == "mfn") {
                        if (qtag_pronoun == "he") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else if (qtag_pronoun == "she") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else if (qtag_pronoun == "it") {
                            console.log("No error.");
                            // debugger;
                            return false;
                        }
                        else {
                            // AddCorrectionsHe(ruleManager, result, 0, qtag_subj_index);
                            AddCorrections(ruleManager, result, 0, qtag_subj_index, "he");
                            // AddCorrectionsShe(ruleManager, result, 1, qtag_subj_index);
                            AddCorrections(ruleManager, result, 1, qtag_subj_index, "she");
                            // AddCorrectionsIt(ruleManager, result, 2, qtag_subj_index);
                            AddCorrections(ruleManager, result, 2, qtag_subj_index, "it");
                            // debugger;
                            return true;
                        }

                    }
                    else {
                        if (main_subj_gender == "m") {
                            qtag_subj_word = "he";
                        }
                        else if (main_subj_gender == "f") {
                            qtag_subj_word = "she";
                        }
                        else if (main_subj_gender == "n") {
                            qtag_subj_word = "it";
                        }
                        else {
                            // debugger;
                            return false;
                        }

                        AddCorrections(ruleManager, result, 0, qtag_subj_index, qtag_subj_word);
                        // debugger;
                        return true;

                    }
                }
                else if (sentence.tokens[sentenceTokenOfsubject_Index].pos.endsWith("S")) {
                    if (qtag_word === "they") {
                        console.log("No error.");
                        // debugger;
                        return false;
                    }
                    else {

                        // AddCorrectionsThey(ruleManager, result, 0, qtag_subj_index);
                        AddCorrections(ruleManager, result, 0, qtag_subj_index, "they");
                        // debugger;
                        return true;
                    }
                }
                else if (sentence.tokens[sentenceTokenOfsubject_Index].pos == "NN") {
                    if (qtag_word === "it") {
                        console.log("No error.");
                        // debugger;
                        return false;
                    }
                    else {

                        // AddCorrectionsIt(ruleManager, result, 0, qtag_subj_index);
                        AddCorrections(ruleManager, result, 0, qtag_subj_index, "it");
                        // debugger;
                        return true;
                    }
                }
            }

        }
    }

    else {
        // debugger;
        return false;
    }

    return false;
}
