/*
* Function checking if the q-tag corresponds to the principle clause.
*
* 1. the auxiliairy verbs are the same
* 2. the subjects are of the same gender
*
*/
function AddError(ruleManager, result, auxIndice) {
    var correction = {
        "type": "insertAfter",
        "token": {
            "pos": "RB",
            "lemma": "not",
            "word": "not"
        },
        "targetTokenIndice": auxIndice
    };
    ruleManager.AddError(result, 0, correction);
}

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

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

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

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

    // aux1: auxiliairy verb of the main phrase
    var aux1TokenIndex = result.tokenRules[1].tokenIndices[0];
    // console.log("aux1 = " + aux1TokenIndex);
    aux1Word = sentence.tokens[aux1TokenIndex].word;
    aux1POS = sentence.tokens[aux1TokenIndex].pos;
    aux1lemma = sentence.tokens[aux1TokenIndex].lemma;
    // tokenRule_phrase == 4
    let auxQtag = null;
    // let phrase = null;
    
    let auxQtagIndex = -1;

    if (phrase.tokenTags &&
        phrase.tokenTags.aux &&
        phrase.tokenTags.aux.length) {
        auxQtag = sentence.tokens[phrase.tokenTags.aux[0]];
        auxQtagIndex = phrase.tokenTags.aux[0];
        // debugger;
    }
    else{
        debugger;
        return false;
    }
    


    if (aux1POS == "MD" && auxQtag.pos == "MD") {
        if (aux1lemma.localeCompare(auxQtag.lemma) != 0) {
            // console.log("the modal verbs are different");
            //debugger;
            return false;
        }
    }
    // checking if the forms of auxiliairies are the same
    if (aux1POS != "MD" && auxQtag.pos != "MD" && (aux1Word.localeCompare(auxQtag.word) != 0)) {
        //debugger;
        // console.log("the auxiliairies are different");
        return false;
    }

    //checking if there subject is the same
    var subject1group = result.tokenRules[0].tokenIndices;
    //debugger;
    // var re = new RegExp("^NN");
    //startsWith
    var nounToken_id = undefined;
    var pronounToken_id = undefined;

    for (let i = 0; i < subject1group.length; i++) {
        // if (re.test(sentence.tokens[i].pos)) {
        element_pos = sentence.tokens[i].pos;
        if (element_pos.startsWith("NN")) {
            nounToken_id = i;
            break;
        }
        else if (element_pos == "PRP") {
            pronounToken_id = i;
            break;
        }
    }

    if (nounToken_id == undefined && pronounToken_id == undefined) {
        //debugger;
        return false;
    }
    // var subject1Lemma = sentence.tokens[nounToken_id].lemma;

    // 
    // looking for the lemma of the 2nd subject
    //
    let pronounToken = null;
    if (phrase.tokenTags &&
        phrase.tokenTags.pronoun &&
        phrase.tokenTags.pronoun.length) {
        pronounToken = sentence.tokens[phrase.tokenTags.pronoun[0]];
        //debugger;
    }
    else{
        debugger;
        return false;
    }
    // console.log("the pronoun of the q-tag = " + pronounToken.word);
    // pronounNumber:"plural"

    // identification of the token index for the modification 'insert'

    let auxIndice = -1;
    if (sentence.tokens[auxQtagIndex+1].lemma == "not"){
        auxIndice = aux1TokenIndex;
        //debugger;
    }
    else if (sentence.tokens[auxQtagIndex+2].lemma == "not"){
        auxIndice = aux1TokenIndex;
        //debugger;
    }
    else{
        auxIndice = auxQtagIndex+1;
        //debugger;
    }

    if (pronounToken_id != null) {
        if (sentence.tokens[pronounToken_id].word === pronounToken.word) {
            // console.log("both pronouns are the same");
            // debugger;
            AddError(ruleManager, result, auxIndice);
            return true;
        }
    }
    else if (nounToken_id != null) {

        let subject1pos = sentence.tokens[nounToken_id].pos;

        if ((subject1pos === "NNS" || subject1pos === "NNPS") && pronounToken.pronounNumber == "plural") {
            // console.log("the pos of the subject " + subject1pos + "et pronoun number are plural");
            AddError(ruleManager, result, auxIndice);
            return true;
        }
        var subject1Gender = sentence.tokens[nounToken_id].gender;


        if ((subject1pos == "NN") || (subject1pos == "NNP")) {
            if ((subject1Gender === "f") && (pronounToken.gender === "f")) {
                // console.log("feminin");
                //debugger;
                AddError(ruleManager, result, auxIndice);
                return true;
            }
            if ((subject1Gender === "m") && (pronounToken.gender === "m")) {
                // console.log("masculin");
                //debugger;
                AddError(ruleManager, result, auxIndice);
                return true;
            }
            if ((subject1Gender === "mf") && (pronounToken.gender === "m" || pronounToken.gender === "f")) {
                // console.log("both");
                // debugger;
                AddError(ruleManager, result, auxIndice);
                return true;
            }
            if ((subject1Gender == undefined) && (pronounToken.gender === "n")) {
                // console.log("neutre");
                AddError(ruleManager, result, auxIndice);
                //debugger;
                return true;
            }
        }
    }

    //debugger;

    return false;
}