/*
* 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": "delete",
        "tokenIndice": auxIndice+1
    };
    ruleManager.AddError(result, 0, correction);
}

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

    // 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;
    }


    // mainVerb: the verb of the main phrase
    var mainVerbTokenIndex = result.tokenRules[1].tokenIndices[0];
    // console.log("mainVerb index = " + mainVerbTokenIndex);
    mainVerbPOS = sentence.tokens[mainVerbTokenIndex].pos;
    let auxQtag = null;
    let auxIndice = -1;

    /*
    * looking for the tokenTag of the phrase (Qtag) to get the auxiliairy verb of the Qtag
    *
    */

    let phrase = null;

    for (let ti = 0; ti < result.tokenIndices.length; ti++) {
        let token = sentence.tokens[result.tokenIndices[ti]];
        if (token.qtags_not_option) {
            phrase = token.qtags_not_option;
            // debugger;
            ti = result.tokenIndices.length;
            break;
        }
        else if (token.qtags_nt_option) {
            phrase = token.qtags_nt_option;
            // debugger;
            ti = result.tokenIndices.length;
            break;
        }
    }
    if (!phrase) {
        debugger;
        return false;
    }

    if (phrase.tokenTags &&
        phrase.tokenTags.aux &&
        phrase.tokenTags.aux.length) {
            // auxQtag est un objet
        auxQtag = sentence.tokens[phrase.tokenTags.aux[0]];

        /* 
        * auxIndice is used to insert a modification ("not") for
        * the generation of sentences with errors
        */

        auxIndice = phrase.tokenTags.aux[0];
        // debugger;
    }
    else {
        debugger;
        return false;
    }

    /*
    **
    ** Checking if the POS of verbs is the same
    **
    */

    if (mainVerbPOS != auxQtag.pos) {
        // debugger;
        return false;
    }

    if (auxQtag.lemma != "do") {
        // debugger;
        return false;
    }

    //checking the correspondance between subjects
    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"


    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;
}