'use strict';
const queue = require("queue");
const fs = require('fs');
const crypto = require('crypto');
const ModificationTokenServices = require("../js/textAnalysis/ModificationTokenServices");
const nlpServices = require("../js/nlpServices");

module.exports = {
    CreateQueue: function (testName, callback) {
        console.log(testName+" Started");
        var q = queue();
        q.concurrency = 5;
        q.autostart = true;
        //q.timeout = 10000;

        q.on('success', function (result, job) {
            //console.log('job finished processing:', job.toString().replace(/\n/g, ''));
            // console.log('length= ' + q.length);
            if (q.length == 0) {
                if(callback)
                {
                    callback();
                }
                console.log(testName + " ended");
            }
        });
        q.on('timeout', function (next, job) {
            //console.log('job timed out:', job.toString().replace(/\n/g, ''));
            console.log('timeout length= ' + q.length);
            if (q.length == 0) {
                console.log(testName + " ended");
            }
            else {
                next();
            }
        });
        q.on('error', function (err, job) {
            debugger;
        });
        return q;
    },
    GetModificationTokenServices: function(type, nlpSentence, results)
    {
        var nlpResult = {
            sentences: [nlpSentence]
        };
        var sentenceAnalysisResult = {
            sentence: nlpServices.ConvertSentence(nlpResult, 0),
        };
        sentenceAnalysisResult[type] = results;
        var sentenceAnalysisResults = [];
        sentenceAnalysisResults.push(sentenceAnalysisResult);

        var modifTokenServices = new ModificationTokenServices(type);
        modifTokenServices.Initialize();
        modifTokenServices.SetSentenceAnalysisResults(sentenceAnalysisResults);
        return modifTokenServices;
    },

    CompareLearningCards: function (reference, learningCards) {
        var result = {
        };
        var missingsLCs = {};
        var newLCs = {};
        var difLCs = {};
        for (var key in learningCards) {
            var count = learningCards[key];
            var refCount = reference[key];
            if (refCount == undefined) {
                if (!result.news) {
                    result.news = {};
                }
                result.news[key] = count;
            }
            else if (count != refCount) {
                if (count > refCount) {
                    if (!result.news) {
                        result.news = {};
                    }
                    result.news[key] = count - refCount;
                }
                else {
                    if (!result.missings) {
                        result.missings = {};
                    }
                    result.missings[key] = refCount - count;
                }
            }
        }
        for (var key in reference) {
            var refCount = reference[key];
            var count = learningCards[key];
            if (count == undefined) {
                if (!result.missings) {
                    result.missings = {};
                }
                result.missings[key] = refCount;
            }
        }
        return result;
    },
    AddCorpusSentence: function (folder, text, json) {
        const hash = crypto.createHmac('sha256', text).digest('hex');
        var path = folder + hash + ".json";
        if (fs.existsSync(path)) {
            console.error("already existing sentence: " + text);
            fs.unlinkSync(path);
        }
        fs.appendFileSync(path, JSON.stringify(json, null, 2));
    }
};