
'use strict';

const fs = require('fs');
const crypto = require('crypto');
const nlp = require("../wordServices/nlp");
const {getRootPath} = require ("../rootPath")

module.exports = {

    ConvertSentence: function(nlpResult, si)
    {
        var sentence = {
            tokens: []
        };
        var nlpSentence = nlpResult.sentences[si];
        var ti=0;
        for (; ti < nlpSentence.tokens.length; ti++)
        {
            var token = nlpSentence.tokens[ti];
            if (!token)
            {
                debugger;
                continue;
            }
            var newToken = {
                word: token.originalText || token.word
            };
            if (token.lemma)
            {
                newToken.lemma = token.lemma;
            }
            if (token.pos)
            {
                newToken.pos = token.pos;
            }
            if (token.value!= undefined)
            {
                newToken.value = token.value;
            }
            if (token.characterOffsetBegin != undefined &&
                token.characterOffsetEnd != undefined)
            {
                var spaceAfter = 0;
                if (ti+1 < nlpSentence.tokens.length)
                {
                    spaceAfter = nlpSentence.tokens[ti+1].characterOffsetBegin - token.characterOffsetEnd;
                }
                else if (si+1 < nlpResult.sentences.length && 
                    nlpResult.sentences[si+1].tokens.length)
                {
                    spaceAfter = nlpResult.sentences[si+1].tokens[0].characterOffsetBegin - token.characterOffsetEnd;
                }
                var spaceBefore = 0;
                if (ti)
                {
                    spaceBefore = token.characterOffsetBegin - nlpSentence.tokens[ti-1].characterOffsetEnd;
                }
                else if (si && nlpResult.sentences[si-1].tokens.length)
                {
                    var prevSentence =  nlpResult.sentences[si-1];
                    spaceBefore = token.characterOffsetBegin - prevSentence.tokens[prevSentence.tokens.length-1].characterOffsetEnd;
                }
                else{
                    spaceBefore = token.characterOffsetBegin;
                    // if (spaceBefore)
                    // {
                    //     debugger;
                    // }
                }
                newToken.begin = token.characterOffsetBegin;
                newToken.end = token.characterOffsetEnd;
                newToken.spaceBefore = spaceBefore;
                newToken.spaceAfter = spaceAfter;
            }
            sentence.tokens.push(newToken);
        }
        return sentence;
    },

    GetTextNLP: function (language, text, folder, constituency,full) {
        return new Promise((resolve, reject) => {
            const hash = crypto.createHmac('sha256', text).digest('hex');
            var nlpFolder = "./"+ getRootPath() + language + "/NLP/";
            if (folder)
            {
                nlpFolder += folder + "/";
                if (!fs.existsSync(nlpFolder))
                {
                    fs.mkdirSync(nlpFolder);
                }
            }
            var path = nlpFolder + hash + ".json";
            var exists = fs.existsSync(path);
            if (exists) {
                try{
                    var stream = fs.readFileSync(path).toString();
                    var nlpSentence = JSON.parse(stream);
                    resolve(nlpSentence);
                    return;
                }
                catch(e){
                    // debugger;
                    console.error("Error GetTextNLP path="+path);
                }
            }
            nlp.GetTextNLP(language, text, full, constituency)
                .then(result => {
                    var nlpSentence = JSON.stringify(result, null, 2);
                    if (fs.existsSync(path))
                    {
                        console.error("NLP path not exists and exists name= " + hash);
                        // debugger;
                        fs.unlinkSync(path);
                    }
                    fs.appendFileSync(path, nlpSentence);
                    resolve(result);
                }).catch(function (error) {
                    console.error(text);
                    console.error(error.message);
                    debugger;
                    reject(error);
                });
        });
    },
    GetTextNLPAsString: function (text) {
        var value = "";
        if (text && text.sentences) {
            var prevEnd=0;
            for (var si = 0; si < text.sentences.length; si++) {
                var sentence = text.sentences[si];
                for (var ti=0; ti<sentence.tokens.length; ti++)
                {
                    var token = sentence.tokens[ti];
                    for (var spi= prevEnd; spi<token.begin; spi++)
                    {
                        value+=" ";
                    }
                    value += token.word;
                    prevEnd = token.end;
                }
            }
        }
        else {
            debugger;
        }
        return value;
    },
    GetTextNLPAsString2: function (text) {
        var value = "";
        if (text && text.sentences) {
            var prevEnd=0;
            for (var si = 0; si < text.sentences.length; si++) {
                var sentence = text.sentences[si];
                for (var ti=0; ti<sentence.tokens.length; ti++)
                {
                    var token = sentence.tokens[ti];
                    value += token.word;
                    value += token.after;
                    prevEnd = token.end;
                }
            }
        }
        else {
            debugger;
        }
        return value;
    }
};