/*
* Function makes the correction of the auxiliairy verb in the question tag
*
*/

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

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

    /*
    * to replace the wrong auxiliairy in the question tag
    * we gether all the information on the auxiliairy in the main clause
    * 
    */

    let MD_form = ruleManager.dictionaries.modal_verb_form_before_not;
    if (!MD_form) {
        console.error("dictionnary error");
        return false;
    }

    let aux_mainIndex = result.tokenRules[1].tokenIndices[0];
    //console.log("aux Verb of main index = " + aux_mainIndex);
    let aux_mainWord = sentence.tokens[aux_mainIndex].word;
    let aux_mainLemma = sentence.tokens[aux_mainIndex].lemma;
    let aux_mainPOS = sentence.tokens[aux_mainIndex].pos;

    let aux_qtagIndex = result.tokenIndices[result.tokenRules[5].tokenIndices[0]];

    if (aux_mainIndex == undefined || aux_qtagIndex == undefined) {
        debugger;
        console.error("problem with auxiliairies");
        return false;
    }

    let aux_qtag = null;

    for (var key in MD_form) {
        if (aux_mainLemma === key) {
            aux_qtag = ruleManager.dictionaries.modal_verb_form_before_not[key].short;
            break;
        }

    }

    if (aux_qtag == undefined) {
        debugger;
        console.error("problem with auxiliairies");
        return false;
    }

    let not_in_qtag_index = null;
    if (result.tokenRules[6].tokenIndices.length == 1) {
        not_in_qtag_index = result.tokenIndices[result.tokenRules[6].tokenIndices[0]];
        var correction = {
            "type": "replaceAttValue",
            "suggestionIndice": 0,
            "att": "word",
            "text": "n't",
            "tokenIndice": not_in_qtag_index
        };

        ruleManager.AddCorrection(result, 0, correction);

        var correction = {
            "type": "replaceAttValue",
            "suggestionIndice": 0,
            "att": "word",
            "text": aux_qtag,
            "tokenIndice": aux_qtagIndex
        };
    
        ruleManager.AddCorrection(result, 0, correction);

        return true;
    }

    var correction = {
        "type": "replaceAttValue",
        "suggestionIndice": 0,
        "att": "word",
        "text": aux_mainLemma,
        "tokenIndice": aux_qtagIndex
    };

    ruleManager.AddCorrection(result, 0, correction);

    var correction = {
        "type": "replaceAttValue",
        "suggestionIndice": 0,
        "att": "lemma",
        "text": aux_mainLemma,
        "tokenIndice": aux_qtagIndex
    };

    ruleManager.AddCorrection(result, 0, correction);

    var correction = {
        "type": "replaceAttValue",
        "suggestionIndice": 0,
        "att": "pos",
        "text": aux_mainPOS,
        "tokenIndice": aux_qtagIndex
    };

    ruleManager.AddCorrection(result, 0, correction);
    return true;
    //debugger;
}